0

In my Google Chrome extension it adds a javaScript file or code when the user click on the browser action. ex:

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null,
                       {code:"document.body.bgColor='red'"});// or any .js code file
});

how can I remove this JavaScript or the code when user click on a button. Assume that there is a code like below triggered when user click on a button.

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

So i want to stop this tabcolor.js execution after User click on the same button. How can I do this ?

Is there any method to stop ExecuteScript ?

prime
  • 14,464
  • 14
  • 99
  • 131
  • You have to override the functionality inside `tabcolor.js` . You can't just stop execution. – Jonny Sooter Sep 11 '13 at 17:49
  • @Jonny Sooter : Can you please explain it with more details. How can i do it ? How can i override it? – prime Sep 11 '13 at 17:52
  • If you want the script to execute once only from what I understand, then you might wanna check out the **storage api** for chrome extensions. Basically you can set up a flag / marker value when the user clicks on the button the first time and execute the `tabcolor.js` depending upon the value of the flag variable you set. I hope this gets you started in the right direction. – Vivek Pradhan Sep 11 '13 at 18:07
  • @Vivek Pradhan : Sorry i didn't get your answer. Actually I want to toggle the functionality. When a user click a button it should inject the relevant .js file and when the user click the same button it should remove that .js file. likewise it should continue. – prime Sep 11 '13 at 18:17
  • 1
    So you mean the javascript should be `undone` when the user clicks on the button you provide? – Vivek Pradhan Sep 11 '13 at 18:21
  • @Vivek Pradhan : Exactly , and it should be re injected and work after user click that button again. – prime Sep 11 '13 at 18:26
  • 1
    Ok, after a bit of searching. I found [this post](http://stackoverflow.com/a/9699373) that addresses a similar problem. Basically it uses the `webrequest` api which allows blocking specific scripts. Documentation can be found [here](https://developer.chrome.com/extensions/webRequest.html) – Vivek Pradhan Sep 11 '13 at 18:39
  • @Vivek Pradhan : thank you very much. I'll check that. – prime Sep 11 '13 at 18:42

1 Answers1

1

Inside your tabcolor.js you have scripts, functions, variables, etc. that execute and do stuff. To make this script "not execute" and not do that stuff, you need to replace the code that does the work. For example:

//tabcolor.js
var banana = function() {
    doMyStuff;
}

If you override the banana function, it will still execute but will accomplish nothing.

banana = null;

This way you replace the code that was inside tabcolor.js and it won't do it again.

Jonny Sooter
  • 2,417
  • 1
  • 24
  • 40
  • Thanks for the explanation. Can I know how can I override the javaScript code. How can I override the `tabcolor.js` file from outside? (sorry my JS Knowledge is not very much strong) – prime Sep 11 '13 at 18:14
  • Just look in your `tabcolor.js` for the names of the objects you want to replace and do `name = null;`. As long as you do that after you load `tabcolor.js` the `null` will override whatever is inside that file. – Jonny Sooter Sep 11 '13 at 18:20
  • So where should i write the code `name = null`. Is it in a seperate .js file ? or in the `button click` event ? Those objects are in a .js file. So how can we access those names from out side. When i tried to do that i got the message `**name** is not defined` – prime Sep 11 '13 at 18:30