问题
I want to learn how to make a script in Greasemonkey to modify variables in a page or bypass a delaying function.
Here is the script found in the middle of the page:
<script language="Javascript">
x300=100;
function countdown() {
x300--;
if (x300 == 0) {
document.getElementById('countdown').innerHTML =
'<strong>Proceed to URL - <a href="http://XXXX.com/11123324">click here</a>!</strong>';
}
if (x300 > 0) {
document.getElementById('countdown').innerHTML =
'You will be redirected in ' + x300 + ' seconds.';
setTimeout('countdown()',100000);
}
}
countdown();
</script>
Of course I want to do a script that will redirect me to http://XXXX.com/11123324,
I am still noob so the best script I made was:
// ==UserScript==
// @name aaa
// @include bbbb
// @grant none
// ==/UserScript==
x300=1;
countdown()
but it didn't work :(
回答1:
Update:
Based on the original question (now deleted), and the apparent target page, the variable x300 and the function countdown() are global. But the variable name x300 actually changes from page to page. This means that it is not a good choice for scripting.
The function name countdown() appears to be constant though. So, you can extract the desired link from the function source using regex:
// ==UserScript==
// @name aaa
// @include bbbb
// @grant none
// ==/UserScript==
var addrFound = false;
var cntDwnFuncSrc = window.countdown; // Use unsafeWindow if grant is not none.
if (cntDwnFuncSrc) {
cntDwnFuncSrc = countdown.toString ();
var payloadAddrMatch = cntDwnFuncSrc.match (/href="([^"]+?)"/);
if (payloadAddrMatch && payloadAddrMatch.length > 1) {
location.assign (payloadAddrMatch[1]);
addrFound = true;
}
}
if ( ! addrFound) {
alert ("Payload address not found. Site probably changed");
}
Given your sample page, your userscript should have worked. That it didn't, means you are omitting or changing a key detail in your question!
What errors do you get in the error console (CtrlShiftJ)?
What happens when you run the following code in the Firefox or Firebug console?
console.log (x300); console.log (countdown);What is the URL of the target page so that we can see what is really going on?
来源:https://stackoverflow.com/questions/15447263/how-to-bypass-a-javascript-function-using-greasemonkey