2

I'm trying to create a chrome extension that scrapes some content from a particular website and then opens a new tab and does stuff with the scraped data.

Below is a test I made to see how I might do this. Unfortunately I can't seem to execute the newtab-script.js file as I get this error:

Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-extension://FAKEIDgfdsgfdsgfdsgdsgfdsgFAKEID/newpage.html". Extension manifest must request permission to access this host. at Object.callback (chrome-extension://FAKEIDgfdsgfdsgfdsgdsgfdsgFAKEID/background.js:43:25)

websitescrape.js

var button = document.createElement("button");
button.classList.add("web-scrape");
button.innerHTML = "scrape web";
document.querySelector('.placeIWantToPutButton').appendChild(button);

button.addEventListener('click', scrapeData);

function scrapeData(){
    //do website scraping stuff here...
    var fakedata = [{test:"data1"},{test:"data2"}];

    //send scraped data to background.js
    chrome.runtime.sendMessage({setdata: fakedata}, function(tab){
        //callback
    });
}

background.js

var dataTempStorage = [];

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (request.setdata) {
        dataTempStorage = request.setdata;

        chrome.tabs.create({
            'url': chrome.extension.getURL('newpage.html')
        }, function(tab) {
            chrome.tabs.executeScript(tab.id, {
                file:chrome.extension.getURL("newtab-script.js")});
        });        
    }

    if (request == "getdata") {
        sendResponse({data: dataTempStorage});
    }    
});

newtab-script.js

chrome.runtime.sendMessage("getdata", function (response) {
    doStuff(response.data);
});

function doStuff(){
    //Do staff on newpage.html with data scraped from original page
}

newpage.html

// page ready to be filled with awesome content!
1
  • 1
    FYI, it's usually helpful to post the manifest of the extension. Commented Nov 21, 2015 at 19:55

1 Answer 1

8

Cause: content scripts can't be injected into extension pages with chrome-extension:// scheme.

Solution: since you have control over that html page just reference the content script file explicitly.

newpage.html:

        <script src="newtab-script.js"></script>
    </body>
</html>

And don't use executeScript.

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! So simple haha! I'll accept answer in 5mins. Thanks for helping me out @wOxxOm :)
you REALLY helped me! =D Tks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.