Firefox WebExtention API: TypeError: browser.browserAction is undefined [duplicate]

橙三吉。 提交于 2019-11-28 12:41:43

browser.browserAction should be called in a background script and not in a content script like you're doing. So assuming this code is in background.js:

function handleClick() {
    console.log("do something.");
    // If you want to something with the content, you will need a content script and messaging
}

browser.browserAction.onClicked.addListener(handleClick);

You add the background_scripts key in manifest.json:

{

  "manifest_version": 2,
  "name": "My Login",
  "version": "3.0",

  "description": "Login to my page",
  "homepage_url": "https://localhost",
  "icons": {
    "48": "icons/button-1.png"
  },
  "background": {
    "scripts": ["background.js"]
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!