Copy Link to Clipboard in Wiki Format from Mozilla/Firefox

/!\ I don't use this bookmarklet anymore--instead, I use the Make Link Firefox extension instead (and CopyAllURLs, which can created a wiki-formatted bulleted list with all open tabs). If it doesn't work, feel free to edit this page to fix it and add your comments! -- BradeyHonsinger 2009-10-09 00:23:40

I save URLS in the wiki a lot, and it's annoying to have to copy the URL and title over separately and paste them into the wiki `[<url> <title>]` format. This code takes the current URL and title and copies them into the clipboard in wiki format:

var copyText = "[" + document.location + " " + document.title + "]";

/* You have to sign the code to enable this or allow the action in
    about:config by changing
    user_pref("signed.applets.codebase_principal_support", true); */
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
    
// Store support string in an object.
var str = 
    Components.classes["@mozilla.org/supports-string;1"].createInstance(
        Components.interfaces.nsISupportsString
        );
if (!str) return false;
str.data=copyText;

// Make transferable.
var trans = 
    Components.classes["@mozilla.org/widget/transferable;1"].createInstance(
        Components.interfaces.nsITransferable
        );
if (!trans) return false;

// Specify what datatypes we want to obtain, which is text in this case.
trans.addDataFlavor("text/unicode");
trans.setTransferData("text/unicode",str,copyText.length*2);
 
var clipid=Components.interfaces.nsIClipboard;
var clip = 
    Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
if (!clip) return false;

clip.setData(trans,null,clipid.kGlobalClipboard);

Running that through the Bookmarklet Crunchinator and setting signed.applets.codebase_principal_support to true in about:config does the trick (looks ugly, but you can cut-and-paste it):

javascript:(function(){var copyText="["+document.location+" "+document.title+"]";netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');var str=Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);if(!str)return false;str.data=copyText;var trans=Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);if(!trans)return false;trans.addDataFlavor("text/unicode");trans.setTransferData("text/unicode",str,copyText.length*2);var clipid=Components.interfaces.nsIClipboard;var clip=Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);if(!clip)return false;clip.setData(trans,null,clipid.kGlobalClipboard);})();

It should be even easier to create a bookmarklet for IE, but I don't use it--see the links below.

Resources:

MoinMoin: BradeyHonsinger/CopyWikiLinkBookmarklet (last edited 2009-10-09 00:23:41 by BradeyHonsinger)