Chrome Extension: how to capture selected text and send to a web service

Posted by phil swenson on Stack Overflow See other posts from Stack Overflow or by phil swenson
Published on 2010-04-13T03:48:56Z Indexed on 2010/04/13 3:52 UTC
Read the original article Hit count: 356

For the Google Chrome extension, I need to capture selected text in a web page and send to a web service. I'm stuck!

First I tried a bookmarklet, but Chrome on Mac seems to have some bookmarklet bugs so I decided to write an extension.

I use this code in my ext:

    function getSelText(){
    var txt = 'nothing';
    if (window.getSelection){
        txt = "1" + window.getSelection();
    } else if (document.getSelection) {
        txt = "2" + document.getSelection();
    } else if (document.selection) {
        txt = "3" + document.selection.createRange().text;
    } else txt = "wtf";
    return txt;
}
var selection = getSelText();
alert("selection = " + selection);

When I click on my extension icon, I get a "1". So I think the act of selecting outside the browser window is causing the text to not be seen by the browser as "selected" any more.

Just a theory....

thoughts?

© Stack Overflow or respective owner

Related posts about google-chrome-extension