gpt4 book ai didi

javascript - 如何使用 Greasemonkey 更改 JavaScript 变量?

转载 作者:行者123 更新时间:2023-11-29 17:29:49 27 4
gpt4 key购买 nike

这是我要修改的页面,我想绕过倒计时,脚本应该怎么写?有没有一种方法可以使用 Greasemonkey 将变量 document.licenseform.btnSubmit.disabled 更改为 yes?


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>dsfsdf</title>
</head>
<body>
<form name="licenseform" method="post" action="">
<input name="btnSubmit" type="button" value="我同意">
</form>
<SCRIPT language=javascript type=text/javascript>
<!--
var secs = 9;
var wait = secs * 1000;
document.licenseform.btnSubmit.value = "我同意 [" + secs + "]";
document.licenseform.btnSubmit.disabled = true;

for(i = 1; i <= secs; i++)
{
window.setTimeout("Update(" + i + ")", i * 1000);
//这一句很关键,记得参数写法为("update("+i+")",i*1000)
}
window.setTimeout("Timer()", wait);


function Update(num)
{
if(num != secs)
{
printnr = (wait / 1000) - num;
document.licenseform.btnSubmit.value = "我同意 [" + printnr + "]";
}
}

function Timer()
{
document.licenseform.btnSubmit.disabled = false;
document.licenseform.btnSubmit.value = " 我同意 ";
}
-->
</SCRIPT>
</td>
<!--网页中部中栏代码结束-->
</body>
</html>

最佳答案

使用 unsafeWindow 更安全的替代方法是将代码注入(inject)文档。您注入(inject)的代码将在与页面代码相同的上下文中运行,因此它将可以直接访问那里的所有变量。但它无法访问用户脚本代码其他部分中的变量或函数。

注入(inject)代码的另一个好处是,以这种方式编写的用户脚本可以在 Chrome 和 Firefox 中运行。 Chrome 根本不支持 unsafeWindow

我最喜欢的注入(inject)代码的方式是编写一个函数,然后使用这个可重用的代码来取回该函数的源代码:

// Inject function so that in will run in the same context as other
// scripts on the page.
function inject(func) {
var source = func.toString();
var script = document.createElement('script');
// Put parenthesis after source so that it will be invoked.
script.innerHTML = "("+ source +")()";
document.body.appendChild(script);
}

要切换btnSubmit,您可以编写如下脚本:

function enableBtnSubmit() {
document.licenseform.btnSubmit.disabled = false;
document.licenseform.btnSubmit.value = " 我同意 ";
// Or just invoke Timer()
}

function inject(func) {
var source = func.toString();
var script = document.createElement('script');
script.innerHTML = "("+ source +")()";
document.body.appendChild(script);
}

inject(enableBtnSubmit);

请记住,当您以这种方式使用函数的序列化形式时,正常的闭包作用域将不起作用。您注入(inject)的函数将无法访问脚本中的变量,除非它们是在该函数内部定义的。

关于javascript - 如何使用 Greasemonkey 更改 JavaScript 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722075/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com