Does a scriptish script need an updated page to run?

This script helps in the verification process in the online store. Everything works correctly, except for the need to refresh the page to start the program initially.

Interestingly, the problem is with the cache, because it works with other products that were previously viewed. I also tried messing around with @run-at no avail. I am using the Scriptish extension and 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 user script only work when updating?

+3
javascript greasemonkey userscripts scriptish
source share
1 answer

This is a classic issue with pages that change their content through AJAX.
@run-at no effect, because the target content loads long after the load event. And the "new" pages are actually being rewritten through AJAX - until you refresh the page, at least.

Change the script to @run-at document-end (or omit the line), and then, depending on the landing page and how you use it, it may be enough to disable hashchange , as shown in "I need to refresh the page for my Greasemonkey script to run? " or as shown in this other answer .

waitForKeyElements Tool More . Your script may 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); } 


See Also, “Selecting and Activating the Right Controls on an AJAX-Managed Site” .

+1
source share

All Articles