tags:

views:

1934

answers:

5

Hi

What I want to do is trigger a function from an extension/GM Script once a page in FireFox reloads/refreshes...

Picture this:

  1. I goto a webpage that asks for a username and password.
  2. My code populates these values and submits the credentials
  3. The webpage reloads and brings up a page asking me to enter a pre-decided number
  4. I enter the number
  5. The webpage reloads and brings up a page asking me to select an option from a dropdown
  6. I select ... .. you get the idea..

I figured I wanted to write some JavaScript to do all this.. and since persistence would be required and I don't have capability to change the source site, I thought of writing a FireFox extension or GreaseMonkey - basically anything on the client side.

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

addEventListener("DOMContentReloaded", pageReloaded, false);

Typical test cases for such code would be to:

  1. Find out how much time it took between page refreshes
  2. Wait for the second instance of page refresh to happen and then redirect to another page ,etc ..

All this would be done from a FireFox extension (or GreaseMonkey in case a slution in GM would be easier/better/recommended) - given this, things should be easy?

A: 

I do not think there is any way of preserving you JavaScript through refreshing the page in browser - it then loads it all again...

Maybe you can start with window.onbeforeunload to overload default function of reload button and use AJAX to reload some main div...

rkj
Correct.. but that's why I added the text in parens in the first line!The persistence is achieved because all this is happening through a FireFox extension which is always alive..Basically I am asking if it's possible for an extension to find out/notified if and when a page reloads in FireFox ..
PoorLuzer
A: 

Assigning window.onbeforeunload would be a workable solution had it not fired when the page is closed too!

Any more suggestions .. maybe FF Extension or GM Script specific?

PoorLuzer
A: 

Use window.name or document.cookie - both properties will survive a refresh of the page.

Christoph
+4  A: 
Dan Herbert
+1 Nice answer - I haven't tested the Javascript but "PoorLuzer" (the questioner) really has to appreciate the work you put into this one.
Mark Brittingham
I tested it in firefox and it worked in my test page.
Dan Herbert
you can't rely on beforeunload though, its often not even supported by browsers.
Kent Fredric
If using jquery (or another javascript API) then you can ensure that unload occurs.
Dan Herbert
Wow Dan,The code to keep track of hitcount was extremely cool !Yet this was one of the features of what I wanted to do - not the goal per-se... and it assumes I wrote the HTML of the site where all this takes place.. about your jQuesry suggestion.. could you point me somewhere to start?
PoorLuzer
It's really beautiful the way you redefined a "reload" problem to a "load" solution - the end result is the same, albeit requiring cookies in the latter.But is there no way to have some "OnReload" functionality? Maybe even using a JS Library?
PoorLuzer
Probably the only "OnReload" functionality you'll be able to get is what I've defined in my answer. I don't think there's anything out there for reloads since the general philosophy of the web is to be "stateless' and therefore ignorant of refreshes.
Dan Herbert
I finally got round to implementing a solution for this problem and your idea helped with only a few code changes here and there. If I find time, I will post my solution.
PoorLuzer
A: 

919756 If you succeed, TC staff will catch and ban you. Wonder how I knew? hmmmmm...