I have made changes to my response to address the modifications in your updated inquiry below.
As suggested by rjk, you can utilize the onbeforeunload
event to execute an action upon page refresh.
Below is a solution that is designed to work, with potential issues that I will elaborate on:
// Cookie utility functions sourced 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());
}
This implementation creates a temporary cookie when the page unloads, storing a timestamp of the current time. Upon page reload, the cookie is accessed, and the timestamp is checked to determine whether it is within a 30-second window to increment the counter.
The 30-second window is chosen based on assumptions regarding site speed. For quicker sites, adjusting the window to 5-10 seconds will provide more accuracy by changing the number of seconds to 50-55.
It is important to note that this method only tracks reloads while the browser remains open. To maintain counts after browser closure, an expiration can be added to the 'count
' cookie.
Despite potential anomalies with tab-closing and reopening within the specified time frame, this script is generally reliable, except on IE unless event handler corrections are made. For Firefox extension application, an alternative approach might be necessary.
UPDATE
Based on a clearer understanding of your intentions, here are some additional insights:
The provided script is tailored for tracking refreshes but can also serve for general navigation monitoring. By activating another function within the 'if (timestamp > temp)
' condition, actions can be executed only upon page refresh. Persistent data storage can be achieved by simply utilizing cookies. For Greasemonkey scripts, DOM cookie access is feasible for persistent data storage, or Greasemonkey's GM_setValue()
and GM_getValue()
functions can be employed for cross-session data storage.
Regarding jQuery, while applicable for DOM manipulation in Greasemonkey scripts, event handling operations can be efficiently conducted without it, as shown in the simplified event handling code snippet replacing the 'window.addEventListener()
' calls.