Do I need to refresh the page for my Greasemonkey script to run?

Therefore, I know that this is related to AJAX, but I read several topics and do not quite understand what I need to do. Everything is working fine now, but I need to refresh the page for my script to run. What needs to be done with my code so that it works without updating?

// ==UserScript== // @name Job Aids // @description Aid in closing tickets // @include https://techaccess.ad.qintra.com/WorkJobs/WorkJobs.aspx* // @namespace camzilla.net // @version 1.1.20121128 // ==/UserScript== var url = window.location.href.split(".aspx"); var page = url[1].toLowerCase(); if (page == "#finaltest") { window.addEventListener ("load", finalResults, false); } else if (page == "#threetoneslope") { window.addEventListener ("load", toneSlopeResults, false); } else if (page == "#codes") { window.addEventListener ("load", closingComments, false); } else if (page == "#cras") { window.addEventListener ("load", crasResults, false); } else if (page == "#jobinfo") { window.addEventListener ("load", addLinks, false); } else if (page == "") { if (getCookie("updater") == null) { var d = new Date(); setCookie("updater", d.getTime(), 1); try { updateCheck(); } catch(err) { // alert('Update checking failed'); } } // setTimeout(promptDispatch, 1000); } function addLinks() { var mydiv = document.querySelector('div[data-bind="CurrentJob.addr"]'); var address = 'https://maps.google.com/maps?q=' + mydiv.textContent + ", UT"; address.replace(/ /g, '+'); var a = document.createElement("a"); var txt = document.createTextNode(mydiv.textContent); mydiv.textContent = ""; a.appendChild(txt); a.href = address; a.target = '_blank'; mydiv.appendChild(a); mydiv = document.querySelector('div[data-bind="CurrentJob.cktid"]'); address = 'http://acmspjv1.interprise.com/cgi-bin/QC/DSL/dslam6100Int.pl?telephoneNum='; address+= mydiv.textContent; a = document.createElement("a"); txt = document.createTextNode(mydiv.textContent); mydiv.textContent = ""; a.appendChild(txt); a.href = address; a.target = '_blank'; mydiv.appendChild(a); } function updateCheck() { try { GM_xmlhttpRequest( { method: 'GET', url: 'http://camzilla.net/downloads/jobAids.user.js?v' + getCookie("updater"), headers: {'Cache-Control': 'no-cache'}, onload: function(resp) { var local_version, remote_version, rt, script_name; rt = resp.responseText; remote_version = (/@version\s*(.*?)\s*$/m.exec(rt)[1]); local_version = GM_info.script.version; if (remote_version != local_version) { if(confirm(GM_info.script.name+' update v'+remote_version+ ' is available.\nWould you like to install it now?')) { GM_openInTab('http://camzilla.net/downloads/jobAids.user.js'); } else { alert('You will be reminded again tomorrow'); } } } }); } catch(err) { // do something here } } function setCookie(c_name, value, exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value; } function getCookie(c_name) { var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i<ARRcookies.length;i++) { x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); x=x.replace(/^\s+|\s+$/g,""); if (x==c_name) { return unescape(y); } } return null; } function finalResults() { var dc, current, loss, noise, pi, lb; dc = getRandom(51, 49, 2); current = getRandom(37, 29, 2); loss = getRandom(7, 2, 2); noise = getRandom(7, 1, 2); pi = getRandom(67, 60, 2); lb = getRandom(79, 70, 2); document.getElementById('finalTestInsightNo').click(); document.getElementById("finalTestBeforeAcTg").value = "0.00"; document.getElementById("finalTestBeforeAcRg").value = "0.00"; document.getElementById("finalTestBeforeAcTr").value = "0.00"; document.getElementById("finalTestBeforeDcTr").value = dc; document.getElementById("finalTestBeforeDcTg").value = "0.00"; document.getElementById("finalTestBeforeDcRg").value = dc; document.getElementById("finalTestBeforeLoopCurrent").value = current; document.getElementById("finalTestAfterLoss").value = loss; document.getElementById("finalTestAfterNoise").value = noise; document.getElementById("finalTestAfterPi").value = pi; document.getElementById("finalTestAfterLb").value = lb; document.getElementById("finalTestAfterLb").focus(); window.scrollTo(0, document.body.scrollHeight-250); // Store cookies to be used on cras page if needed setCookie("loss", loss, 1); setCookie("pi", pi, 1); setCookie("current", current, 1); setCookie("noise", noise, 1); setCookie("lb", lb, 1); } function crasResults() { document.getElementById("crasCircuitLoss").value = getCookie("loss"); document.getElementById("crasPowerInfluence").value = getCookie("pi"); document.getElementById("crasLineCurrent").value = getCookie("current"); document.getElementById("crasCircuitNoise").value = getCookie("noise"); document.getElementById("crasBalance").value = getCookie("lb"); document.getElementById('crasFoundInF2').click(); } function toneSlopeResults() { var dc = getRandom(51, 49, 2); document.getElementById("ToneSlopeInsightNo").click(); document.getElementById("ToneSlopeDmarcTaggedYes").click(); document.getElementById("ToneSlopeClecDtPresentYes").click(); document.getElementById("ToneSlopeBeforeDcTr").value = dc; document.getElementById("ToneSlopeBeforeDcTg").value = "0.00"; document.getElementById("ToneSlopeBeforeDcRg").value = dc; document.getElementById("ToneSlopeRingbackTr").value = getRandom(90,85,2); document.getElementById("ToneSlopeAfterResistTg").value = getRandom(998,20,0); document.getElementById("ToneSlopeAfterResistRg").value = getRandom(998,20,0); document.getElementById("ToneSlopeAfterResistTr").value = getRandom(998,20,0); document.getElementById("ToneSlopeAfter404").value = "0.00"; document.getElementById("ToneSlopeAfter1004").value = getRandom(7, 3, 2); document.getElementById("ToneSlopeAfter2804").value = "0.00"; document.getElementById("ToneSlopeAfterNoise").value = getRandom(7, 1, 2); document.getElementById("ToneSlopeAfterLb").value = getRandom(79, 70, 2); document.getElementById("ToneSlopeCotName").value = "NA"; document.getElementById("ToneSlopeTn").focus(); } function closingComments() { document.getElementById("flatRateJacks").value = "0"; document.getElementById("rewiredJacks").value = "0"; document.getElementById("tripNo").click(); document.getElementById("marketMass").click(); document.getElementById("custTypeRes").click(); document.getElementById("pairChangeNo").click(); document.getElementById("tempDropNo").click(); document.getElementById("goodCbrYes").click(); document.getElementById("jobCloseComments").focus(); window.scrollTo(0, 5000); } function getRandom(max, min, tenths) { var result = min + (Math.random() * (max - min + 1)); return result.toFixed(tenths); } 
+3
javascript greasemonkey
source share
2 answers

For more information and a similar scenario, see "addEventListener only works on the update page?" .

The page elements that your script expects will undoubtedly appear after the load event is fired. Also, from your comments, it looks like all sections of the page are exchanging AJAX, but AJAX is polite enough to change the URL hash. This means that you want to disable the hashchange event.

Do not use addEventListener ("load"... in this case. Use the waitForKeyElements () utility with hashchange .

Without refactoring the entire script to use jQuery (which will give clearer and more reliable code), replace everything up to function addLinks() {... with:

 // ==UserScript== // @name Job Aids // @description Aid in closing tickets // @include https://techaccess.ad.qintra.com/WorkJobs/WorkJobs.aspx* // @namespace camzilla.net // @version 1.1.20121128 // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js // @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @grant GM_addStyle // ==/UserScript== /*- The @grant directive is needed to work around a design change introduced in GM 1.0. It restores the sandbox. */ //-- Pages are "loaded" via AJAX... window.addEventListener ("hashchange", fireOnNewPage, false); waitForKeyElements ("#crasCircuitLoss", crasResults); waitForKeyElements ("#finalTestInsightNo", finalResults); waitForKeyElements ("#flatRateJacks", closingComments); waitForKeyElements ("#ToneSlopeInsightNo", toneSlopeResults); waitForKeyElements ("div[data-bind="CurrentJob.addr"]", addLinks); function fireOnNewPage () { switch (location.hash.toLowerCase() ) { case "#finaltest": case "#threetoneslope": case "#codes": case "#cras": case "#jobinfo": //-- No action needed, waitForKeyElements() handles this. break; default: if (getCookie("updater") == null) { var d = new Date(); setCookie("updater", d.getTime(), 1); try { updateCheck(); } catch(err) { // alert('Update checking failed'); } } break; } } fireOnNewPage (); //-- Initial run on initial, full page load. 
+6
source share

check out some ajax examples over the net by searching for example: http://www.w3schools.com/ajax/ajax_examples.asp

AJAX or (A) synchronous (J) avascript (A) nd (X) ML (which is interesting, as a rule, usually has a tendency to use JSON these days), this is a system in which Javascript uses a browser object to communicate with a remote server. In general, this is the ability to update the client interface without having to go to another page. Before we begin, a word of caution.

Ajax is not recommended for authorization of login and publication. Users may disable Javascript or may be limited to run Javascript due to IT policies.

With this in mind, it is recommended that you do not use AJAX as the only solution for critical user functions! Always come back! Note. This wiki post community uses jQuery to show examples of AJAX calls. It is recommended for beginners as it hides browser compatibility issues when calling AJAX. Please check out the jQuery website for more information on jQuery.

Note. The examples use communication with the PHP server, but any language on the server side will work.

AJAX callbacks

 function makeAjaxCall() { $.ajax({ url: 'ajax/test.html', success: function(data) { alert('Horray the AJAX call succeeded!'); }, error: function(xhr, error) { alert('Holy errors batman!'); } }); } 

The nature of AJAX calls

AJAX calls can be either asynchronous or synchronous. Asynchronous means the browser will execute an AJAX request and continue to do other things. Synchronous means that the browser will pause until the AJAX call ends. Here is an example of the differences in the two codes:

 // An asynchronous call // This is the default $.ajax({ url: '/server.php', success: function(data) { alert('Horray the AJAX call succeeded!'); }, error: function(xhr, error) { alert('Holy errors batman!'); } }); // This will get called right away myFunction(); Now for a synchronous call: // A synchronous call $.ajax({ url: '/server.php', async: false, // set the property here success: function(data) { alert('Horray the AJAX call succeeded!'); }, error: function(xhr, error) { alert('Holy errors batman!'); } }); 
-one
source share

All Articles