UserPreferences

DailyNotes/2005/12/15


  1. Getting copyurlplus to copy the HTML of the selection
  2. Notelets
    1. Work
    2. Personal
  3. Today's pictures
  4. Yesterday's pictures
  5. Open Threads

Getting copyurlplus to copy the HTML of the selection

I've found the [WWW]copyurlplus extension for Firefox very useful. It lets me make a selection in a HTML window and then invoke the context menu to format that selection in a variety of ways and place that formatted version on the clipboard. (I've put together a [WWW]little screencast to demonstrate the copyurlplus extension.)

I've been wanting to extend the functionality of the extension to enable grabbing of the HTML of the selection (and not just some text reduction of the selection). I thought that I'd be able to borrow an idea from http://www.squarefree.com/bookmarklets/webdevel.html#partial_source where there is a piece of code:

getSelSource() { x = document.createElement("div"); x.appendChild(window.getSelection().getRangeAt(0).cloneContents()); return x.innerHTML; }

that returns the HTML of the current selection. I can confirm that this code works in a greasemonkey script. Unfortunately, I've had not been able to use the logic of this script in the context of the extension.

That is, I've replaced [WWW]content/copyurlplus/copyurlplusOverlay.js with [WWW]my version.

Essentially, I've made two different attempts at applying the logic. The first is:

function getCurrentHTMLSelection() {

    // borrowed from http://www.squarefree.com/bookmarklets/webdevel.html#partial_source
   
    var sel = "";
    try {
        var x = document.createElement("div");
        var focusedWindow = document.commandDispatcher.focusedWindow;
        var selection = focusedWindow.getSelection.call(focusedWindow);
        x.appendChild(selection.getRangeAt(0).cloneContents());
        sel = x.innerHTML;
    } catch(e) {
        sel = "An exception occurred in the script. Error name: " + e.name + "Error message: " + e.message;
    }
   
    return sel

}

but get sel = undefined

When I try

function getCurrentHTMLSelection2() {

    // borrowed from http://www.squarefree.com/bookmarklets/webdevel.html#partial_source
   
    var sel = "";
    try {
        var x = document.createElement("div");
        x.appendChild(window.getSelection().getRangeAt(0).cloneContents());
        sel = x.innerHTML;
    } catch(e) {
        sel = "An exception occurred in the script. Error name: " + e.name + "Error message: " + e.message;
    }
   
    return sel

}

I get the error:

An exception occurred in the script.  
Error name: NS_ERROR_ILLEGAL_VALUEError message: Component returned failure code: 0x80070057 
(NS_ERROR_ILLEGAL_VALUE) [nsISelection.getRangeAt]

At this point, I don't really understand how document.commandDispatcher works.....

Oh, yes, I did a [WWW]customization of copyurlplus to get what I call %HTMLSEL%:

user_pref("copyurlplus.menus.7.label", "Copy URL + Title + HTML Sel (for wiki)");
user_pref("copyurlplus.menus.7.copy", "[%URL% %TITLE%]: {{{#!html %HTMLSEL% }}}");

Notelets

Work

Personal

[WWW]Digital Photos Even a Miser Can Enjoy - New York Times:

Today, I bought an issue of [WWW]Stone Soup Magazine as a present for a bright and energetic eight-year old girl. I'm glad for [WWW]an ad in the New York Review of Books that prompted me into looking at a magazine that I had never heard of before a couple of days ago.

Today's pictures

Picture308_15Dec05

Yesterday's pictures

IMGP4844
IMGP4845
IMGP4846
sun
cafe at Tang Center
what color is this?
kids around campus
sky
sushi lunch
nigiri
teriyaki chicken
The old Sun Hong Kong
The
       

Open Threads

I usually like to work in parallel on a number of entries. Here I list them so they can be easily noted and accessed: