2

I've been wondering if there is a way to spoof JS objects like navigator, screen, etc. to send fake data whenever a page request it.

I use C# to develop an application which contains a GeckoFx browser and I want to do something like this: every time I open a webpage which contains JS to retrieve information about the user (for example screen resolution, what plugins I have installed, etc), the browser should send fake information (my screen resolution is 1024x768 and I want to send 1440x900, etc).

Can anyone help me with this piece of information?

Thanks!

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
Ionut Ungureanu
  • 380
  • 3
  • 9
  • 25

1 Answers1

5

It is possible to spoof JS objects by replacing them. You will obviously have to be very careful that you don't mess up functionality that is required for desired operation. Anyway, here's an example of how the screen object can be replaced to report any resolution you want.

In action in a jsFiddle: http://jsfiddle.net/jfriend00/bfAYe/

var oldScreen = screen;  // save old screen object just in case

var myScreen = {};       // create new screen object

// prefill with all properties of old object
for (var i in screen) {
    myScreen[i] = screen[i];
}
screen = myScreen;    // replace existing object with mine
screen.width = 1440;  // change properites on mine
screen.height = 900;

// verify that changed properties are in place
$("#container").html("width="+screen.width+", height="+screen.height);

// outputs width=1440, height=900

Seems to work in Opera and Chrome, but not in IE9, FF5 or Safari. I guess you can't do this across browser.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • What browser are you running the fiddle in? – jfriend00 Jul 16 '11 at 00:02
  • Guess you can't do it across all browsers. Works in Chrome and Opera, not in others. – jfriend00 Jul 16 '11 at 00:47
  • Ok, thanks! But my question is still standing. I really need this especially for Mozilla. – Ionut Ungureanu Jul 16 '11 at 00:50
  • If you're trying to change what the underlying browser reports for screen resolution, that comes from the screen object and we've just shown that Mozilla doesn't let you change it. If you want to expand your question a bit to explain what you're trying to do at a higher level, then perhaps folks could come up with other ideas. – jfriend00 Jul 16 '11 at 00:53
  • In few words... at a higher level I want to implement kind of browser application that let you to send fake information. I don't want the webmaster to know what CPU do I have, what is my screen resolution and so on. Every piece of information that somebody wants to retrieve through JS I want to be fake. – Ionut Ungureanu Jul 16 '11 at 01:00
  • Please update your question with this new information. This sends the question in a whole different direction. You need a browser plug-in to do this kind of thing. – jfriend00 Jul 16 '11 at 01:04
  • But I am developing a C# application with a webbrowser control based on XULRunner. There is no need for browser plug-ins. – Ionut Ungureanu Jul 16 '11 at 01:36
  • Then, you'll have to look at the internals of XULRunner to see what capabilities it has. Sorry, not something I can help with. I misunderstood what you were trying to do from the beginning. – jfriend00 Jul 16 '11 at 02:18