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.