HaNdTriX / generator-chrome-extension-kickstart

Scaffold out a Web Extension http://yeoman.io
MIT License
239 stars 33 forks source link

How can I add support for scraping web site ? #34

Closed yogesh-kamble closed 6 years ago

yogesh-kamble commented 6 years ago

Following code I tried in background.js

window.addEventListener('load', function (evt) {
    chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null, {
        file: 'popup.js'
    });
});

// Listen to messages from the payload.js script and write to popout.html
chrome.runtime.onMessage.addListener(function (message) {
    console.log(message)
    document.getElementById('pagetitle').innerHTML = message;
});

and in payload.js

// send the page title as a chrome message

var element = document.evaluate('<SOME_XPATH>' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue

chrome.runtime.sendMessage(element.text);

but in payload.js document contain popup.html dom object. how can I access opened page html DOM in payload.js ? or is there any other way to scrap web site data?

Thanks

HaNdTriX commented 6 years ago

You basically need some kind of content script where you do the scraping. And send its payload back to the background page. Since this is not an issue with this boilerplate please use https://stackoverflow.com/.