一些网站(即 Steam 社区市场)要求用户手动选中特定复选框以进行重复操作,例如购买元素。
我希望始终选中该复选框。
- 网址:
http://steamcommunity.com/market/listings/730/USP-S%20%7C%20Torque%20(Field-Tested)
- 元素:
<input id="market_buynow_dialog_accept_ssa" type="checkbox" value="0" name="accept_ssa">
这可以用 Tampermonkey 完成吗?
我找到了 document.getElementById("checkbox").checked = true;
这对我来说似乎合乎逻辑。我把它放在一个新的 Tampermonkey 脚本中,并将 Steam 市场添加到脚本激活的网站列表中,但它不起作用。
最佳答案
- 在应该执行用户脚本的 url 中找到一个模式,例如如果它是
http://steamcommunity.com/market/listings/730/USP-S%20%7C%20Torque%20(Field -Tested)
然后我们可以假设以730
开头的部分是易变的,所以我们将在@include
中用*
替换它键。 - 我们应该等待复选框元素被添加到页面上,有两种方法:MutationObserver基于(我将使用 setMutationHandler 包装器)和基于
setInterval
的(最著名的包装器是 waitForKeyElements )。两者都通过@require
键插入。
// ==UserScript==
// @name Steam - accept the agreement
// @include http://steamcommunity.com/market/listings/*
// @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js
// ==/UserScript==
// maybe the elements are already on the page
checkThem([].slice.call(document.querySelectorAll('input[type="checkbox"]')));
// but anyway set a MutationObserver handler for them
setMutationHandler(document, 'input[type="checkbox"]', checkThem);
function checkThem(nodes) {
nodes.forEach(function(n) { n.checked = true });
}
更多信息:Greasespot wiki .
关于javascript - 使用用户脚本自动选中复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33764551/