最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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:

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",

  "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

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:

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.",

  "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./MaxLord/ctrl-vk/tree/with_bug

Share Improve this question asked Feb 16, 2013 at 19:06 Max ZhuravlevMax Zhuravlev 3343 silver badges13 bronze badges 1
  • I was figured out, what when we pass first argument as null to executeScript() - this mean that we trying to execute script in current tab. It is obviously not we want to get.. We currently need a way how to get this damn tab's id.. – milushov Commented Feb 16, 2013 at 23:38
Add a ment  | 

3 Answers 3

Reset to default 2

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.

(Would've submitted this as ment 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.

We should notice that, in the manifest cofig:

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

in the "matches" part, only http, https are matched, so if you load your extension in page like: 'chrome://extensions/', or 'file:///D:xxx', that error will occur.

You may load your extension in the page with the url 'http://'; or add more rules in your 'matches' array.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论