Reputation: 23
I'm trying, for a few days now, to modify a variable during a script's execution.
The HTML page has a script tag, which calls a JavaScript file :
<script type="text/javascript" src="script.js"></script>
We have something like this in the JS file :
/*File's beggining*/
val = 1;
if (val == 1) { window.open('getout.html', '_self');}
...
/*File's ending*/
I would like to modify val
's value to avoid redirection.
I've been searching for a few days; but I'm stuck. If someone can give me a hint, it would be very nice !
Thank you for reading !
Upvotes: 2
Views: 1117
Reputation: 4309
What you could do is run your script before any of the page's scripts run, and declare val
as an un-assignable property of the global object window
with Object.defineProperty
:
// ==UserScript==
// ...
// @run-at document-start
// ==/UserScript==
Object.defineProperty(window, 'val', { value: 0 });
Subsequent assignments to global variable val
will have no effects.
val = true;
console.log(val); // => 0
And it still works and doesn't throw if the variable gets redeclared at the same scope with var
.
That's assuming the code you posted is in the global scope. If it isn't, then there's no way to override that value from your script. In that case I'd try another avenue and directly override the window.open
function.
Upvotes: 1