浏览器扩展:动态禁用内容脚本

问题描述 投票:0回答:1

我有一个浏览器扩展程序,带有在所有URL上运行的内容脚本。

{
  "name": "foobar",
  "version": "0.1.0",
  "permissions": [],
  "manifest_version": 2,
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content.js"]
    }
  ]
}

我现在想允许用户在某些站点上disable内容脚本,例如,通过单击弹出窗口中的按钮。我将如何处理?

google-chrome-extension firefox-webextensions
1个回答
0
投票

从后台脚本(具有"activeTab""https://*/"权限),可以使用chrome.tabs.executeScript注入任何脚本:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  const inject = /.*:\/\/.*stackoverflow.com\/.*/.test(request.url);

  if (inject) {
    chrome.tabs.executeScript({
      file: 'foobar.js'
    });
  }

  // Send an empty response to avoid warning
  sendResponse({});
});
© www.soinside.com 2019 - 2024. All rights reserved.