Joe Mastey
Joe Mastey

Reputation: 27119

detect change to window.name

I'm trying to find a good way to detect when the window's name has changed. It is easy enough to do with setInterval like this:

var old_name = window.name;
var check_name = function() {
    if(window.name != old_name) {
        console.log("window name changed to "+window.name);
        old_name = window.name;
    }
}
setInterval(check_name, 1000);

However, setInterval is more than a little hacky and may come with a significant (1s in this case) time delay before detecting changes. Is there any kind of event or clever bit of JS that can fire a function when the window name changes?

Thanks! Joseph Mastey


EDIT:

Unfortunately, I don't have control over the code that will be changing the window name, which is in a way the entire point of the exercise. Having looked at evercookie and other posts about storing data in the window name as a way to track users, I've decided that this isn't really acceptable to me. So, I'm creating a Greasemonkey script (actually, the Chromium version of a greasemonkey script) to prevent this sort of data leakage.

As an aside, I did find some magic functionality that Chromium implements that goes a long way towards what I want. JS provides magic getters and setters that can be used like this:

window.__defineSetter__("name", function(nm) { console.log("intercepted window.name attempt: "+nm); });
window.__defineGetter__("name", function() { return ""; }); 

If I run this on the console, I can successfully hijack sets of the window name. However, running this code as a Chromium script (at least any variant I've tried so far) does not seem to be able to override the window behavior.

Upvotes: 2

Views: 2296

Answers (1)

alex
alex

Reputation: 490283

Doesn't look like there is a native event, so polling may be your only bet.

What piece of code updates window.name? You could always do...

window.name = 'new';
windowNameChanged();

Upvotes: 2

Related Questions