javascript - Getting HTML out of selectionrange from a web page in Mozilla Firefox - Stack Overflow

I currently using the following the get the selected text from a webpage into a custom firefox extensio

I currently using the following the get the selected text from a webpage into a custom firefox extension:

getSelectedText: function(){
    var textWindow = documentmandDispatcher.focusedWindow;
    var text = textWindow.getSelection();      
    if (text == null) {text =' ';}
    text = text.toString();
    text = text.replace(/^\s*$/ , "");
    text = text.replace(/\r/g, "\r");
    text = text.replace(/\n/g, "\n");
    text = text.replace(/^\s+|\s+$/g , " ");
    text = text.replace(new RegExp(/\u2019/g), "'");
    text = text.replace(new RegExp(/\u201A/g), ",");
    text = text.replace(new RegExp(/\u201B/g), "'");
    return {str:text};
}

This works just fine for plain text.

My problem is that i want all the elements of the webpage copied as well ( kind of like the webclips feature in safari )

Use case - If the user selects through a webpage with formatted text and images, I want the underlying HTML to get copied as well, so that I can accurately paste it into another XUL window - even send the contents as a rich HTML email if I so wish.

Any pointers?

I currently using the following the get the selected text from a webpage into a custom firefox extension:

getSelectedText: function(){
    var textWindow = document.mandDispatcher.focusedWindow;
    var text = textWindow.getSelection();      
    if (text == null) {text =' ';}
    text = text.toString();
    text = text.replace(/^\s*$/ , "");
    text = text.replace(/\r/g, "\r");
    text = text.replace(/\n/g, "\n");
    text = text.replace(/^\s+|\s+$/g , " ");
    text = text.replace(new RegExp(/\u2019/g), "'");
    text = text.replace(new RegExp(/\u201A/g), ",");
    text = text.replace(new RegExp(/\u201B/g), "'");
    return {str:text};
}

This works just fine for plain text.

My problem is that i want all the elements of the webpage copied as well ( kind of like the webclips feature in safari )

Use case - If the user selects through a webpage with formatted text and images, I want the underlying HTML to get copied as well, so that I can accurately paste it into another XUL window - even send the contents as a rich HTML email if I so wish.

Any pointers?

Share Improve this question asked Dec 29, 2009 at 2:55 Manish ChakravartyManish Chakravarty 6011 gold badge7 silver badges9 bronze badges 6
  • Just out of curiosity, how do you propose to deal with stylesheets? – Joel Commented Dec 29, 2009 at 3:01
  • @Joel : I dont think you can get the stylesheet out , ie, you just capture the Html and that's it. – Manish Chakravarty Commented Feb 14, 2010 at 4:39
  • But any linked resources (images, css, etc.) will have to resolved if you want the display to be at all representative. – Joel Commented Feb 15, 2010 at 0:03
  • @Joel - check this out - manish-chaks.livejournal./103700.html – Manish Chakravarty Commented Feb 16, 2010 at 8:38
  • Ah, I see your absolutifyUrlsInNode function. But how does n.setAttribute(a, n[a]); do the trick? – Joel Commented Feb 16, 2010 at 14:56
 |  Show 1 more ment

1 Answer 1

Reset to default 9

Try using this code:

var range = window.getSelection().getRangeAt(0);
var content = range.cloneContents();

After this code is executed, content will be a document fragment that contains a copy of the select DOM nodes. Note that event listeners will not be cloned. For more information go to https://developer.mozilla/en/DOM/range.cloneContents

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744145375a4560397.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信