WebExtension如何将数据从popup.html发送到内容脚本

时间:2017-01-17 12:07:17

标签: javascript firefox-webextensions

我使用以下代码将弹出工​​具栏中的变量/消息发送到内容脚本。但是,我无法从内容脚本中检索任何内容。当我将'asdf'发送到内容脚本时,我能够在控制台中看到“未定义”。请帮助,我试图关注chrome extension answer,但似乎没有任何效果。我正在使用firefox webextension而不是chrome扩展。

的manifest.json

{
  "manifest_version": 2,
  "name" : "FPV",
  "version" : "1.0",

  "content_scripts":[
    {
      "matches": ["http://www.*.com/*","https://www.*.com/*"],
      "js": ["jquery-3.1.1.min.js","CalculatePost.js"]
    }
  ],

  "permissions": [
    "activeTab"
  ],

  "browser_action": {
      "browser_style" : true,
      "default_icon": "icons/border-48.png",
      "default_title": "FPV",
      "default_popup": "popup/Toolbar.html"
  },

  "background":{
      "scripts" : ["background.js"]
  }}

弹出窗口(Toolbar.html)

<html>
  <head>
    <link rel="stylesheet" href="Toolbar.css"/>
    <script src="Toolbar.js"></script>
  </head>

<body>
  <div> <input type="text" id="AccessToken" placeholder="Access Token" ></input> </div>
  <div class="button CalculatePost"> RUN </div>
</body>
</html>

popup js(Toolbar.js)

    if (e.target.classList.contains("CalculatePost")) { //function to refresh page
      //var AccessToken = document.getElementById('AccessToken').value;
      browser.tabs.executeScript(null, {
             file: "/CalculatePost.js"
       }, function(){
             //browser.tabs.sendMessage(null, AccessToken);
             //trying to pass a data from html to the content scripts
               browser.tabs.sendMessage(null, 'test');
       });
}}

内容脚本(CalculatePost.js)

var received;
browser.runtime.onMessage.addListener(function(message, sender, sendResponse) {
      received = message;
});
console.log(received);

0 个答案:

没有答案