Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a Google Chrome Extensions with a On and Off Button. How?

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...

like image 458
paintball247 Avatar asked May 18 '13 03:05

paintball247


People also ask

How do I get rid of Chrome extensions without removing the button?

Remove hidden extensions on WindowsOpen the extensions folder to see which extensions remain in their folders. Delete the folder for any extensions you want to remove. Once done, open Chrome and check the extensions list in your preferences. You should no longer see the extensions you permanently removed.


1 Answers

One way you could achieve this is by reading and modifying a boolean variable in a Background Page and use Message Passing to communicate 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..
    }
});
like image 136
QFDev Avatar answered Sep 25 '22 06:09

QFDev