网站上有一个按钮,当我查看页面时,我需要自动点击该按钮,它位于以下按钮:
<a class="btn" data-building="main" id="main_buildlink_main" href="#">Submit</a>
但是,当我在greasemonkey脚本中使用以下代码时,它无效:
var submiting = document.createEvent("MouseEvents");
submiting.initEvent("click", true, true);
document.getElementById('main_buildlink_main').dispatchEvent(submiting);
我做错了哪部分代码?
更新
这被标记为重复,所以我在链接页面上尝试了答案,但仍然无效。这是我的代码:
// ==UserScript==
// @name Upgrade
// @namespace Upgadevillage
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @include *
// @version 1
// @grant none
// ==/UserScript==
function upgrade (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
//-- Value match is case-sensitive
waitForKeyElements (
//"#btn_submit a[id='main_buildlink_main']",
"a[id='main_buildlink_main']",
upgrade
);