Question

The Situation

I need to automate the copying of a HTML link to the current page that is viewed in the current Firefox Tab into other WYSIWYG editors. This is not the same as copying just the plain-text of the URL, nor is it the same as pasting just the plain-text of the web pages title. This is also not the same thing as navigating to some other web page that has the HTML link to the page of interest, selecting the text with the mouse cursor, and typing CTRL-C to copy it into the current operating systems clipboard (both Linux and Windows, should not make any difference). Only the update to the clipboard is to be automated; the pasting from the clipboard into the target application will be done manually.

The desired use case is as follows:

  1. The user browses to any web page from within Firefox.
  2. The user types some user-specified key sequence that is not in conflict with standard Firefox built-in key bindings.
  3. Firefox will then do only part of what Copy Link Bookmarklet does: Instead of opening up a new separate window/tab and constructing and rendering the HTML for the link, and then requiring the user to waste motion in selecting and copying the link into the clipboard, the extension will then format the HTML itself and copy that into the clipboard directly.
  4. The user then selects any of the targets described below and types CTRL-V to paste the formatted text.
  5. The user then sees the link as a link in that target area, and does not see anything literal like http://...

For example, if the webpage browsed to was http://www.google.com, and the user clicked the user-defined key sequence, and if the user pasted it into some Google Document, what they would see in that document is not http://www.google.com nor would they see Google, but instead would see what you would see when you read this in StackOverflow in a web browser: Google

Now, there are Firefox extensions and bookmarklets that come close, but they all involve no net reduction in mouse motion and/or key press overhead, which is the most time-wasting aspect of this frequently occuring use case. My searches for an existing extension turned up nothing that exactly meets my needs (see Research section below). Therefore, I think I may need to roll my own extension (or modify an existing one), unless someone can point me to an existing extension that provides this functionality.

The extension I have in mind should work in Firefox version 11 or greater running on either Linux or any version of Windows. Only Firefox and a suitable Firefox extension should be needed, and not any other special software.

Targets of the paste should be:

  1. GMail compose text areas
  2. Google Documents
  3. Microsoft Word documents
  4. Microsoft Outlook compose text areas.
  5. Any other WYSIWYG editor such as the Blogger post editor.
  6. Notepad (in which case it is the web page title that is pasted only and not the URL, or both the web page title and URL as separate plaintext; either way).

About user-specified key bindings: If there was an extension already that did the above but without providing the ability to bind a keybinding to it, then I would expect to be able to use the keyconfig extension extension to handle that aspect. Actually, that might even be preferable; I don't know yet.

Research

Below are approaches I investigated that came close to what I want, but did not exactly meet the need:

  1. Hacking on Copy Link Bookmarklet won't work because, from what I can tell, there is no way to update the OS's clipboard from a bookmarklet, hence why I think that a Firefox extension is required.
  2. In a Firefox extension, how can I copy rich text / links to the clipboard?
  3. 3 FireFox Addons to Easier Copy Links and Anchor Texts -- None of the extensions listed do what I want because they force you to use the right mouse button and navigate down one or two levels of context menu, which is wasted motion.
  4. Copy Link Text (CoLT) -- CoLT also supports copying a hyperlink and it’s associated text as a rich-text formatted link, however it does not include a default keybinding. It looks like someone else is attempting to tie keyconfig to CoLT, which might be an option as a solution.
  5. Copy URL Plus -- Looks like it has the copy-to-clipboard logic, but doesn't look like it has been maintained since Firefox 1.x timeframe.
Was it helpful?

Solution

I am answering my own question:

enter image description here The CTRL-SHIFT-F11 binding will silently stop working if both keysnail and keyconfig are installed into the same Firefox browser. The fix for me was to simply uninstall keysnail as I don't use it.

I did not actually need to write my own Firefox extension, but I did need to scrape out a bit of code that copies the richtext link from the Copy Link Text (CoLT) extension and apply it directly as a binding into the keyconfig extension as follows:

  1. Install the keyconfig extension.
  2. Restart Firefox.
  3. After Firefox loads up, type CTRL-SHIFT-F12 to bring up the keyconfig configuration menu.
  4. On the bottom of the page, click on the Add a new key button.
  5. In the Name field, type in some suitable name such as Copy Rich Text Link to Current Page.
  6. Type in the following chunk of Javascript code (This code I carved out of the objCoLT.CopyBoth function inside the content/colt.js file inside the Copy Link Text (CoLT) extension):

    var url = content.document.location.href;
    var text = content.document.title;
    
    // Use the users selection instead of the title if text is selected:
    var selection = document.commandDispatcher.focusedWindow.getSelection().toString();
    if (selection != "")
    {
        text = selection;
    }
    
    var richText = "<a href=\"" + url + "\">" + text + "</a>";
    
    var xfer = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
    xfer.addDataFlavor("text/html");
    
    var htmlString = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    htmlString.data = richText;
    xfer.setTransferData("text/html", htmlString, richText.length * 2);
    
    var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
    clipboard.setData(xfer, null, Components.interfaces.nsIClipboard.kGlobalClipboard);
    
  7. Click Ok.

  8. Back in the main Keyconfig dialog, <disabled> should be shown in the text field to the left of the Apply button.
  9. Click in that text field, and type the keybinding you want to associate with it, such as CTRL-SHIFT-F11.
  10. Click the Apply button.
  11. Click the Close button to close the Keyconfig configuration dialog box.

To test this out, proceed as follows:

  1. In Firefox, navigate to some arbitrary page.
  2. Type in CTRL-SHIFT-F11 (or whatever keybinding you chose above).
  3. Notice that no dialog boxes popup; that is intentional.
  4. Open up Google Documents, and Create a new document.
  5. Click in the new document, and type CTRL-V.
  6. You should see the HTML/rich-text form of the link pasted in.
  7. Click on the link and then click on the URL to the left of Change.
  8. The browser should open up the original page corresponding to that URL.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top