Browse Source

fix #485

pull/2/head
gorhill 7 years ago
parent
commit
8e073579f6
No known key found for this signature in database GPG Key ID: 25E1490B761470C2
  1. 124
      src/js/contentscript.js
  2. 8
      src/js/messaging.js

124
src/js/contentscript.js

@ -334,84 +334,6 @@ var collapser = (function() {
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
var hasInlineScript = function(nodeList, summary) {
var i = 0;
var node, text;
while ( (node = nodeList.item(i++)) ) {
if ( node.nodeType !== 1 ) {
continue;
}
if ( node.localName === 'script' ) {
text = node.textContent.trim();
if ( text === '' ) {
continue;
}
summary.inlineScript = true;
break;
}
if ( node.localName === 'a' && node.href.lastIndexOf('javascript', 0) === 0 ) {
summary.inlineScript = true;
break;
}
}
if ( summary.inlineScript ) {
summary.mustReport = true;
}
};
var nodeListsAddedHandler = function(nodeLists) {
var i = nodeLists.length;
if ( i === 0 ) {
return;
}
var summary = {
what: 'contentScriptSummary',
locationURL: window.location.href,
inlineScript: false,
mustReport: false
};
while ( i-- ) {
if ( summary.inlineScript === false ) {
hasInlineScript(nodeLists[i], summary);
}
collapser.addNodeList(nodeLists[i]);
}
if ( summary.mustReport ) {
vAPI.messaging.send('contentscript.js', summary);
}
collapser.process();
};
/******************************************************************************/
/******************************************************************************/
// Executed only once.
(function() {
var summary = {
what: 'contentScriptSummary',
locationURL: window.location.href,
inlineScript: false,
mustReport: true
};
// https://github.com/gorhill/httpswitchboard/issues/25
// &
// Looks for inline javascript also in at least one a[href] element.
// https://github.com/gorhill/httpswitchboard/issues/131
hasInlineScript(document.querySelectorAll('a[href^="javascript:"],script'), summary);
//console.debug('contentscript.js > firstObservationHandler(): found %d script tags in "%s"', Object.keys(summary.scriptSources).length, window.location.href);
vAPI.messaging.send('contentscript.js', summary);
collapser.addMany(document.querySelectorAll('img'));
collapser.addIFrames(document.querySelectorAll('iframe'));
collapser.process();
})();
/******************************************************************************/
/******************************************************************************/
// Observe changes in the DOM // Observe changes in the DOM
// Added node lists will be cumulated here before being processed // Added node lists will be cumulated here before being processed
@ -421,11 +343,15 @@ var nodeListsAddedHandler = function(nodeLists) {
if ( !document.body ) { return; } if ( !document.body ) { return; }
var addedNodeLists = []; var addedNodeLists = [];
var addedNodeListsTimer = null;
var addedNodeListsTimer;
var treeMutationObservedHandler = function() { var treeMutationObservedHandler = function() {
nodeListsAddedHandler(addedNodeLists);
addedNodeListsTimer = null;
addedNodeListsTimer = undefined;
var i = addedNodeLists.length;
while ( i-- ) {
collapser.addNodeList(addedNodeLists[i]);
}
collapser.process();
addedNodeLists = []; addedNodeLists = [];
}; };
@ -440,7 +366,7 @@ var nodeListsAddedHandler = function(nodeLists) {
addedNodeLists.push(nodeList); addedNodeLists.push(nodeList);
} }
} }
if ( addedNodeListsTimer === null ) {
if ( addedNodeListsTimer === undefined ) {
addedNodeListsTimer = vAPI.setTimeout(treeMutationObservedHandler, 47); addedNodeListsTimer = vAPI.setTimeout(treeMutationObservedHandler, 47);
} }
}; };
@ -453,13 +379,13 @@ var nodeListsAddedHandler = function(nodeLists) {
}); });
vAPI.shutdown.add(function() { vAPI.shutdown.add(function() {
if ( addedNodeListsTimer !== null ) {
if ( addedNodeListsTimer !== undefined ) {
clearTimeout(addedNodeListsTimer); clearTimeout(addedNodeListsTimer);
addedNodeListsTimer = null;
addedNodeListsTimer = undefined;
} }
if ( treeObserver !== null ) { if ( treeObserver !== null ) {
treeObserver.disconnect(); treeObserver.disconnect();
treeObserver = null;
treeObserver = undefined;
} }
addedNodeLists = []; addedNodeLists = [];
}); });
@ -470,6 +396,34 @@ var nodeListsAddedHandler = function(nodeLists) {
// Executed only once. // Executed only once.
// https://github.com/gorhill/httpswitchboard/issues/25
// https://github.com/gorhill/httpswitchboard/issues/131
// Looks for inline javascript also in at least one a[href] element.
// https://github.com/gorhill/uMatrix/issues/485
// Mind "on..." attributes.
(function() {
vAPI.messaging.send('contentscript.js', {
what: 'contentScriptSummary',
locationURL: window.location.href,
inlineScript:
document.querySelector('script:not([src])') !== null ||
document.querySelector('a[href^="javascript:"]') !== null ||
document.querySelector('[onabort],[onblur],[oncancel],[oncanplay],[oncanplaythrough],[onchange],[onclick],[onclose],[oncontextmenu],[oncuechange],[ondblclick],[ondrag],[ondragend],[ondragenter],[ondragexit],[ondragleave],[ondragover],[ondragstart],[ondrop],[ondurationchange],[onemptied],[onended],[onerror],[onfocus],[oninput],[oninvalid],[onkeydown],[onkeypress],[onkeyup],[onload],[onloadeddata],[onloadedmetadata],[onloadstart],[onmousedown],[onmouseenter],[onmouseleave],[onmousemove],[onmouseout],[onmouseover],[onmouseup],[onwheel],[onpause],[onplay],[onplaying],[onprogress],[onratechange],[onreset],[onresize],[onscroll],[onseeked],[onseeking],[onselect],[onshow],[onstalled],[onsubmit],[onsuspend],[ontimeupdate],[ontoggle],[onvolumechange],[onwaiting],[onafterprint],[onbeforeprint],[onbeforeunload],[onhashchange],[onlanguagechange],[onmessage],[onoffline],[ononline],[onpagehide],[onpageshow],[onrejectionhandled],[onpopstate],[onstorage],[onunhandledrejection],[onunload],[oncopy],[oncut],[onpaste]') !== null
});
collapser.addMany(document.querySelectorAll('img'));
collapser.addIFrames(document.querySelectorAll('iframe'));
collapser.process();
})();
/******************************************************************************/
/******************************************************************************/
// Executed only once.
// https://github.com/gorhill/uMatrix/issues/232 // https://github.com/gorhill/uMatrix/issues/232
// Force `display` property, Firefox is still affected by the issue. // Force `display` property, Firefox is still affected by the issue.

8
src/js/messaging.js

@ -391,9 +391,7 @@ var contentScriptSummaryHandler = function(tabId, details) {
// TODO: Investigate "Error in response to tabs.executeScript: TypeError: // TODO: Investigate "Error in response to tabs.executeScript: TypeError:
// Cannot read property 'locationURL' of null" (2013-11-12). When can this // Cannot read property 'locationURL' of null" (2013-11-12). When can this
// happens? // happens?
if ( !details || !details.locationURL ) {
return;
}
if ( !details || !details.locationURL ) { return; }
// scripts // scripts
if ( details.inlineScript !== true ) { if ( details.inlineScript !== true ) {
@ -402,9 +400,7 @@ var contentScriptSummaryHandler = function(tabId, details) {
// https://github.com/gorhill/httpswitchboard/issues/25 // https://github.com/gorhill/httpswitchboard/issues/25
var pageStore = µm.pageStoreFromTabId(tabId); var pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
return;
}
if ( pageStore === null ) { return; }
var pageHostname = pageStore.pageHostname; var pageHostname = pageStore.pageHostname;
var µmuri = µm.URI.set(details.locationURL); var µmuri = µm.URI.set(details.locationURL);

Loading…
Cancel
Save