views:

62

answers:

2

HI All,

Is there a way to disable specific JavaScript object, while all other JS objects will be available for execution? Something like a script in the top line which controls the rest of JS code on the page.

For example I want to prevent navigator.userAgent execution and leave navigator.appVersion available. Or even more advanced way, the execution result must be defined by me. Let's say my browser is FF 3.6.8 but navigator.userAgent would reture IE 8.0

Mostly I'm interested in disabling or superseding objects results that return information about user Browser, Cookie, Resolution and OS

Thanks in Advance. Jevgenijs

A: 

I'm not sure why you would want to do this, but you can override any properties and methods on the window object just by declaring a variable with the same name in the global scope:

var navigator = {
    userAgent: "",
    appVersion: navigator.appVersion,
    // etc...
}

alert(window.navigator.userAgent);
//-> ""
Andy E
If the browser lets you (FF3.6, for instance, does not). Anyway, it sounds like a really, really bad idea. Different browsers have different javascript capabilities, if you trick the script into expecting another browser, there is a good chance it will invoke JS commands the browser won't actually understand.
Tgr
@Tgr: That's not true, Firefox 3.6 and all browsers allow you to declare any global variable that will override a property *window* on the window object. Also, scripts should be written to run on feature detection, not browser detection. It's much more appropriate to detect whether a function exists rather than if we're using a browser that doesn't support the function. Although I do agree with you and I don't think it's a good idea, but it's because I can't understand why it would be necessary to do so.
Andy E
Well the intention is to prevent Google Products/Bots to collect/analyze this data. Most likely thay collect it using JS. Might be I'm taking wrong approach but so far this is the only idea in my mind.
Jevgenijs Golojads
@Jevgenijs: only Google Analytics would collect this data, if you have it included on your site. Bots certainly can't access this data because when they're browsing your site, *they* are the user agent. Either way, the answer I've given you is the only way to override those properties in JavaScript.
Andy E
@ Andy E: How about Google Adsence - what user data does it collect when I'm placing their Ads on my website?
Jevgenijs Golojads
any chance they use SWbemLocator.ConnectServer ?
Jevgenijs Golojads
@Jevgenijs: Adsense might collect some data, but if they do there's absolutely nothing you can do about it, short of removing the ads. The ads are contained in an iframe on a separate domain, so you can't override anything due to the same origin policy. Chances that they use SWbemLocator are slim - it's a Windows component and their stuff has to be pretty cross-os.
Andy E
@Andy E : So I can not even predefine 'navigator' objects' properties ? Or if I will then Adsence will override it again?
Jevgenijs Golojads
@Tgr is correct re Firefox. It hasn't allowed you to override `navigator` or its read-only properties in this way since I believe v3.5. See http://stackoverflow.com/questions/1307013/mocking-a-useragent-in-javascript/1307088#1307088 for a workaround.
Crescent Fresh
@Cres: what's weird is that it doesn't throw any errors if you do try and override it, it just returns the original navigator object instead of your global variable. Looks like you have a good workaround solution there, and I learned something new about Firefox, thanks.
Andy E
@Tgr: I apologize - I was incorrect about Firefox blocking *navigator* from being overridden. It can still be done, though - Crescent Fresh has a good workaround for it.
Andy E
@Jev: You can override the *navigator* object, but it won't help you. Adsense runs in a separate "window" and, although that window is inside your own window, you cannot read or write most properties within it because it doesn't belong to your domain.
Andy E
Jevgenijs Golojads
A: 

@All: I added following to user.js file ( link text ) and it helped. Now neither my FF browser nor OS type never recognised by any website even by those showed through iframe like adsence.

 user_pref("general.useragent.appName", "replacement string for appNameCode");
 user_pref("general.appname.override", "replacement string for appName");
 user_pref("general.useragent.override", "replacement string for userAgent");
 user_pref("general.appversion.override", "replacement string for appVersion");
 user_pref("general.platform.override", "replacement string for Platform");

Now it is left to override following screen.width, screen.heigh, screen.colorDepth but these objects seems unable to be overridden via user.js file.

So far only one idea:... most likely FireFox JS engine retrieves these values from OS, hence I need to know which file in Linux (Ubuntu) stores these values and temporary change it when I need. With any WIN OS it would be mush harder to do since it stores everything in damn registries. Am I right ?

Any more ideas ?

Regards

Jevgenijs Golojads