uBlock/src/js/contentscript-start.js

175 lines
5.8 KiB
JavaScript
Raw Normal View History

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 */
2015-02-14 18:16:36 +01:00
/* global vAPI, HTMLDocument */
2014-11-16 20:06:29 +01:00
/******************************************************************************/
2014-06-24 00:42:43 +02:00
// Injected into content pages
/******************************************************************************/
(function() {
'use strict';
/******************************************************************************/
2015-01-13 21:52:15 +01:00
// https://github.com/gorhill/uBlock/issues/464
if ( document instanceof HTMLDocument === false ) {
2015-01-23 17:32:49 +01:00
//console.debug('contentscript-start.js > not a HTLMDocument');
2015-01-13 21:52:15 +01:00
return false;
}
2014-12-19 20:04:17 +01:00
// Because in case
if ( !vAPI ) {
2015-01-23 17:32:49 +01:00
//console.debug('contentscript-start.js > vAPI not found');
return;
}
2014-08-28 17:08:51 +02:00
// https://github.com/gorhill/uBlock/issues/456
// 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');
return;
}
2015-01-02 03:14:53 +01:00
vAPI.contentscriptStartInjected = true;
/******************************************************************************/
2014-06-24 00:42:43 +02: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.
var cosmeticFilters = function(details) {
var donthideCosmeticFilters = {};
var hideCosmeticFilters = {};
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-- ) {
donthideCosmeticFilters[donthide[i]] = true;
2014-11-16 20:06:29 +01:00
}
2015-02-14 18:16:36 +01:00
}
// https://github.com/gorhill/uBlock/issues/143
if ( hide.length !== 0 ) {
i = hide.length;
var selector;
while ( i-- ) {
selector = hide[i];
if ( donthideCosmeticFilters[selector] ) {
hide.splice(i, 1);
} 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 ) {
var text = hide.join(',\n');
2014-08-17 22:07:42 +02:00
hideElements(text);
var style = vAPI.specificHideStyle = document.createElement('style');
// The linefeed before the style block is very important: do not remove!
2014-08-14 02:03:55 +02:00
style.appendChild(document.createTextNode(text + '\n{display:none !important;}'));
//console.debug('µBlock> "%s" cosmetic filters: injecting %d CSS rules:', details.domain, details.hide.length, hideStyleText);
var parent = document.head || document.documentElement;
if ( parent ) {
parent.appendChild(style);
}
2014-08-07 22:12:15 +02:00
}
vAPI.donthideCosmeticFilters = donthideCosmeticFilters;
vAPI.hideCosmeticFilters = hideCosmeticFilters;
2014-06-24 00:42:43 +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));
};
var filteringHandler = function(details) {
2014-12-19 20:00:46 +01:00
vAPI.skipCosmeticFiltering = !details || details.skipCosmeticFiltering;
2014-09-16 21:39:41 +02:00
if ( details ) {
if ( details.cosmeticHide.length !== 0 || details.cosmeticDonthide.length !== 0 ) {
cosmeticFilters(details);
}
if ( details.netHide.length !== 0 ) {
netFilters(details);
}
// The port will never be used again at this point, disconnecting allows
// the browser to flush this script from memory.
}
2015-01-23 17:32:49 +01:00
2015-01-23 17:47:31 +01:00
// https://github.com/gorhill/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
localMessager.close();
};
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;
}
2014-08-17 22:07:42 +02:00
// https://github.com/gorhill/uBlock/issues/158
// Using CSSStyleDeclaration.setProperty is more reliable
2014-06-24 00:42:43 +02:00
var elems = document.querySelectorAll(selectors);
var i = elems.length;
while ( i-- ) {
2014-08-17 22:07:42 +02:00
elems[i].style.setProperty('display', 'none', 'important');
2014-06-24 00:42:43 +02:00
}
};
var url = window.location.href;
localMessager.send(
2014-06-24 00:42:43 +02:00
{
what: 'retrieveDomainCosmeticSelectors',
pageURL: url,
locationURL: url
2014-06-24 00:42:43 +02:00
},
filteringHandler
2014-06-24 00:42:43 +02:00
);
/******************************************************************************/
/******************************************************************************/
})();
/******************************************************************************/