Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
further foolproofing injected ua spoof code: we dont want to break co…
Browse files Browse the repository at this point in the history
…de on web pages
  • Loading branch information
gorhill committed May 4, 2014
1 parent 13f30f2 commit ccba5d9
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions js/contentscript-uaspoof.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,36 @@

/******************************************************************************/

var navigatorSpoofer = ' \
(function() { \
var spoofedUserAgent = "{{ua}}"; \
if ( spoofedUserAgent === navigator.userAgent ) { \
return; \
} \
var realNavigator = navigator; \
var SpoofedNavigator = function(ua) { \
this.navigator = navigator; \
}; \
var spoofedNavigator = new SpoofedNavigator(spoofedUserAgent); \
var makeFunction = function(n, k) { \
n[k] = function() { \
return this.navigator[k].apply(this.navigator, arguments); }; \
}; \
for ( var k in realNavigator ) { \
if ( typeof realNavigator[k] === "function" ) { \
makeFunction(spoofedNavigator, k); \
} else { \
spoofedNavigator[k] = realNavigator[k]; \
var navigatorSpoofer = " \
;(function() { \
try { \
var spoofedUserAgent = '{{ua}}'; \
if ( spoofedUserAgent === navigator.userAgent ) { \
return; \
} \
var realNavigator = navigator; \
var SpoofedNavigator = function(ua) { \
this.navigator = navigator; \
}; \
var spoofedNavigator = new SpoofedNavigator(spoofedUserAgent); \
var makeFunction = function(n, k) { \
n[k] = function() { \
return this.navigator[k].apply(this.navigator, arguments); }; \
}; \
for ( var k in realNavigator ) { \
if ( typeof realNavigator[k] === 'function' ) { \
makeFunction(spoofedNavigator, k); \
} else { \
spoofedNavigator[k] = realNavigator[k]; \
} \
} \
spoofedNavigator.appVersion = spoofedUserAgent; \
spoofedNavigator.userAgent = spoofedUserAgent; \
Object.freeze(spoofedNavigator); \
navigator = window.navigator = spoofedNavigator; \
} catch (e) { \
} \
spoofedNavigator.appVersion = spoofedUserAgent; \
spoofedNavigator.userAgent = spoofedUserAgent; \
Object.freeze(spoofedNavigator); \
navigator = window.navigator = spoofedNavigator; \
})();\
';
})();";

/******************************************************************************/

Expand All @@ -74,7 +76,7 @@ var injectNavigatorSpoofer = function(spoofedUserAgent) {
}
var script = document.createElement('script');
script.type = 'text/javascript';
var js = document.createTextNode(navigatorSpoofer.replace('{{ua}}', spoofedUserAgent));
var js = document.createTextNode(navigatorSpoofer.replace('{{ua}}', spoofedUserAgent.replace(/'/g, '')));
script.appendChild(js);
document.documentElement.appendChild(script, document.documentElement.firstChild);
};
Expand Down

0 comments on commit ccba5d9

Please sign in to comment.