0

I'm writing simple Chrome extension for current page parsing. I get the page source, but how can I find any tag and get its text? Here is my code fragment:

chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
    // here I want read tag text e.g. <div class="getter">How get it?</div> 
    message.innerText = request.source;
}
});
6
  • Any content script that's been injected into the page can access the DOM e.g. element = document.getElementsByTagName('div'). Many people include jQuery to make this kind of task easier. You can include jQuery along with your content script without fear of it interfering with the page itself, even if it has jQuery loaded. Not sure why you've put your code in a message handler, that's not necessary (in case you thought it was). Digest that and try and refine your question.
    – Troy Wray
    Nov 17, 2017 at 20:17
  • @Troy Wray yes, I know how to use jquery and simple javascript to read/write text to html-tags. But I don't know how do it from the extension. I tried do it without a message handler. But I got info from my extention pop-up, but not from the current tab. As here:
    – Jurchello
    Nov 17, 2017 at 20:38
  • function onWindowLoad() { alert($('.vote span').text()); }
    – Jurchello
    Nov 17, 2017 at 20:38
  • Looks like my main question is how read some tag info (using jquery) from active tab?
    – Jurchello
    Nov 17, 2017 at 20:40
  • You need the script for accessing the DOM to be in a content script. You must get that script into the active tab, either by specifying it in the manifest or by injecting it using chrome.tabs.executeScript. Read developer.chrome.com/extensions/overview to understand all this and look through the samples
    – Troy Wray
    Nov 17, 2017 at 20:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.