Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.tabs.executeScript: How to get access to variable from content script in background script?

How to get access to variable app from content script app.js in background script background.js?

Here is how I try it (background.js):

chrome.tabs.executeScript(null, { file: "app.js" }, function() {
   app.getSettings('authorizeInProgress'); //...
});

Here is what I get: Error during tabs.executeScript: Cannot access contents of url "chrome-devtools://devtools/devtools.html?docked=true&dockSide=bottom&toolbarColor=rgba(223,223,223,1)&textColor=rgba(0,0,0,1)". Extension manifest must request permission to access this host. \n Uncaught ReferenceError: app is not defined

Here is manifest.json:

{
  "name": "ctrl-vk",
  "version": "0.1.3",
  "manifest_version": 2,
  "description": "Chrome extension for ctrl+v insertion of images to vk.com",

  "content_scripts": [{
    "matches": [
        "http://*/*",
        "https://*/*"
    ],
    "js": ["jquery-1.9.1.min.js"
    ],
    "run_at": "document_end"
  }],

  "web_accessible_resources": [
    "jquery-1.9.1.min.js"
  ],

  "permissions" : [
    "tabs",
    "http://*/*",
    "https://*/*"
  ],

  "background": {
    "persistent": false,
    "scripts": ["background.js"]
  }
}

Full code for instance, at github

https://github.com/MaxLord/ctrl-vk/tree/with_bug

like image 397
Max Zhuravlev Avatar asked Feb 16 '13 19:02

Max Zhuravlev


People also ask

What is content_ scripts?

Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.

What does background js do in Chrome extension?

js') is a JavaScript script that runs once our extension either gets installed or the user refreshes the extension manually. The background script doesn't actually have access to any of the webpages your user is viewing, so your background script can't manipulate the DOM.


2 Answers

To avoid above error use following code

if (tab.url.indexOf("chrome-devtools://") == -1) {
    chrome.tabs.executeScript(tabId, {
        file: "app.js"
    }, function () {

        if (app.getSettings('authorizeInProgress')) {
            alert('my tab');
            REDIRECT_URI = app.getSettings('REDIRECT_URI');
            if (tab.url.indexOf(REDIRECT_URI + "#access_token") >= 0) {
                app.setSettings('authorize_in_progress', false);
                chrome.tabs.remove(tabId);
                return app.finishAuthorize(tab.url);
            }
        } else {
            alert('not my');
        }

    });
}

instead of

chrome.tabs.executeScript(null, {
    file: "app.js"
}, function () {

    if (app.getSettings('authorizeInProgress')) {
        alert('my tab');
        REDIRECT_URI = app.getSettings('REDIRECT_URI');
        if (tab.url.indexOf(REDIRECT_URI + "#access_token") >= 0) {
            app.setSettings('authorize_in_progress', false);
            chrome.tabs.remove(tabId);
            return app.finishAuthorize(tab.url);
        }
    } else {
        alert('not my');
    }

});

Explanation

  • chrome://extensions/ page also fires chrome.tabs.onUpdated event, to avoid it we have to add a filter to skip all dev-tool pages.
like image 72
Sudarshan Avatar answered Sep 16 '22 17:09

Sudarshan


(Would've submitted this as comment to the accepted answer but still lack the required reputation)

You should also give the tabId to chrome.tabs.executeScript as first argument when you have it. Otherwise you risk user switching windows/tabs right after requesting a URL and background.js doing executeScript against wrong page.

While fairly obvious on hindsight it threw me for a loop when I got that same error message "Cannot access contents of url "chrome-devtools://.." even though my chrome.tabs.onUpdated eventhandler was checking that the page user requested had some specific domain name just before doing the executeScript call.

So keep in mind, chrome.tabs.executeScript(null,..) runs the script in active window, even if the active window might be developer tools inspector.

like image 40
stt Avatar answered Sep 16 '22 17:09

stt