1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-09 12:22:33 +01:00
uBlock/js/contentscript-end.js

533 lines
17 KiB
JavaScript
Raw Normal View History

2014-06-24 00:42:43 +02:00
/*******************************************************************************
µBlock - a Chromium browser extension to block requests.
Copyright (C) 2014 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/uBlock
*/
/* global chrome */
// Injected into content pages
/******************************************************************************/
/******************************************************************************/
2014-07-02 18:02:29 +02:00
(function() {
/******************************************************************************/
2014-06-24 00:42:43 +02:00
// https://github.com/gorhill/httpswitchboard/issues/345
var messaging = (function(name){
var port = null;
var dangling = false;
var requestId = 1;
var requestIdToCallbackMap = {};
var listenCallback = null;
var onPortMessage = function(details) {
if ( typeof details.id !== 'number' ) {
return;
}
// Announcement?
if ( details.id < 0 ) {
if ( listenCallback ) {
listenCallback(details.msg);
}
return;
}
var callback = requestIdToCallbackMap[details.id];
if ( !callback ) {
return;
}
callback(details.msg);
delete requestIdToCallbackMap[details.id];
checkDisconnect();
};
var start = function(name) {
port = chrome.runtime.connect({
name: name +
'/' +
String.fromCharCode(
Math.random() * 0x7FFF | 0,
Math.random() * 0x7FFF | 0,
Math.random() * 0x7FFF | 0,
Math.random() * 0x7FFF | 0
)
});
port.onMessage.addListener(onPortMessage);
};
if ( typeof name === 'string' && name.length > 0 ) {
start(name);
}
var stop = function() {
listenCallback = null;
dangling = true;
checkDisconnect();
};
var ask = function(msg, callback) {
if ( !callback ) {
tell(msg);
return;
}
var id = requestId++;
port.postMessage({ id: id, msg: msg });
requestIdToCallbackMap[id] = callback;
};
var tell = function(msg) {
port.postMessage({ id: 0, msg: msg });
};
var listen = function(callback) {
listenCallback = callback;
};
var checkDisconnect = function() {
if ( !dangling ) {
return;
}
if ( Object.keys(requestIdToCallbackMap).length ) {
return;
}
port.disconnect();
port = null;
};
return {
start: start,
stop: stop,
ask: ask,
tell: tell,
listen: listen
};
})('contentscript-end.js');
/******************************************************************************/
/******************************************************************************/
// ABP cosmetic filters
2014-07-02 18:02:29 +02:00
var cosmeticFiltering = (function() {
var queriedSelectors = {};
var injectedSelectors = {};
var classSelectors = null;
var idSelectors = null;
var domLoaded = function() {
// https://github.com/gorhill/uBlock/issues/14
// Treat any existing domain-specific exception selectors as if they had
// been injected already.
var style = document.getElementById('uBlock1ae7a5f130fc79b4fdb8a4272d9426b5');
var exceptions = style && style.getAttribute('uBlock1ae7a5f130fc79b4fdb8a4272d9426b5');
if ( exceptions ) {
exceptions = decodeURIComponent(exceptions).split('\n');
var i = exceptions.length;
while ( i-- ) {
injectedSelectors[exceptions[i]] = true;
}
2014-06-26 00:44:35 +02:00
}
2014-07-02 18:02:29 +02:00
// TODO: evaluate merging into a single loop
selectorsFromNodeList(document.querySelectorAll('*[class],*[id]'));
retrieveGenericSelectors();
};
2014-06-24 00:42:43 +02:00
2014-07-02 18:02:29 +02:00
var retrieveGenericSelectors = function() {
var selectors = classSelectors !== null ? Object.keys(classSelectors) : [];
if ( idSelectors !== null ) {
selectors = selectors.concat(idSelectors);
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
if ( selectors.length > 0 ) {
//console.log('µBlock> ABP cosmetic filters: retrieving CSS rules using %d selectors', selectors.length);
messaging.ask({
what: 'retrieveGenericCosmeticSelectors',
pageURL: window.location.href,
selectors: selectors
},
retrieveHandler
);
}
idSelectors = null;
classSelectors = null;
};
2014-06-24 00:42:43 +02:00
2014-07-02 18:02:29 +02:00
var retrieveHandler = function(selectors) {
if ( !selectors ) {
return;
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
var styleText = [];
2014-07-04 22:47:34 +02:00
filterLowGenerics(selectors, 'hide');
filterHighGenerics(selectors, 'hide');
2014-07-02 18:02:29 +02:00
reduce(selectors.hide, injectedSelectors);
if ( selectors.hide.length ) {
var hideStyleText = '{{hideSelectors}} {display:none !important;}'
.replace('{{hideSelectors}}', selectors.hide.join(','));
styleText.push(hideStyleText);
applyCSS(selectors.hide, 'display', 'none');
//console.debug('µBlock> generic cosmetic filters: injecting %d CSS rules:', selectors.hide.length, hideStyleText);
2014-06-24 00:42:43 +02:00
}
2014-07-04 22:47:34 +02:00
filterLowGenerics(selectors, 'donthide');
filterHighGenerics(selectors, 'donthide');
2014-07-02 18:02:29 +02:00
reduce(selectors.donthide, injectedSelectors);
if ( selectors.donthide.length ) {
var dontHideStyleText = '{{donthideSelectors}} {display:initial !important;}'
.replace('{{donthideSelectors}}', selectors.donthide.join(','));
styleText.push(dontHideStyleText);
applyCSS(selectors.donthide, 'display', 'initial');
//console.debug('µBlock> generic cosmetic filters: injecting %d CSS rules:', selectors.donthide.length, dontHideStyleText);
}
if ( styleText.length > 0 ) {
var style = document.createElement('style');
style.appendChild(document.createTextNode(styleText.join('\n')));
var parent = document.body || document.documentElement;
if ( parent ) {
parent.appendChild(style);
2014-06-24 00:42:43 +02:00
}
}
2014-07-02 18:02:29 +02:00
};
2014-06-24 00:42:43 +02:00
2014-07-02 18:02:29 +02:00
var applyCSS = function(selectors, prop, value) {
if ( document.body === null ) {
return;
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
var elems = document.querySelectorAll(selectors);
var i = elems.length;
while ( i-- ) {
elems[i].style[prop] = value;
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
};
2014-07-04 22:47:34 +02:00
var filterTitleGeneric = function(generics, root, out) {
if ( !root.title.length ) {
return;
}
var selector = '[title="' + root.title + '"]';
if ( generics[selector] && !injectedSelectors[selector] ) {
out.push(selector);
}
selector = root.tagName + selector;
if ( generics[selector] && !injectedSelectors[selector] ) {
out.push(selector);
}
};
var filterAltGeneric = function(generics, root, out) {
var alt = root.getAttribute('alt');
if ( !alt || !alt.length ) {
return;
}
var selector = '[alt="' + root.title + '"]';
if ( generics[selector] && !injectedSelectors[selector] ) {
out.push(selector);
}
selector = root.tagName + selector;
if ( generics[selector] && !injectedSelectors[selector] ) {
out.push(selector);
}
};
var filterLowGenerics = function(selectors, what) {
if ( selectors[what + 'LowGenericCount'] === 0 ) {
return;
}
var out = selectors[what];
var generics = selectors[what + 'LowGenerics'];
var nodeList, iNode;
// Low generics: ["title"]
nodeList = document.querySelectorAll('[title]');
iNode = nodeList.length;
while ( iNode-- ) {
filterTitleGeneric(generics, nodeList[iNode], out);
}
// Low generics: ["alt"]
nodeList = document.querySelectorAll('[alt]');
iNode = nodeList.length;
while ( iNode-- ) {
filterAltGeneric(generics, nodeList[iNode], out);
}
};
var filterHighGenerics = function(selectors, what) {
var out = selectors[what];
var generics = selectors[what + 'HighGenerics'];
var iGeneric = generics.length;
2014-07-02 18:02:29 +02:00
var selector;
2014-07-04 22:47:34 +02:00
while ( iGeneric-- ) {
selector = generics[iGeneric];
2014-07-02 18:02:29 +02:00
if ( injectedSelectors[selector] ) {
2014-06-24 00:42:43 +02:00
continue;
}
2014-07-02 18:02:29 +02:00
if ( document.querySelector(selector) !== null ) {
2014-07-04 22:47:34 +02:00
out.push(selector);
2014-07-02 18:02:29 +02:00
}
}
};
var reduce = function(selectors, dict) {
var i = selectors.length, selector, end;
while ( i-- ) {
selector = selectors[i];
if ( !dict[selector] ) {
if ( end !== undefined ) {
selectors.splice(i+1, end-i);
end = undefined;
}
dict[selector] = true;
} else if ( end === undefined ) {
end = i;
}
}
if ( end !== undefined ) {
selectors.splice(0, end+1);
}
};
2014-07-09 07:53:49 +02:00
// TODO: split back into to specialized functions as it was before. Too
// many code paths creeped back, I thought I could get rid of most.
2014-07-02 18:02:29 +02:00
var selectorsFromNodeList = function(nodes) {
if ( !nodes || !nodes.length ) {
return;
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
if ( idSelectors === null ) {
idSelectors = [];
}
if ( classSelectors === null ) {
classSelectors = {};
}
var qq = queriedSelectors;
var cc = classSelectors;
var ii = idSelectors;
var node, v, classNames, j;
var i = nodes.length;
while ( i-- ) {
node = nodes[i];
if ( node.nodeType !== 1 ) {
2014-06-24 00:42:43 +02:00
continue;
}
2014-07-02 18:02:29 +02:00
// id
2014-07-09 07:53:49 +02:00
v = nodes[i].id;
// quite unlikely, so no need to be fancy
if ( typeof v !== 'string' ) {
v = '';
}
v = v.trim();
2014-07-02 18:02:29 +02:00
if ( v !== '' ) {
v = '#' + v;
if ( !qq[v] ) {
ii.push(v);
qq[v] = true;
}
}
// class
v = nodes[i].className;
// it could be an SVGAnimatedString...
if ( typeof v !== 'string' ) { continue; }
v = v.trim();
if ( v === '' ) { continue; }
// one class
if ( v.indexOf(' ') < 0 ) {
v = '.' + v;
if ( qq[v] ) { continue; }
cc[v] = true;
qq[v] = true;
2014-06-24 00:42:43 +02:00
continue;
}
2014-07-02 18:02:29 +02:00
// many classes
classNames = v.trim().split(/\s+/);
j = classNames.length;
while ( j-- ) {
v = classNames[j];
if ( v === '' ) { continue; }
v = '.' + v;
if ( qq[v] ) { continue; }
cc[v] = true;
qq[v] = true;
}
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
};
2014-06-24 00:42:43 +02:00
2014-07-02 18:02:29 +02:00
var processNodeLists = function(nodeLists) {
var i = nodeLists.length;
var nodeList, j, node;
while ( i-- ) {
nodeList = nodeLists[i];
selectorsFromNodeList(nodeList);
j = nodeList.length;
while ( j-- ) {
node = nodeList[j];
if ( node.querySelectorAll ) {
selectorsFromNodeList(node.querySelectorAll('*[id],*[class]'));
}
}
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
retrieveGenericSelectors();
};
2014-06-24 00:42:43 +02:00
2014-07-02 18:02:29 +02:00
domLoaded();
2014-06-24 00:42:43 +02:00
2014-07-02 18:02:29 +02:00
return {
processNodeLists: processNodeLists
};
})();
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2014-06-27 23:06:42 +02:00
// https://github.com/gorhill/uBlock/issues/7
2014-07-02 18:02:29 +02:00
var blockedElementHider = (function() {
var hideOne = function(elem, collapse) {
// If `!important` is not there, going back using history will likely
// cause the hidden element to re-appear.
elem.style.visibility = 'hidden !important';
if ( collapse && elem.parentNode ) {
elem.parentNode.removeChild(elem);
}
2014-06-29 18:38:18 +02:00
};
2014-07-02 18:02:29 +02:00
var observeOne = function(elem) {
var onComplete = function() {
var elem = this;
var onAnswerReceived = function(details) {
if ( details.blocked ) {
hideOne(elem, details.collapse);
}
};
messaging.ask({ what: 'blockedRequest', url: this.src }, onAnswerReceived);
this.removeEventListener('load', onComplete);
};
elem.addEventListener('load', onComplete);
};
2014-06-29 18:38:18 +02:00
2014-07-02 18:02:29 +02:00
var hideMany = function(elems, details) {
var blockedRequests = details.blockedRequests;
var collapse = details.collapse;
var i = elems.length;
var elem, src;
while ( i-- ) {
elem = elems[i];
src = elem.src;
if ( typeof src !== 'string' ) {
continue;
}
if ( src === '' ) {
observeOne(elem);
} else if ( blockedRequests[src] ) {
hideOne(elem, collapse);
}
2014-06-27 23:06:42 +02:00
}
2014-07-02 18:02:29 +02:00
};
2014-06-27 23:06:42 +02:00
2014-07-02 18:02:29 +02:00
var processElements = function(elems) {
var blockedRequestsReceived = function(details) {
hideMany(elems, details);
var i = elems.length;
while ( i-- ) {
hideMany(elems[i].querySelectorAll('img,iframe'), details);
}
};
messaging.ask({ what: 'blockedRequests' }, blockedRequestsReceived);
};
// rhill 2014-07-01: Avoid useless work: only nodes which are element are
// of interest at this point -- because it is common that a lot of plain
// text nodes get added.
var addNodeLists = function(nodeLists) {
var elems = [];
var i = nodeLists.length;
var nodeList, j, node;
2014-06-27 23:06:42 +02:00
while ( i-- ) {
2014-07-02 18:02:29 +02:00
nodeList = nodeLists[i];
j = nodeList.length;
while ( j-- ) {
node = nodeList[j];
if ( node.querySelectorAll ) {
elems.push(node);
}
2014-06-27 23:06:42 +02:00
}
}
2014-07-02 18:02:29 +02:00
if ( elems.length ) {
processElements(elems);
}
2014-06-27 23:06:42 +02:00
};
var onBlockedRequestsReceived = function(details) {
2014-07-02 18:02:29 +02:00
hideMany(document.querySelectorAll('img,iframe'), details);
2014-06-27 23:06:42 +02:00
};
messaging.ask({ what: 'blockedRequests' }, onBlockedRequestsReceived);
2014-06-24 00:42:43 +02:00
2014-07-02 18:02:29 +02:00
return {
addNodeLists: addNodeLists
};
})();
2014-06-24 00:42:43 +02:00
/******************************************************************************/
// rhill 2013-11-09: Weird... This code is executed from µBlock
// context first time extension is launched. Avoid this.
// TODO: Investigate if this was a fluke or if it can really happen.
// I suspect this could only happen when I was using chrome.tabs.executeScript(),
// because now a delarative content script is used, along with "http{s}" URL
// pattern matching.
// console.debug('µBlock> window.location.href = "%s"', window.location.href);
if ( /^https?:\/\/./.test(window.location.href) === false ) {
console.debug("Huh?");
return;
}
2014-06-27 23:06:42 +02:00
/******************************************************************************/
2014-06-24 00:42:43 +02:00
// Observe changes in the DOM
2014-07-02 18:02:29 +02:00
var mutationObservedHandler = function(mutations) {
var iMutation = mutations.length;
var nodeLists = [], nodeList;
while ( iMutation-- ) {
nodeList = mutations[iMutation].addedNodes;
if ( nodeList && nodeList.length ) {
nodeLists.push(nodeList);
}
}
if ( nodeLists.length ) {
cosmeticFiltering.processNodeLists(nodeLists);
blockedElementHider.addNodeLists(nodeLists);
}
};
2014-06-24 00:42:43 +02:00
// This fixes http://acid3.acidtests.org/
if ( document.body ) {
// https://github.com/gorhill/httpswitchboard/issues/176
var observer = new MutationObserver(mutationObservedHandler);
observer.observe(document.body, {
attributes: false,
childList: true,
characterData: false,
subtree: true
});
}
/******************************************************************************/
})();