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;
}
});
Jurchello
  • 23
  • 1
  • 3
  • 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 '17 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 '17 at 20:38
  • function onWindowLoad() { alert($('.vote span').text()); } – Jurchello Nov 17 '17 at 20:38
  • Looks like my main question is **how read some tag info (using jquery) from active tab?** – Jurchello Nov 17 '17 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 https://developer.chrome.com/extensions/overview to understand all this and look through the samples – Troy Wray Nov 17 '17 at 20:54
  • Possible duplicate of [How to access the webpage DOM rather than the extension page DOM?](https://stackoverflow.com/questions/4532236/how-to-access-the-webpage-dom-rather-than-the-extension-page-dom) – wOxxOm Nov 18 '17 at 06:01

0 Answers0