This repository has been archived by the owner on Nov 15, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
162 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/******************************************************************************* | ||
httpswitchboard - a Chromium browser extension to black/white list requests. | ||
Copyright (C) 2013 Raymond Hill | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see {http://www.gnu.org/licenses/}. | ||
Home: https://github.com/gorhill/httpswitchboard | ||
*/ | ||
|
||
/******************************************************************************/ | ||
|
||
// https://github.com/gorhill/httpswitchboard/issues/252 | ||
|
||
(function() { | ||
|
||
/******************************************************************************/ | ||
|
||
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]; \ | ||
} \ | ||
} \ | ||
spoofedNavigator.appVersion = spoofedUserAgent; \ | ||
spoofedNavigator.userAgent = spoofedUserAgent; \ | ||
Object.freeze(spoofedNavigator); \ | ||
/*Object.setPrototypeOf(spoofedNavigator, Object.getPrototypeOf(realNavigator));*/ \ | ||
navigator = window.navigator = spoofedNavigator; \ | ||
})();\ | ||
'; | ||
|
||
/******************************************************************************/ | ||
|
||
// Because window.userAgent is read-only, we need to create a fake Navigator | ||
// object to contain out fake user-agent string. | ||
// Because objects created by a content script are local to the content script | ||
// and not visible to the web page itself (and vice versa), we need the context | ||
// of the web page to create the fake Navigator object directly, and the only | ||
// way to do this is to inject appropriate javascript code into the web page. | ||
|
||
var injectNavigatorSpoofer = function(spoofedUserAgent) { | ||
if ( typeof spoofedUserAgent !== 'string' ) { | ||
return; | ||
} | ||
if ( spoofedUserAgent === navigator.userAgent ) { | ||
return; | ||
} | ||
var script = document.createElement('script'); | ||
script.type = 'text/javascript'; | ||
var js = document.createTextNode(navigatorSpoofer.replace('{{ua}}', spoofedUserAgent)); | ||
script.appendChild(js); | ||
document.documentElement.appendChild(script, document.documentElement.firstChild); | ||
}; | ||
|
||
chrome.runtime.sendMessage({ what: 'getUserAgentReplaceStr' }, injectNavigatorSpoofer); | ||
|
||
/******************************************************************************/ | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters