0

I'm trying to get the DOM node of the current selected text. I'm building a chrome extension and trying to do so from the background file. I know I can get the selection using:

chrome.tabs.executeScript(tab.id, {code:     
'chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
{if (request.method == "getSelection")   
    sendResponse({data:window.getSelection().toString()});
else sendResponse({});});'
});"

chrome.tabs.sendRequest(tab.id, {method: "getSelection"}, function(response){                    
    return respose.data;
});

But I don't know how to get the node (for traversing).

Any ideas?

thanks

adi ohaion
  • 374
  • 6
  • 21

1 Answers1

1

You can execute more code in the content script to get the node, I suspect you know this, but here's a link to that question: How can I get the DOM element which contains the current selection?

But note that you can't send the node back to the background page and have the background page traverse the node or look at it. It has no access to the DOM in the tab, it can only receive messages (which are serialized into text). So whatever work you need to do on the node will need to be done in the content script.

Community
  • 1
  • 1