如何使用chrome.tabs.executeScript()对网页进行更改



我想在浏览器中单击我的扩展图标,并在控制台中显示我当前在浏览器中选择的网页/选项卡的"test"。

manifest.json:

{
    "name": "some name",
    "version": "1.0",
    "description": "some description",
    "manifest_version": 2,
    "permissions": ["storage", "tabs", "activeTab"],

    "browser_action": {
      "default_title": "hello!",
      "default_popup": "popup.html",
      "default_icon": "icon.png"
    },
    "background": {
      "scripts": ["background.js"],
      "persistent": true
    }
}

背景.js:

chrome.browserAction.onClicked.addListener(function(){
chrome.tabs.executeScript(null, {
    code: "test"
})

我使用"选项卡"和"activeTab"作为权限,因为我相信这些是运行此代码所必需的。

如果我单击扩展程序,它不会显示错误,但它也不会在我所在的网页的控制台日志中显示"test"。

是否有明显的原因无法从我提供的代码中工作?

chrome.browserAction.onClicked.addListener(function(){
  browser.tabs.executeScript({
    code: `console.log('test');`
  });
});

来源: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/executeScript

最新更新