本文介绍了Scriptish脚本需要刷新页面才能运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 此脚本有助于在线商店的结帐流程。一切正常,除了需要刷新页面以使程序最初运行。This script assists in a checkout process at an online store. All is working correctly, except the need to refresh the page to get the program to run initially.我想知道它是否是一个缓存问题,因为它适用于以前查看过的其他产品。我也尝试修补 @ run-at 无济于事。我正在使用 Scriptish 扩展名,它是一个独立的 .js 文件。I wonder if it is a cache problem because it works on other products that have been previously viewed. I also tried tinkering with the @run-at to no avail. I'm using the Scriptish extension and it is a standalone .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();为什么这个用户只能在刷新时运行?Why does this userscript only run on refresh?推荐答案这是一个经典问题,页面通过AJAX更改其内容。 @ run-at 无效,因为目标内容在窗口加载事件后很久加载。并且,新页面实际上是通过AJAX重写 - 直到你刷新页面,至少。This is a classic problem with pages that change their content via AJAX.@run-at has no effect because the targeted content is loaded long after the window load event. And, "new" pages are really complete rewrites via AJAX -- until you refresh the page, at least.将脚本更改为 @run -at document-end (或省略该行)然后,根据目标页面以及您如何使用它,可能足以触发 hashchange 如我必须刷新我的Greasemonkey脚本页面才能运行?或如图所示在其他答案上。Change the script to @run-at document-end (or omit the line) and then, depending on the target page and how you are using it, it may be enough to fire off of hashchange as shown on "I have to refresh the page for my Greasemonkey script to run?" or as shown on this other answer.转到工具是 waitForKeyElements 更多信息 。您的脚本可能非常简单:The go to tool is waitForKeyElementsMore info, 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驱动的网站上选择和激活正确的控件。 这篇关于Scriptish脚本需要刷新页面才能运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 18:34