后台脚本 chrome.tabs 在 chrome 扩展中未定义?

2024-06-20

我试过这个:

背景.js

chrome.browserAction.onClicked.addListener(function (activeTab) {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
        console.log(response.farewell);
      });
    });
});

foo.js:

chrome.runtime.onMessage.addListener(

  function (request, sender, sendResponse) {
    new contentFunction()

    console.log(sender.tab ?
      "from a content script:" + sender.tab.url :
      "from the extension");
    if (request.greeting == "hello") {
      sendResponse({ farewell: "goodbye" });
    }
  });

显现:

{
  "manifest_version": 2,
  "name": "foo",
  "browser_action": {

  },
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
    "activeTab",
    "tabs",
    "<all_urls>"

  ],
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": ["foo.js"]
    }
  ]
}

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

后台脚本 chrome.tabs 在 chrome 扩展中未定义? 的相关文章

随机推荐