0

I'm writing a chrome extension, and I want to be able to disable JavaScript based on a condition. So the page loads, a bunch of JavaScript runs, and if my condition is met then I'd like to disable all JavaScript. Is that possible? We would need to terminate/stop the currently running scripts.

I don't care too much if my chrome extension's script is stopped/disabled as well. But it would be nice if I could keep just that javascript running.

Thanks

Alex Amato
  • 1,591
  • 4
  • 19
  • 32
  • 3
    Possible duplicate of [Google Chrome extension - how to turn JavaScript on/off?](http://stackoverflow.com/questions/4663359/google-chrome-extension-how-to-turn-javascript-on-off) – Haibara Ai Sep 07 '16 at 07:49

1 Answers1

0

One hacky way to achieve this is by defining a object with all your functions in it and then deleting said object from memory like so.

var obj = {


    first : function(){

        return "1";


    },

    second : function(){

        return "2";


    }


};

console.log( obj.first() ); // "1"
console.log( obj.second() ); // "2"

delete obj; // will remove from memory thus disabling it.

//will now return error
console.log( obj.first() );
console.log( obj.second() );

Please note though if you declare a var outside the var obj scope that points to obj it will still exist even after using delete.

var obj = {


    first : function(){

        return "1";


    },

    second : function(){

        return "2";


    }


};

var stillAlive = obj;
delete obj; // will it remove from memory?

//Ha gotcha i'm still here.
console.log( stillAlive.first() );
console.log( stillAlive.second() );
Darkrum
  • 1,325
  • 1
  • 10
  • 27
  • Thanks, but I'm wondering a bit more how I would do it without redefining a javascript, like if I could make a chrome extension disable the javascript with a toggle, or after a short wait. – Alex Amato Sep 13 '16 at 06:35
  • The comment under your question is what you want then. – Darkrum Sep 13 '16 at 13:39