我的 friend 有一个在线网页,它有一个内联脚本标签,有一个JavaScript函数:
(#1):
var domain = window.location.protocol + "//" + window.location.host + '/';
$(document).ready(function() {
var count = 5;
countdown = setInterval(function() {
if (count == 0) {
$('#countdow').hide();
$('#link-news').show()
} else {
$('#countdow').text(count);
count--
}
}, 1700);
$('#link-news').click(function() {
var urls = $('input[name=linknexttop]').val();
if (urls == 1) {
$('input[name=linknexttop]').val(2);
$.ajax({
type: "GET",
url: domain + "click.html",
data: "code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b",
contentType: "application/json; charset=utf-8",
success: function(html) {
//alert(html);
window.location = html;
}
})
}
})
});
他提出了一个难题:“我可以在不修改他的原始代码的情况下运行下面的代码吗?”。
(#2):
$('input[name=linknexttop]').val(2);
$.ajax({
type: "GET",
url: domain + "click.html",
data: "code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b",
contentType: "application/json; charset=utf-8",
success: function(html) {
//alert(html);
window.location = html;
}
游戏规则:“我只能在他的原始代码下面插入我的代码;所以,我不能更改他的代码中的任何内容。而且,最重要的是,code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b
是随机的。”。
为了节省等待时间,我用了这个extension阻止 countdown();
; 的工作并将此代码添加到其中:
(#3):
$('#countdow').hide();
$('#link-news').show();
<小时/>
接下来,我尝试传递urls = $('input[name=linknexttop]').val();
。我尝试使用 urls == 1;
;但是,我失败了。
我的问题是:“有什么方法可以传递 urls = $('input[name=linknexttop]').val();
,并且接受该游戏的所有规则? ”。如果可以的话请告诉我。
最佳答案
如果相关脚本位于内联 script
标记中,您可以通过循环遍历脚本来找到它,然后从中提取代码,并在插入的代码中使用它。
按照这些思路,您可以将其作为 script
元素插入:
(function() {
var code;
$("script").each(function() {
var match = /data=: "code=([^"]+)"/.exec($(this).text());
if (match) {
code = match[1];
return false;
}
});
$('#link-news').off("click");
$('#link-news').click(function() {
if (code) {
$('input[name=linknexttop]').val(2);
$.ajax({
type: "GET",
url: domain + "click.html",
data: "code=" + code,
contentType: "application/json; charset=utf-8",
success: function(html) {
//alert(html);
window.location = html;
}
});
}
});
})();
在上面,我假设您应该接管以前的事件处理程序,而不是添加到它。这就是 .off("click")
的作用。
关于javascript - 如何在 JavaScript 中通过这个谜题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31739366/