How to run Greasemonkey Script only once

I made a greasemonkey script for the domain, now how do I get it to run only once? As with every start of the domain, I do not want to do this. How to make it run only once, and then how to remove itself or make itself inactive?

Thanks.

+4
source share
2 answers

If you want the script (or part of the script) to be run only once, you need to save the flag in non-volatile memory. A script cannot delete itself or disconnect. But it can exit right away and not run any other code.

To store things based on the site (by domain), use localStorage . To save content in a script + namespace, use GM_setValue . Here's the base script:

// ==UserScript== // @name _Run a GM script one-time only // @namespace _pc // @include http://YOUR_SERVER.COM/YOUR_PATH/* // @grant GM_getValue // @grant GM_setValue // ==/UserScript== var alreadyRun = GM_getValue ("alreadyRun", false); if ( ! alreadyRun) { GM_setValue ("alreadyRun", true); alert ("This part of the script will run exactly once per install."); } 
+5
source

Redefine the function as the last expression in the body:

 function foo() { ... foo = function(){}; } 
+1
source

All Articles