-1

Using the Tampermonkey extension in Chrome, my script will fail to execute as soon as a variable is undefined. I've tried using an if conditional but the console just spits out the "ERROR! Execution of script..." message as soon as it reads the variable.

The variable is undefined because, using JSON.parse, it grabs a value that exists in another part of the website. I would like to make it so it avoids this undefined variable and continues with the rest of the script instead of stopping right after failing to get the value.

Example:

We have variable 1 and variable 2 in website.com/test1.html

The script runs fine and outputs the value of both variables.

However, website.com/test2.html only has the value that variable 2 is assigned to, but the value of variable 1 is non-existent.

The script stops executing as soon as this is detected and Chrome's console outputs it was undefined, but fails to provide the value of variable 2.

How can I work around this?

wOxxOm
  • 43,497
  • 7
  • 75
  • 96
TBG
  • 215
  • 1
  • 9

1 Answers1

-1

You can use following condition if(typeof variable1 === "undefined")

Amruta Moghe
  • 24
  • 1
  • 4
  • I already tried this. The problem is that Chrome stops executing the script as soon as it finds that the variable is undefined EVEN if there's a conditional below it that indicates what to do if that happens. – TBG Apr 22 '19 at 07:48
  • Can you share the code snippet? – Amruta Moghe Apr 22 '19 at 09:24