I'm writing a greasemonkey script to keep session open on a webapp I use for work. Which javascript command would you use to create some feedback with the server and ensure the session doesn't fall without having to bother the user making a complete refresh of the page?
== Second choice ==
If you absolutely insist on using greasemonkey, any element.click() method on any event that submits an XMLHTTPrequest should do.
== First choice==
If you are willing to use a solution that does not require you to write a greasemonkey script:
ReloadEvery 3.0.0 by Jaap Haitsma
https://addons.mozilla.org/en-US/firefox/addon/115
Reloads web pages every so many seconds or minutes. The function is accessible via the context menu (menu you get when you right click on a web page) or via a drop down menu on the reload button ...
It's not what you asked for, but unless you simply want to brush up on your javascript skills, this is probably the most straight-forward method for solving your problem.
I can definitely recommend the solution suggested by dreftymac!
If you don't want to worry about remembering to click the reloadevery option, your grease monkey script is simply
window.location.href = window.location.href
This is presuming you don't want to keep the page constantly refreshing, as suggested by others.
Ordinarily, if you were simply looking to ping the server, you would probably do best to do something like use an image that you know exists on the remote server, and check it's height - thus creating traffic.
However, your problem (I presume) needs to access some limited area of the website, in order to keep the session active... with this being the case (and the fact that you can't directly ping), find a page that is 'restricted' and do an Ajax call, or similar. Don't quote me on this, as I'm by no means a JavaScript expert... just throwing out a potential solution.
If you want to write JavaScript to solve this (the other answers are a lot easier and I would recommend going the easier route), here's what I would do:
- Call document.createElement("iframe") to create an iframe
- Size the iframe to 0x0, style.display = "none", set the ID to something, and set the src property to a page on the site that will extend your session
- Use document.body.appendChild to add the iframe to the page
- Using setTimeout refresh the iFrame using window.frames['iframeID'].location.reload(true); and also call setTimeout to refresh the page again.
See if the other answers would work because using JavaScript seems like it would be more trouble than it's worth.
Thanks all! I've solved the issue using:
function keep_alive() {
http_request = new XMLHttpRequest();
http_request.open('GET', "/restricted_file_url");
http_request.send(null);
};
setInterval(keep_alive,840000); //My session expires at 15 minutes