基本Firefox插件-browser.browserAction.onClicked.addListener(...)和browser.tabs.executeScript(...)不起作用

时间:2019-03-02 21:42:18

标签: javascript firefox-addon

我的清单文件:

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

      "description": "myAddon.",

      "icons": {
        "48": "icons/myAddon-48.png",
        "96": "icons/myAddon-96.png"
      },

      "content_scripts": [
        {
          "matches": ["https://mytestmatchwhichworkedfine.pl/*"],
          "js": ["jquery-3.3.1.min.js", "myAddon.js"]
        }
      ],

    "permissions": [
        "activeTab"
    ],

    "browser_action": {
        "default_icon": "icons/myAddon-32.png",
        "default_title": "myAddon"
    }
}

myAddon.js

browser.browserAction.onClicked.addListener((tab) => {
    browser.tabs.executeScript(tab.id, {
        code: `document.body.style.border = "5px solid red"`
    })
})

我希望在单击插件按钮之后执行脚本。当我在脚本中单独编写document.body.style.border =“ 5px纯红色”时,它会执行。我做错了什么?

1 个答案:

答案 0 :(得分:0)

浏览器操作单击侦听器只能在后台脚本中设置。所提供的代码仍将在页面(选项卡)上执行,因为browser.tabs.executeScript(...)执行内容脚本。

  "content_scripts": [
    {
      "matches": ["https://mytestmatchwhichworkedfine.pl/*"],
      "js": ["jquery-3.3.1.min.js"]
    }
  ],

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