2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-03-07 19:20:18 +01:00
|
|
|
µBlock - a browser extension to block requests.
|
2014-06-24 00:42:43 +02:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* jshint multistr: true */
|
2014-11-16 20:06:29 +01:00
|
|
|
|
2014-10-19 13:11:27 +02:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
// Injected into content pages
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
2014-11-26 20:26:33 +01:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-15 19:15:11 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-08 01:10:03 +02:00
|
|
|
// This can happen
|
2015-05-16 19:40:56 +02:00
|
|
|
if ( typeof vAPI !== 'object' ) {
|
2015-01-23 17:32:49 +01:00
|
|
|
//console.debug('contentscript-start.js > vAPI not found');
|
2014-10-17 21:44:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-08-28 17:08:51 +02:00
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/456
|
2015-01-02 02:58:19 +01:00
|
|
|
// Already injected?
|
2015-01-02 03:14:53 +01:00
|
|
|
if ( vAPI.contentscriptStartInjected ) {
|
2015-01-23 17:32:49 +01:00
|
|
|
//console.debug('contentscript-start.js > content script already injected');
|
2015-01-02 02:58:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-01-02 03:14:53 +01:00
|
|
|
vAPI.contentscriptStartInjected = true;
|
2015-04-05 18:03:14 +02:00
|
|
|
vAPI.styles = vAPI.styles || [];
|
2015-01-02 02:58:19 +01:00
|
|
|
|
2014-11-15 19:15:11 +01:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2014-11-15 19:15:11 +01:00
|
|
|
var localMessager = vAPI.messaging.channel('contentscript-start.js');
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Domain-based ABP cosmetic filters.
|
|
|
|
// These can be inserted before the DOM is loaded.
|
|
|
|
|
2014-08-14 19:59:37 +02:00
|
|
|
var cosmeticFilters = function(details) {
|
2014-11-26 20:26:33 +01:00
|
|
|
var donthideCosmeticFilters = {};
|
|
|
|
var hideCosmeticFilters = {};
|
2014-08-14 19:59:37 +02:00
|
|
|
var donthide = details.cosmeticDonthide;
|
|
|
|
var hide = details.cosmeticHide;
|
2015-02-14 18:16:36 +01:00
|
|
|
var i;
|
2014-08-07 22:12:15 +02:00
|
|
|
if ( donthide.length !== 0 ) {
|
2015-02-14 18:16:36 +01:00
|
|
|
i = donthide.length;
|
2014-11-16 20:06:29 +01:00
|
|
|
while ( i-- ) {
|
2014-11-26 20:26:33 +01:00
|
|
|
donthideCosmeticFilters[donthide[i]] = true;
|
2014-11-16 20:06:29 +01:00
|
|
|
}
|
2015-02-14 18:16:36 +01:00
|
|
|
}
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/143
|
2015-02-14 18:16:36 +01:00
|
|
|
if ( hide.length !== 0 ) {
|
|
|
|
i = hide.length;
|
|
|
|
var selector;
|
|
|
|
while ( i-- ) {
|
|
|
|
selector = hide[i];
|
|
|
|
if ( donthideCosmeticFilters[selector] ) {
|
|
|
|
hide.splice(i, 1);
|
2015-09-27 16:13:31 +02:00
|
|
|
} else {
|
|
|
|
hideCosmeticFilters[selector] = true;
|
2014-08-07 22:12:15 +02:00
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-07 22:12:15 +02:00
|
|
|
if ( hide.length !== 0 ) {
|
2015-12-06 00:02:04 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1015
|
|
|
|
// Boost specificity of our CSS rules.
|
|
|
|
var styleText = ':root ' + hide.join(',\n:root ');
|
2015-10-31 05:55:10 +01:00
|
|
|
var style = document.createElement('style');
|
2015-12-08 20:06:33 +01:00
|
|
|
style.setAttribute('type', 'text/css');
|
2014-10-17 21:44:19 +02:00
|
|
|
// The linefeed before the style block is very important: do not remove!
|
2015-12-06 00:02:04 +01:00
|
|
|
style.appendChild(document.createTextNode(styleText + '\n{display:none !important;}'));
|
2014-08-14 19:59:37 +02:00
|
|
|
//console.debug('µBlock> "%s" cosmetic filters: injecting %d CSS rules:', details.domain, details.hide.length, hideStyleText);
|
2015-02-14 18:27:33 +01:00
|
|
|
var parent = document.head || document.documentElement;
|
|
|
|
if ( parent ) {
|
|
|
|
parent.appendChild(style);
|
2015-04-05 18:03:14 +02:00
|
|
|
vAPI.styles.push(style);
|
2015-02-14 18:27:33 +01:00
|
|
|
}
|
2015-12-06 00:02:04 +01:00
|
|
|
hideElements(styleText);
|
2014-08-07 22:12:15 +02:00
|
|
|
}
|
2014-11-26 20:26:33 +01:00
|
|
|
vAPI.donthideCosmeticFilters = donthideCosmeticFilters;
|
|
|
|
vAPI.hideCosmeticFilters = hideCosmeticFilters;
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2015-12-22 22:32:09 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-14 19:59:37 +02:00
|
|
|
var netFilters = function(details) {
|
|
|
|
var parent = document.head || document.documentElement;
|
|
|
|
if ( !parent ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var style = document.createElement('style');
|
|
|
|
var text = details.netHide.join(',\n');
|
|
|
|
var css = details.netCollapse ?
|
|
|
|
'\n{display:none !important;}' :
|
|
|
|
'\n{visibility:hidden !important;}';
|
|
|
|
style.appendChild(document.createTextNode(text + css));
|
|
|
|
parent.appendChild(style);
|
2014-08-17 22:07:42 +02:00
|
|
|
//console.debug('document.querySelectorAll("%s") = %o', text, document.querySelectorAll(text));
|
2014-08-14 19:59:37 +02:00
|
|
|
};
|
|
|
|
|
2015-12-22 22:32:09 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Create script tags and assign data URIs looked up from our library of
|
|
|
|
// redirection resources: Sometimes it is useful to use these resources as
|
|
|
|
// standalone scriptlets.
|
|
|
|
// Library of redirection resources:
|
2015-12-23 13:23:09 +01:00
|
|
|
// https://github.com/gorhill/uBlock/blob/master/assets/ublock/resources.txt
|
2015-12-22 22:32:09 +01:00
|
|
|
|
2015-12-23 17:02:36 +01:00
|
|
|
var injectScripts = function(scripts) {
|
2015-12-22 22:32:09 +01:00
|
|
|
var parent = document.head || document.documentElement;
|
|
|
|
if ( !parent ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-23 17:02:36 +01:00
|
|
|
var i = scripts.length, scriptTag;
|
2015-12-22 22:32:09 +01:00
|
|
|
while ( i-- ) {
|
|
|
|
scriptTag = document.createElement('script');
|
2015-12-23 17:02:36 +01:00
|
|
|
scriptTag.appendChild(document.createTextNode(scripts[i]));
|
2015-12-22 22:32:09 +01:00
|
|
|
parent.appendChild(scriptTag);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-14 19:59:37 +02:00
|
|
|
var filteringHandler = function(details) {
|
2015-04-25 13:28:35 +02:00
|
|
|
var styleTagCount = vAPI.styles.length;
|
|
|
|
|
2014-09-16 21:39:41 +02:00
|
|
|
if ( details ) {
|
2015-12-13 06:33:53 +01:00
|
|
|
if (
|
|
|
|
(vAPI.skipCosmeticFiltering = details.skipCosmeticFiltering) !== true &&
|
|
|
|
(details.cosmeticHide.length !== 0 || details.cosmeticDonthide.length !== 0)
|
|
|
|
) {
|
2014-09-16 21:39:41 +02:00
|
|
|
cosmeticFilters(details);
|
|
|
|
}
|
|
|
|
if ( details.netHide.length !== 0 ) {
|
|
|
|
netFilters(details);
|
|
|
|
}
|
2015-12-23 17:02:36 +01:00
|
|
|
if ( Array.isArray(details.scripts) ) {
|
2015-12-22 22:32:09 +01:00
|
|
|
injectScripts(details.scripts);
|
|
|
|
}
|
2014-09-16 21:39:41 +02:00
|
|
|
// The port will never be used again at this point, disconnecting allows
|
|
|
|
// the browser to flush this script from memory.
|
2014-08-14 19:59:37 +02:00
|
|
|
}
|
2015-01-23 17:32:49 +01:00
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
// This is just to inform the background process that cosmetic filters were
|
|
|
|
// actually injected.
|
|
|
|
if ( vAPI.styles.length !== styleTagCount ) {
|
2015-04-25 14:27:27 +02:00
|
|
|
localMessager.send({ what: 'cosmeticFiltersActivated' });
|
2015-04-25 13:28:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/587
|
2015-01-23 17:32:49 +01:00
|
|
|
// If no filters were found, maybe the script was injected before uBlock's
|
2015-03-07 19:20:18 +01:00
|
|
|
// process was fully initialized. When this happens, pages won't be
|
2015-01-23 17:32:49 +01:00
|
|
|
// cleaned right after browser launch.
|
2015-01-23 21:02:47 +01:00
|
|
|
vAPI.contentscriptStartInjected = details && details.ready;
|
2015-01-23 17:32:49 +01:00
|
|
|
|
|
|
|
// Cleanup before leaving
|
2014-10-17 21:44:19 +02:00
|
|
|
localMessager.close();
|
2014-08-14 19:59:37 +02:00
|
|
|
};
|
|
|
|
|
2015-12-22 22:32:09 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-17 22:07:42 +02:00
|
|
|
var hideElements = function(selectors) {
|
2014-06-24 00:42:43 +02:00
|
|
|
if ( document.body === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var elems = document.querySelectorAll(selectors);
|
|
|
|
var i = elems.length;
|
2015-07-06 13:48:56 +02:00
|
|
|
if ( i === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/158
|
|
|
|
// Using CSSStyleDeclaration.setProperty is more reliable
|
2015-07-06 19:53:39 +02:00
|
|
|
if ( document.body.shadowRoot === undefined ) {
|
2015-07-06 13:48:56 +02:00
|
|
|
while ( i-- ) {
|
|
|
|
elems[i].style.setProperty('display', 'none', 'important');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// https://github.com/gorhill/uBlock/issues/435
|
|
|
|
// Using shadow content so that we do not have to modify style
|
|
|
|
// attribute.
|
|
|
|
var sessionId = vAPI.sessionId;
|
|
|
|
var elem, shadow;
|
2014-06-24 00:42:43 +02:00
|
|
|
while ( i-- ) {
|
2015-07-06 13:48:56 +02:00
|
|
|
elem = elems[i];
|
2015-10-14 22:49:57 +02:00
|
|
|
shadow = elem.shadowRoot;
|
2015-08-14 01:36:54 +02:00
|
|
|
// https://www.chromestatus.com/features/4668884095336448
|
|
|
|
// "Multiple shadow roots is being deprecated."
|
2015-10-14 22:49:57 +02:00
|
|
|
if ( shadow !== null ) {
|
|
|
|
if ( shadow.className !== sessionId ) {
|
|
|
|
elem.style.setProperty('display', 'none', 'important');
|
|
|
|
}
|
2015-07-06 13:48:56 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-08-03 15:25:18 +02:00
|
|
|
// https://github.com/gorhill/uBlock/pull/555
|
|
|
|
// Not all nodes can be shadowed:
|
|
|
|
// https://github.com/w3c/webcomponents/issues/102
|
2015-10-14 22:49:57 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/762
|
|
|
|
// Remove display style that might get in the way of the shadow
|
|
|
|
// node doing its magic.
|
2015-08-03 05:49:17 +02:00
|
|
|
try {
|
|
|
|
shadow = elem.createShadowRoot();
|
|
|
|
shadow.className = sessionId;
|
2015-10-14 22:49:57 +02:00
|
|
|
elem.style.removeProperty('display');
|
2015-08-03 05:49:17 +02:00
|
|
|
} catch (ex) {
|
2015-10-14 22:49:57 +02:00
|
|
|
elem.style.setProperty('display', 'none', 'important');
|
2015-08-03 05:49:17 +02:00
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-22 22:32:09 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-04 10:01:04 +01:00
|
|
|
var url = window.location.href;
|
2014-10-17 21:44:19 +02:00
|
|
|
localMessager.send(
|
2014-06-24 00:42:43 +02:00
|
|
|
{
|
|
|
|
what: 'retrieveDomainCosmeticSelectors',
|
2015-03-04 10:01:04 +01:00
|
|
|
pageURL: url,
|
|
|
|
locationURL: url
|
2014-06-24 00:42:43 +02:00
|
|
|
},
|
2014-08-14 19:59:37 +02:00
|
|
|
filteringHandler
|
2014-06-24 00:42:43 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|