Browse Source

"fix" #771

pull/2/head
gorhill 8 years ago
parent
commit
6781a98fd6
  1. 10
      src/js/contentscript-end.js
  2. 41
      src/js/contentscript-start.js

10
src/js/contentscript-end.js

@ -359,12 +359,6 @@ var hasInlineScript = function(nodeList, summary) {
} }
if ( node.localName === 'script' ) { if ( node.localName === 'script' ) {
// https://github.com/gorhill/httpswitchboard/issues/252
// Do not count uMatrix's own script tags, they are not required
// to "unbreak" a web page
if ( typeof node.id === 'string' && ownScripts[node.id] ) {
continue;
}
text = node.textContent.trim(); text = node.textContent.trim();
if ( text === '' ) { if ( text === '' ) {
continue; continue;
@ -383,10 +377,6 @@ var hasInlineScript = function(nodeList, summary) {
} }
}; };
var ownScripts = {
'umatrix-ua-spoofer': true
};
/******************************************************************************/ /******************************************************************************/
var nodeListsAddedHandler = function(nodeLists) { var nodeListsAddedHandler = function(nodeLists) {

41
src/js/contentscript-start.js

@ -79,9 +79,12 @@ var navigatorSpoofer = " \
var pos = spoofedUserAgent.indexOf('/'); \n \ var pos = spoofedUserAgent.indexOf('/'); \n \
var appName = pos === -1 ? '' : spoofedUserAgent.slice(0, pos); \n \ var appName = pos === -1 ? '' : spoofedUserAgent.slice(0, pos); \n \
var appVersion = pos === -1 ? spoofedUserAgent : spoofedUserAgent.slice(pos + 1); \n \ var appVersion = pos === -1 ? spoofedUserAgent : spoofedUserAgent.slice(pos + 1); \n \
Object.defineProperty(navigator, 'userAgent', { get: function(){ return spoofedUserAgent; } }); \n \
Object.defineProperty(navigator, 'appName', { get: function(){ return appName; } }); \n \
Object.defineProperty(navigator, 'appVersion', { get: function(){ return appVersion; } }); \n \
Object.defineProperty(navigator, 'userAgent', { value: spoofedUserAgent }); \n \
Object.defineProperty(navigator, 'appName', { value: appName }); \n \
Object.defineProperty(navigator, 'appVersion', { value: appVersion }); \n \
var c = document.currentScript, \n \
p = c && c.parentNode; \n \
if ( p ) { p.removeChild(c); } \n \
} catch (e) { \n \ } catch (e) { \n \
} \n \ } \n \
})();"; })();";
@ -102,17 +105,35 @@ var injectNavigatorSpoofer = function(spoofedUserAgent) {
if ( spoofedUserAgent === navigator.userAgent ) { if ( spoofedUserAgent === navigator.userAgent ) {
return; return;
} }
var script = document.createElement('script');
script.type = 'text/javascript';
script.id = 'umatrix-ua-spoofer';
var js = document.createTextNode(navigatorSpoofer.replace('{{ua-json}}', JSON.stringify(spoofedUserAgent)));
script.appendChild(js);
var parent = document.head || document.documentElement,
scriptText = navigatorSpoofer.replace('{{ua-json}}', JSON.stringify(spoofedUserAgent)),
script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('id', 'umatrix-ua-spoofer');
script.appendChild(document.createTextNode(scriptText));
try {
parent.appendChild(script);
}
catch (ex) {
}
// https://github.com/gorhill/uMatrix/issues/771
var spoofer = document.querySelector('script#umatrix-ua-spoofer');
if ( spoofer !== null ) {
spoofer.parentNode.removeChild(spoofer);
script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('id', 'umatrix-ua-spoofer');
script.setAttribute('src', 'data:application/javascript;base64,' + window.btoa(scriptText));
try { try {
var parent = document.head || document.documentElement;
parent.appendChild(script); parent.appendChild(script);
} }
catch (e) {
catch (ex) {
}
spoofer = document.querySelector('script#umatrix-ua-spoofer');
if ( spoofer !== null ) {
spoofer.parentNode.removeChild(spoofer);
}
} }
// The port will never be used again at this point, disconnecting allows // The port will never be used again at this point, disconnecting allows

Loading…
Cancel
Save