1

Is it possible to run a TamperMonkey Script only once ?

According to me TamperMonkey Script are always executed when the url location does match so I was wondering to know if there was a way to execute a script only once and then to disable it for exemple. So when I refresh the page the script will not be executed again.

Is it possible ?

2 Answers 2

3

Sure.

You could use GM_setValue and GM_getValue to save the URL you run on, and if the URL had already been saved, not re-run the script.

Sign up to request clarification or add additional context in comments.

3 Comments

But what if I want to use 2 different script on a same url ? For exemple : I load 1page script is executed, I refresh the page other script is executed
are GM a kind of global variable ?
I think they're scoped to each userscript.
1

If you want to share info between scripts, you can use localStorage, but:

  • Local storage is not eternal, meaning it can get deleted after a long time
  • Scripts from the webpage itself can read the same local storage meaning they can compromise or detect your script (theoretically)
  • Also, just like with GM_setValue, remember that you don't know in which order the scripts will access the value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.