如何在2个事件之间传递变量:onMessageExternal和onClicked

时间:2017-09-29 17:25:09

标签: javascript google-chrome-extension

我的background.js文件中包含此代码:

var productId = '';

chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    productId = request; << *** I want to use this variable below ***
});


chrome.browserAction.onClicked.addListener(function(tab) {

    chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {

        chrome.tabs.executeScript(null, {file: "script.js"}); << *** the variable above is not readable inside this file ***

    });

});

如何传递productId变量,以便script.js文件可以使用它。谢谢。

更新:

我尝试按照建议使用全局变量,但它无法从文件中读取。

0 个答案:

没有答案