Search code examples
htmlgreasemonkeytampermonkey

Having an issue with a tampermonkey script constantly refreshing a page


My company has a status screen that we access through PLEX to have a visual reference of the state of machines across the factory floor. We display this status screen across multiple monitors throughout the floor.

The goal is to automate the entire process of restarting the machine to pulling up chrome to PLEX to logging-in and loading the status screen. So far I have everything up to pulling up the status screen, but I'm having an issue now with that.

Because the status screen is selected and loaded in on the same page it is displayed on, my script continues to run after loading the screen and causes the page to constantly reload.

Is there anyway to stop the script from functioning or maybe put a delay after it runs to prevent it from reloading the page?

// ==UserScript==
// @name         status trigger
// @namespace    http://tampermonkey.net/
// @version      2023-12-06
// @description  Trigger Status Load
// @author       You
// @match        */PlantFloorVisual.aspx
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

function kioskGreen() {
    var button = document.getElementById("fltShop_Screen_Key_Link");
button.click();
    //click for dropdown.
}

function kioskBlue() {
    var button = document.getElementById("tr_3");
button.click();
    // Selection for status screen.
}



kioskGreen();
setTimeout(kioskBlue,1000);
        //delay for screen selection.


Solution

  • I cannot work out from the code why your script is re-running. From what I see, the code should run once and stop.

    Perhaps the page is refreshing of its own accord? If so, you can store the last time the script was run and not re-run the script until x hours have passed:

    Note: all code untested - consider it pseudo-code

    //Create alias for "document.querySelector" so you can just type $
    const $ = document.querySelector.bind(document);
    
    const dtLastRunLS = localStorage.getItem('lastrun') || '0';
    const dtcode = new Date().getTime();
    
    //Only run the code if > 8 hours since last run
    if (dtcode - dtLastRunLS > (60*60*8) ){
       $("#fltShop_Screen_Key_Link").click();
       setTimeout( () => {
          $("#tr_3").click();
       },1000);
    }
    
    

    Optionally, you can test if an element exists, and if so, do nothing:

       if ( document.getElementById("tr_3") ){
          //stops here
       } else {
          document.getElementById("fltShop_Screen_Key_Link").click();
          setTimeout( () => {
              document.getElementById("tr_3").click();
          },1000);
       }
    

    Generally, when you click buttons on a page, the page changes and new elements either become visible or are added to the page. You can test for their existence via the method above.

    You can also check whether an element is present but invisible:

    Check if element is visible in DOM