22

I am trying to browse a website, however, it only works under Windows and Mac because they use the navigator.platform from JavaScript to find out the architecture I am running on. Of course, they also use the browser's user agent, but that was easy to spoof.

Here is the .js in question: http://pastebin.com/f56fd608d. The code responsible for browser detection is at the top. Is there any way of changing the .js file before the site runs, or something similar, so I can eliminate the check?

Using the JavaScript console yields:

>navigator.platform
Linux i686

Evidently I changed the browser's user agent, but navigator.platform does not seem to take it's value from the user agent.

Maybe someone knows how to change the value returned by navigator.platform, because I hate running Windows under VirtualBox to use this site.

EDIT: This could be of interest because Linux users might be artificially denied access to websites, and can do nothing about it.

Kevin Fegan
  • 1,292
  • 1
  • 16
  • 29
Radu
  • 338
  • 1
  • 2
  • 6
  • what browser do you use? – Rubens Farias Jan 30 '10 at 02:13
  • I use Chrome and Firefox. Both take the platform information not from the User Agent, but from somewhere else. Probably it's built-in. I would use any browser under Linux that can provide a spoofed `navigator.platform` – Radu Jan 30 '10 at 02:16
  • 1
    Radu: All browsers except IE on all platforms support `__defineGetter__`. This is what you're looking for. – Eli Grey Jan 30 '10 at 03:12
  • It's revolved here http://stackoverflow.com/questions/1307013/mocking-a-useragent-in-javascript – Iván Gajate May 07 '16 at 01:07

6 Answers6

20
var fakePlatformGetter = function () {
  return "your fake platform";
};
if (Object.defineProperty) {
  Object.defineProperty(navigator, "platform", {
    get: fakePlatformGetter
  });
  Object.defineProperty(Navigator.prototype, "platform", {
    get: fakePlatformGetter
  });
} else if (Object.prototype.__defineGetter__) {
  navigator.__defineGetter__("platform", fakePlatformGetter);
  Navigator.prototype.__defineGetter__("platform", fakePlatformGetter);
}
Eli Grey
  • 35,104
  • 14
  • 75
  • 93
17

Since you can't directly set navigator.platform, you will have to be sneaky - create an object that behaves like navigator, replace its platform, then set navigator to it.

var fake_navigator = {};

for (var i in navigator) {
  fake_navigator[i] =  navigator[i];
}

fake_navigator.platform = 'MyOS';

navigator = fake_navigator;

If you execute this code before the document loads (using GreaseMonkey, an addon or a Chrome extension), then the page will see navigator.platform as "MyOS".

Note: tested only in Chrome.

Max Shawabkeh
  • 37,799
  • 10
  • 82
  • 91
  • Yes, this is sneaky :) Thank you for the help! – Radu Jan 30 '10 at 02:30
  • 5
    Actually, you can replace the platform getter. There is no platform setter so `navigator.platform = ...` doesn't do anything but Chrome supports the non-standard `__defineGetter__` method on all objects. Pre-alphas of Firefox support ECMAScript5's `Object.defineProperty` too. – Eli Grey Jan 30 '10 at 03:07
  • I may be biased (because it's my answer) but I like defining a getter more, too. But this works too, and it's what I would have thought of if the getter approach didn't exist. – ephemient Jan 30 '10 at 06:00
  • 1
    This doesn't work anymore. The page doesn't see it. I used other method: navigator.__defineGetter__('platform', function(){ return( "Windows" ); }); but this also doesn't work. Any help please appreciated ! – Skuta Oct 29 '13 at 22:47
  • I thought I'd install older Chrome from 2010 .. but there is a slight issue where to get tampermonkey for that :) – Skuta Oct 31 '13 at 00:19
  • a list of platforms http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today, despite I cant make it work with tampermonkey, I think tampermonkey is buggy because it says no script is running, so it may work but I am not finding a way to test it. – Aquarius Power Oct 16 '14 at 22:23
  • oh, I ran syntax check and it failed for this line: `navigator = fake_navigator;` on tampermonkey; may be `navigator` is readonly? – Aquarius Power Oct 16 '14 at 22:38
6

Provided that the browser you're using supports Object.defineProperty() (it likely does), a more modern way of achieving the same goal is as follows:

Object.defineProperty(navigator, 'platform', {
  value: 'my custom value',
  configurable: true // necessary to change value more than once
});

This allows you to set it to any custom value you want, and it also allows you to change it as many times as you want without needing to reload the page.

CITguy
  • 198
  • 1
  • 2
  • 9
4

For a Mozilla-based browser, GreaseSpot / Code Snippets # Hijacking browser properties demonstrates how it may be done. This code may be injected from a GreaseMonkey script.

ephemient
  • 198,619
  • 38
  • 280
  • 391
4

about:config - > general.platform.override

-2

Attempting to change this property (at any time) in Firefox yields:

Error: setting a property that has only a getter

Source File: index.html

Line: 1

So I think you will have a hard time.

I'd try to contact the author about obtaining a fix.

Community
  • 1
  • 1
scunliffe
  • 62,582
  • 25
  • 126
  • 161