小编gui*_*rac的帖子

上下文菜单不能运行firefox附加WebExtensions

我正在尝试使用WebExtensions API向我的firefox附加组件添加上下文菜单.我需要后台脚本来监听菜单项上的单击并向内容脚本发送消息.这就是我所拥有的:

的manifest.json

{
  "manifest_version": 2,
  "name": "MyExt",
  "version": "0.0.1",

  "description": "Test extension",
  "icons": {
    "48": "icons/icon-48.png"
  },

  "applications": {
    "gecko": {
      "id": "myext@local",
      "strict_min_version": "45.0"
    }
  },

  "permissions": ["contextMenus"],

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

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content-script.js"]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

背景scripts.js中

chrome.contextMenus.create({
    id: "clickme",
    title: "Click me!",
    contexts: ["all"]
});

browser.contextMenus.onClicked.addListener(function(info, tab) {
    console.log("Hello World!");
    sendMessage(info, tab);
});

function sendMessage(info, tab) {
    chrome.tabs.query(
        {active: true, currentWindow: true }, 
        function(tabs) {
            chrome.tabs.sendMessage(tabs[0].id, "Test message from background …
Run Code Online (Sandbox Code Playgroud)

javascript contextmenu firefox-addon firefox-addon-webextensions

5
推荐指数
1
解决办法
981
查看次数