Spoof JS Objects

亡梦爱人 提交于 2019-12-08 02:04:28

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/6713434/spoof-js-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!