JavaScript reload / refresh steps

What I want to do is call the function from the / GM Script extension after the page in FireFox reloads / refreshes ...

Figure this out:

  • I have a webpage that asks for a username and password.
  • My code fills these values ​​and sends the credentials
  • The web page reloads and displays a page asking you to enter a predefined number.
  • I enter the number
  • The web page reloads and displays a page asking you to select an option from the drop-down menu
  • I choose ... .. you get the idea ..

I decided that I wanted to write some kind of JavaScript to do all this .. and since it requires persistence, and I don’t have the ability to change the source site, I was thinking of writing a FireFox or GreaseMonkey extension - basically anything on the client side .

Something like the DOMContentReloaded event would have acted (if it existed):

addEventListener ("DOMContentReloaded", pageReloaded, false);

Typical test cases for such code:

  • Find out how long it took between page updates
  • Wait for the second instance of page refresh to happen, and then redirected to another page, etc.

All this will be done from the FireFox extension (or GreaseMonkey in case sluting in GM is easier / better / recommended) - should this be all taken into account?

+4
source share
5 answers

I updated my answer to reflect your updated question below.

As rjk mentioned, you can use the onbeforeunload event to perform an action when the page refreshes.

Here is a solution that should work with some potential problems, which I will explain below:

 // Just some cookie utils from: http://www.quirksmode.org/js/cookies.html var Cookie = { create: function(name, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); var expires = "; expires=" + date.toGMTString(); } else var expires = ""; document.cookie = name + "=" + value + expires + "; path=/"; }, read: function(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length, c.length); } } return null; }, erase: function(name) { createCookie(name, "", -1); } } window.addEventListener('beforeunload', pageClosed, false); window.addEventListener('load', pageOpened, false); function pageOpened() { var timestampString = Cookie.read('closeTester'); if (timestampString) { var timestamp = new Date(timestampString); var temp = new Date(); temp.setMinutes(temp.getMinutes() - 1, temp.getSeconds() - 30); // If this is true, the load is a re-load/refresh if (timestamp > temp) { var counter = Cookie.read('counter'); if (counter) { counter++; Cookie.create('counter', counter); } else { Cookie.create('counter', 1); } } Cookie.erase('closeTester'); } } function pageClosed() { Cookie.create('closeTester', new Date()); } 

What this means is to create a temporary cookie when the page is unloaded. A cookie stores the timestamp of the current time. When the page loads, this cookie is read and the timestamp is checked to see how old it is. If it is within 30 seconds, it will increase the counter.

I chose 30 seconds because I don’t know how intense your site data is or how long it takes to load. If your site is fast, I would change it to 5-10 seconds to make the script more accurate. To do this, change the number of seconds to 50-55 seconds, and you will receive 10 seconds or 5 seconds, respectively.

It only monitors reloads while the browser remains open. When it is closed, the counter will be lost. You can change this by adding expiration to the cookie ' count '.

Since the timestamp cookie is only supported while the browser is open, this script is trustworthy because it will not consider closing and reopening the browser. The only time you may have problems is when a user opens a tab, then closes the tab and reopens it at the time you specify.

All this is done without the firefox extension and will work in any browser except IE (unless you fix the event handler to work with it). I do not know how to do this using the firefox extension, although it is possible that it is better to use the extension.

UPDATE

Now that I understand what you're trying to do a little better, here are a few things that might be helpful:

I included the script above (obviously) specifically for tracking updates. However, it can also be used to simply track navigation. In a state that checks if (timestamp > temp) , you can call another function that will perform some action (the action will only be executed when the page is refreshed, etc.). If you need persistent data, you just need to save it in a cookie, as I do above. If you do not need to save the number of pages, you can save any other information in this cookie.

I have never created a greasemonkey script before, but I assume that since it can refer to elements in the DOM, it can also refer to document cookies. This will allow you to store persistent data for the site using greasemonkey, simply using the code that I included above. If you cannot access the cookie DOM, you can use the greasemonkey GM_setValue() and GM_getValue() functions to store persistent data. This data will be stored in a browser session, as far as I know. You will need to enter some sentinel values ​​to make sure that it only works on page loads (something like the timestamp example that I used above).

As for jQuery, this is the javascript API that is used for JavaScript in general. I do not know how useful this is for GreaseMonkey scripts, although I would suggest that this would work if you used it in a script. If you want to get started with jQuery, check out their documentation . It is really well done. The only part of my example that can really use jQuery effectively is the event handling parts. Here's how you handle events in jQuery:

 $().ready(pageOpened); $().unload(pageClosed); 

Replace the calls to ' window.addEventListener() with the two lines above, and you have a cross browser implementation. Since you are using the greasemonkey script, however, the jQuery API becomes unnecessary if you do not want to manipulate the DOM, which is very convenient for jQuery.

+6
source

I don’t think there is a way to save you JavaScript by refreshing the page in the browser - it loads all of this again ...

Perhaps you can start with window.onbeforeunload to overload the default reload function and use AJAX to reload some main div ...

0
source

Assigning window.onbeforeunload would be a workable solution if it did not start when the page is also closed!

More suggestions .. maybe the FF Extension or GM Script is specific?

0
source

Use window.name or document.cookie - both properties will be saved when the page is refreshed.

0
source

919756 If you succeed, TC employees will catch you and ban you. I wonder how I knew? Hm ...

0
source

All Articles