小编Mr.*_*r.X的帖子

如何仅在特定链接上运行background.js?

我正在尝试编写一个 chrome 扩展,如果标签的链接包含特定的单词/字符串,则在加载标签时会关闭标签。matches我的目的是使用manifest.json. 不幸的是,这不起作用。我的manifest.json看起来像这样:

{
  "manifest_version": 2,
  "name": "Chrome Extension",
  "version": "0.1",
   "permissions": [
    "tabs"
  ],
  "content_scripts": [
    {
      "matches": [
       "<all_urls>"
      ],
      "js": ["content.js"]
    }
  ],
  "background": {
          "matches": [
               "https://www.google.de/",
                "https://sghm.eu/iserv/login"
          ],
          "scripts": ["background.js"],
          "persistent": true
  }
}
Run Code Online (Sandbox Code Playgroud)

我的background.js像这样:

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete') {
      console.log('background running');
      chrome.tabs.remove(tabId, function() { });
  }
})
Run Code Online (Sandbox Code Playgroud)

在我看来,我已经明确表示该脚本仅在google和上运行sghm.eu,那么为什么它在每个加载的页面上运行?

javascript json google-chrome google-chrome-extension

2
推荐指数
1
解决办法
1895
查看次数