views:

830

answers:

5

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?

+2  A: 

== 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.

dreftymac
First, I've already written a big greasemonkey script for adding keyboard shortcuts to everything, so I'd like bundle everything inside that greasemonkey script.The problem is that I don't want to make a element.click() as it will fire a refresh, or take the user focus outside while he's working.I'm looking for a way to make an unobtrusive ping...
I could of sworn firefox had reload every as an inbuilt feature. Must of been Opera had it out of the box.
Macha
A: 

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
David Caunt
A: 

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.

James Burgess
Indeed - a GET request to the homepage/dashboard/index of the app via AJAX would be a good idea here.
David Caunt
A: 

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.

Jesse Dearing
+4  A: 

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
Glad you found your own answer - it was what I was going to suggest.
gnarf
And what I already did suggest...
James Burgess
I've used similar scripts, usually having a /pulse as a server uri that really doesn't do much, but keep the session alive. I tend to set the timing to 1 minute, and reduce the server sessions to 3 minutes, this way I can recycle the state on the server much sooner. It also allows for a much more accurate "who's on" listing.
Tracker1
He's trying to prevent the session expiring on a third-party website, as far as I understood it...
James Burgess