Chrome扩展程序自动触发

Kev*_*vin 5 google-chrome google-chrome-extension

我正在制作Chrome扩展程序以查找我正在使用的页面中的所有链接browser_action.我在刷新页面时遇到错误,chrome扩展会自动触发javascript代码.我如何制作它,以便如果你点击浏览器上的刷新,扩展名不会触发?我只想在单击工具栏上的小图标时才能工作.

这就是我的清单的样子

{
  "name": "Find all first",
  "version": "1.0",
  "description": "Find all Linkes",
  "browser_action": {
    "default_icon": "icon.png",
    "popup": "popup.html"
  },
  "icons": {
    "16": "icon.png",
    "128": "icon-big.png"
  },
  "permissions": [
    "tabs",
    "http://*/*",
    "https://*/*"
  ],
  "content_scripts": [ {
    "matches": ["http://*/*", "https://*/*"], 
    "js": ["content.js"]
  }]
}
Run Code Online (Sandbox Code Playgroud)

然后我在我的popup.html中调用它

chrome.tabs.executeScript(null, {file: 'content.js'}, function() {
  console.log('Success');
});
Run Code Online (Sandbox Code Playgroud)

Chr*_*and 7

因为您已contents_scripts在清单中定义,content.js所以每次加载与您匹配的页面时都会作为内容脚本运行matches(因此,任何网页都是如此).

要仅content.js在用户单击"页面操作"按钮时在页面上运行,请content_scripts从清单中删除该部分,以便不会自动运行任何脚本.然后,当单击"页面操作"按钮时,popup.html将按预期执行content.js.