how do i toggle on/off content_scripts in a google extension?

后端 未结 2 712
栀梦
栀梦 2021-02-10 10:39

I have this simple extension, it displays icon on chrome\'s toolbar and shows a enable/disable button. i want to add function to the button to disable or enable the conten

2条回答
  •  一向
    一向 (楼主)
    2021-02-10 11:02

    Try injecting the content script using code rather than the manifest file, something like this:

    chrome.tabs.executeScript(null, {file: "content_script.js"});
    

    You can then use message passing between the background page and your content script to decide whether or not to inject the content script, or perhaps an if statement in the content script itself that only runs when a flag set by the extension's action button.

    You can use a browser action event to detect when the action button is pressed.

提交回复
热议问题