If it's not that important, why isn't simply the referer enough? It also works "most of the time," it's just not reliable. And you wouldn't have to run scripts.
Personally, I'd just look what resources where called by which user in the last 30 (or something) seconds. Also I have to apologize, but I'm a loser in javascript.
Ordinary morality is for ordinary people. -- Aleister Crowley