Recently after a discussion with my friend bard we realized how it is easy to obtain HTML source code associated to the current browser (eg Firefox, Flock) selection.
It’s too simple to discuss the snippet, leave the code speaks itself
function selectionToHTMLSource() {
var sel = document.commandDispatcher.focusedWindow.getSelection();
if (sel.rangeCount < 1) {
return "";
}
var node = sel.getRangeAt(0).cloneContents();
var xmlStr = new XMLSerializer().serializeToString(node);
return xmlStr;
}
The code handles single selection, if you need to work with multiple selections like tables rows/cells take a look at Table2Clipboard
Amazon wish list
Thanks a lot dafi. You made my day
Comment by Murali Bala — December 10, 2009 @ 6:10 am
[...] Article original: http://dafizilla.wordpress.com/2008/03/22/get-html-code-from-nsiselection-in-xul/ [...]
Pingback by Extension firefox: Récupérer le contenu d’une sélection au format HTML — Gael Crova — May 15, 2010 @ 3:09 pm