Refresh the page if not available?

I have a separate Raspberry Pi that is showing a webpage from another server.

It reloads after 30 minutes via JavaScript on the webpage.

In some cases, the server is down for a very short time and Chromium shows a normal message This webpage is not available

and stops reloading (because no JavaScript from the page triggers the reload).

In this case, how can I reload the webpage after a few seconds?

Now I got the idea to get the results of a website via AJAX and replace it on the current page if available.

+3


source to share


4 answers


Instead of refreshing the webpage every few minutes, what you can do is ping the server with javascript ( pingjs is a good library that can do this)

Now, if the ping is successful, reload the page. If this fails, wait another 30 seconds and ping again. By doing this continuously, you will basically keep you waiting until the server is open again (i.e. you can execute it)

I think this is a much simpler method compared to making your own java browser and creating a browser plugin.




Additional information . You should use exponential function or timeout checker to avoid unnecessary overhead. those. the first timeout detects the ping is failing, wait 30 seconds, second time wait 30 * (2 ^ 1) seconds, third time wait 30 * (2 ^ 2), and so on until you reach the maximum value.




Note - this assumes that your server is indeed unavailable ... and not only that the html page is not available (there is a slight but noticeable difference)

+5


source


My preferred approach was to copy the webpage locally using a script every 30 minutes and chrome for the local copy.

The advantage is that the script can run every 30 seconds and it checks to see if the page was clicked successfully in the last 30 minutes. If YES, it does nothing. If NO, you can keep trying to pull it out. At the same time, the browser will be set to refresh the page every 5 seconds, but due to the fact that it pulls the local page, there is little to do for each refresh. Then you can determine if what he's pulled has the required content in it.

This approach assumes that your goal is not to refresh the page every few seconds and therefore reduce the load on the remote page.

Use these options to capture the entire page ...

# exit if age of last reload is less than 1800 seconds (30 minutes)
AGE_IN_SECS=$(( $( perl -e 'print time();' ) - $(stat -c "%Y" /success/directory/index.html) ))
[[ $AGE_IN_SECS -lt 1800 ]] && exit

# copy whole page to the current directory
cd /temporary/directory
wget -p -k http://www.example.com/

      



and then you need to check the page somehow to make sure you have what you need, like (using a bash script) ....

RESULT=$(grep -ci "REQUIRED_PATTERN_MATCH" expected_file_name )
[[ $result -gt 0 ]] && cp -r /temporary/directory/* /success/directory
rm -rf /temporary/directory/*

      

Note:

These are just the bare bones of what you need as I don't know the specifics of what you need. But you should also look at trying ...

  • make sure you have a timeout in wget so you don't have multiple wgets running.
  • create some feedback form so you don't hit the remote server when it's a problem.
  • Ideally, show the post on the page if it's more than 40 minutes old, so the viewer knows the problem isn't experienced.
  • you can use chrome update plugin to pull the page locally.
  • you can use a script to change the page after it loads if you want to add additional / changed formatting (like replace css file?)
+2


source


I see three solutions:

  • Load the page into an iframe (if not blocked) and check the content / response).

  • Create a simple browser in java (not that hard even if you don't know this language using webview)

  • Create a plugin for your browser.
+1


source


reloading the page via javascript is pretty simple:

function refresh() {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status === 200)
      document.body.innerHTML = this.responseXML.body;
    else
      setTimeout('refresh', 1500);
  };

  xhr.open('GET', window.location.href);
  xhr.responseType = "document"
  xhr.send();
}

setInterval('refresh', 30*60*1000);

      

this should work as you asked

0


source







All Articles