该脚本有助于在线商店的结帐过程。除了需要刷新页面以使程序最初运行之外,一切正常。
我想知道这是否是缓存问题,因为它适用于以前查看过的其他产品。我也尝试修补@run-at
无济于事。我正在使用脚本式的扩展,它是一个独立的.js
file.
// ==UserScript==
// @id proper-id
// @name proper-name
// @version 1.0
// @namespace
// @description
// @include proper-url-here
// @include about:blank
// @run-at window-load
// ==/UserScript==
for (var i=0; i<document.getElementsByName("skuAndSize")[0].length; i++){
if(document.getElementsByName("skuAndSize")[0].options[i].text == 11) {
document.getElementsByName("skuAndSize")[0].selectedIndex = i;
}
}
document.getElementsByClassName("button-container add-to-cart")[0].click();
为什么此用户脚本仅在刷新时运行?
这是通过 AJAX 更改内容的页面的一个典型问题。
@run-at
没有效果,因为目标内容是在窗口很久之后加载的load
事件。而且,“新”页面实际上是通过 AJAX 完全重写的——至少在您刷新页面之前是这样。
将脚本更改为@run-at document-end
(或省略该行)然后,根据目标页面以及您如何使用它,可能足以触发hashchange
如上所示“我必须刷新页面才能运行 Greasemonkey 脚本吗?” https://stackoverflow.com/q/14024120/331508或如图所示这个另一个答案 https://stackoverflow.com/a/12261238/331508.
The go to tool is waitForKeyElementsMore info https://stackoverflow.com/a/11197969/331508, however. Your script might be as simple as:
// ==UserScript==
// @name _YOUR_SCRIPT_NAME
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// ==/UserScript==
waitForKeyElements ("[name='skuAndSize']", setSizeIndex);
waitForKeyElements (".button-container.add-to-cart", clickAddToCart);
function setSizeIndex (jNode) {
var node = jNode[0];
for (var J = node.options.length - 1; J >= 0; --J) {
if (node.options[J].text == "11") {
node.selectedIndex = J;
break;
}
}
}
function clickAddToCart (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
也可以看看,“在 AJAX 驱动的网站上选择并激活正确的控件” https://stackoverflow.com/q/15048223/331508.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)