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

Commit

Permalink
this fixes #252
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed May 4, 2014
1 parent ebb5548 commit 844da08
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 24 deletions.
18 changes: 0 additions & 18 deletions js/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,10 @@ function onMessageHandler(request, sender, callback) {
HTTPSB.reloadAllLocalAssets();
break;

case 'contentScriptHasLocalStorage':
response = contentScriptLocalStorageHandler(request.url);
break;

case 'contentScriptSummary':
contentScriptSummaryHandler(request, sender);
break;

case 'forceReloadTab':
HTTPSB.forceReload(request.pageURL);
break;

case 'checkScriptBlacklisted':
response = {
scriptBlacklisted: HTTPSB.blacklisted(
request.url,
'script',
HTTPSB.URI.hostnameFromURI(request.url)
)
};
break;

case 'gotoExtensionURL':
gotoExtensionURL(request.url);
break;
Expand Down
87 changes: 87 additions & 0 deletions js/contentscript-uaspoof.js
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);

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

})();
62 changes: 58 additions & 4 deletions js/contentscripthandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@

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

function contentScriptSummaryHandler(details, sender) {
(function() {

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

var contentScriptSummaryHandler = function(details, sender) {
// TODO: Investigate "Error in response to tabs.executeScript: TypeError:
// Cannot read property 'locationURL' of null" (2013-11-12). When can this
// happens?
Expand Down Expand Up @@ -66,11 +70,11 @@ function contentScriptSummaryHandler(details, sender) {

// https://github.com/gorhill/httpswitchboard/issues/181
httpsb.onPageLoadCompleted(pageURL);
}
};

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

function contentScriptLocalStorageHandler(pageURL) {
var contentScriptLocalStorageHandler = function(pageURL) {
var httpsb = HTTPSB;
var httpsburi = httpsb.URI.set(pageURL);
var response = httpsb.blacklisted(pageURL, 'cookie', httpsburi.hostname);
Expand All @@ -85,5 +89,55 @@ function contentScriptLocalStorageHandler(pageURL) {
httpsb.localStorageRemovedCounter++;
}
return response;
}
};

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

// Handling stuff asynchronously simplifies code

var onMessageHandler = function(request, sender, callback) {
if ( !request || !request.what ) {
return;
}

var response;

switch ( request.what ) {

case 'contentScriptHasLocalStorage':
response = contentScriptLocalStorageHandler(request.url);
break;

case 'contentScriptSummary':
contentScriptSummaryHandler(request, sender);
break;

case 'checkScriptBlacklisted':
response = {
scriptBlacklisted: HTTPSB.blacklisted(
request.url,
'script',
HTTPSB.URI.hostnameFromURI(request.url)
)
};
break;

case 'getUserAgentReplaceStr':
response = HTTPSB.userSettings.spoofUserAgent ? HTTPSB.userAgentReplaceStr : undefined;
break;

default:
// console.error('HTTP Switchboard > onMessage > unknown request: %o', request);
break;
}

if ( response !== undefined && callback ) {
callback(response);
}
};

chrome.runtime.onMessage.addListener(onMessageHandler);

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

})();
7 changes: 7 additions & 0 deletions js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ var onBeforeSendHeadersHandler = function(details) {

if ( httpsb.userSettings.spoofUserAgent ) {
changed = foilUserAgent(httpsb, details) || changed;
// https://github.com/gorhill/httpswitchboard/issues/252
// To avoid potential mismatch between the user agent from HTTP headers
// and the user agent from subrequests and the window.navigator object,
// I could always store here the effective user agent, but I am really
// not convinced it is worth the added overhead given the low
// probability and the benign consequence if it ever happen. Can always
// be revised if ever I become aware a mismatch is a terrible thing
}

if ( changed ) {
Expand Down
12 changes: 10 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
"background": {
"page": "background.html"
},
"content_scripts": [{
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["js/contentscript.js"],
"run_at": "document_end",
"all_frames": true
}],
},
{
"matches": ["http://*/*", "https://*/*"],
"js": ["js/contentscript-uaspoof.js"],
"run_at": "document_start",
"all_frames": true
}
],
"homepage_url": "https://github.com/gorhill/httpswitchboard/wiki",
"minimum_chrome_version": "22.0",
"default_locale": "en",
Expand Down

0 comments on commit 844da08

Please sign in to comment.