admin管理员组

文章数量:1426597

I have by default an External JS called alerton that will run on anywebppage when the extension is enabled. I've also set up a Popup/Menu for when you click the Chrome Extension Icon at the top right. I want to when the user presses the button "off" to Turn off/Remove an external javascript file called "alerton"

After many many hours, I'm at a loss as to what I need to do to get this to work! I've looked at chrome.contentSettings.javascript However it doesn't seem like I can disable just one particular Javascript file.

I'm hoping someone has an answer...

I have by default an External JS called alerton that will run on anywebppage when the extension is enabled. I've also set up a Popup/Menu for when you click the Chrome Extension Icon at the top right. I want to when the user presses the button "off" to Turn off/Remove an external javascript file called "alerton"

After many many hours, I'm at a loss as to what I need to do to get this to work! I've looked at chrome.contentSettings.javascript However it doesn't seem like I can disable just one particular Javascript file.

I'm hoping someone has an answer...

Share Improve this question asked May 18, 2013 at 3:01 f00df00d 5811 gold badge7 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

One way you could achieve this is by reading and modifying a boolean variable in a Background Page and use Message Passing to municate to and from your content-script and popup page. You can define a Background Page in your Manifest as such:

  ....
  "background": {
    "scripts": ["background.js"]
  },
  ....

The background.js would look something like this:

var isExtensionOn = true;

chrome.extension.onMessage.addListener(
function (request, sender, sendResponse) {
    if (request.cmd == "setOnOffState") {
        isExtensionOn = request.data.value;
    }

    if (request.cmd == "getOnOffState") {
        sendResponse(isExtensionOn);
    }
});

From your PopUp.html and your content-script you could then call the background.js to read and set the isExtensionOn variable.

//SET VARIABLE
var isExtensionOn = false;
chrome.extension.sendMessage({ cmd: "setOnOffState", data: { value: isExtensionOn } });

//GET VARIABLE
chrome.extension.sendMessage({ cmd: "isAutoFeedMode" }, function (response) {
    if (response == true) {
     //Run the rest of your content-script in here..
    }
});

本文标签: javascriptStop a Google Chrome Extensions with a On and Off Button HowStack Overflow