1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-16 23:42:39 +01:00

Merge pull request #1339 from gorhill/chrisaljoudi

code review re. https://github.com/gorhill/uBlock/issues/134: no need to...
This commit is contained in:
Chris Aljoudi 2015-04-30 13:47:25 -06:00
commit c283ff5879
15 changed files with 561 additions and 259 deletions

View File

@ -1,60 +1,60 @@
e42e13e07117589d46671e876c787cc2 assets/ublock/filter-lists.json
7ef50e668ccfdd4de56af0003f575e93 assets/ublock/filters.txt
c9c5cc56bec563bc1885847f925b9be2 assets/ublock/mirror-candidates.txt
ba85c7991d208fe26c8f74f57bb5cfe1 assets/ublock/privacy.txt
0d707b2cee4a38988662107971137d13 assets/thirdparties/abp.mozilla-hispano.org/nauscopio/filtros.txt
36a285b3ef9aadad4529d40153364013 assets/thirdparties/adblock-chinalist.googlecode.com/svn/trunk/adblock.txt
7c0d09fbccd87b5252d3bab3e822f2e4 assets/thirdparties/adblock-czechoslovaklist.googlecode.com/svn/filters.txt
bd73f55fa11330ec115e41d7194935f2 assets/ublock/filters.txt
c9c5cc56bec563bc1885847f925b9be2 assets/ublock/mirror-candidates.txt
e42e13e07117589d46671e876c787cc2 assets/ublock/filter-lists.json
132b3ecc9da8a68c3faf740c00af734b assets/thirdparties/adblock-plus-japanese-filter.googlecode.com/hg/abp_jp.txt
94c0a3eab74c42783855f07b22a429cf assets/thirdparties/home.fredfiber.no/langsholt/adblock.txt
1888a9521b2a138699ec01c654e35118 assets/thirdparties/www.zoso.ro/pages/rolist.txt
72373316d0e7ad22604d307c2d93e7cc assets/thirdparties/adblock.gardar.net/is.abp.txt
81fb33375814d8fb1653ba721ffd6a01 assets/thirdparties/adblock.schack.dk/block.txt
041f42feff1f0046a350506207d83a4c assets/thirdparties/raw.githubusercontent.com/k2jp/abp-japanese-filters/master/abp_jp.txt
eef36569f78b6a2401c1e228f7761bc7 assets/thirdparties/raw.githubusercontent.com/AdBlockPlusIsrael/EasyListHebrew/master/EasyListHebrew.txt
0b1e52e8e0bb5f0458e2d315f0fd96de assets/thirdparties/raw.githubusercontent.com/cjx82630/cjxlist/master/cjxlist.txt
aaaefbe31ac8c7d2c32e684ffdca2bff assets/thirdparties/raw.githubusercontent.com/reek/anti-adblock-killer/master/anti-adblock-killer-filters.txt
24231e63eeab50ad6116218aa1dc3b5b assets/thirdparties/raw.githubusercontent.com/wiltteri/wiltteri.txt/master/wiltteri.txt
5471b40cfefa128a133b993e5ef2f555 assets/thirdparties/raw.githubusercontent.com/tomasko126/easylistczechandslovak/master/filters.txt
068ceb3b1d8ce5ef155d6bdd30ec9d53 assets/thirdparties/raw.githubusercontent.com/szpeter80/hufilter/master/hufilter.txt
cbde6559e6100aca9690fd01f2a93ad3 assets/thirdparties/raw.githubusercontent.com/adblockpolska/Adblock_PL_List/master/adblock_polska.txt
d8c58f5bb8072907ae361bc14dbbd28f assets/thirdparties/cjxlist1.googlecode.com/svn/cjxlist.txt
b2823c07be0286782cc72c2355425a67 assets/thirdparties/dl.dropboxusercontent.com/u/1289327/abpxfiles/filtri.txt
8d87a4fd68102ba59da4989cfd65ed05 assets/thirdparties/easylist-downloads.adblockplus.org/advblock.txt
e0fcb343a05b399f0b0824f573c5015c assets/thirdparties/easylist-downloads.adblockplus.org/bitblock.txt
674e58227219e0e477dc5b6f664388ed assets/thirdparties/easylist-downloads.adblockplus.org/easylist.txt
f58ca4633056194a1350ebfa90a7f9d3 assets/thirdparties/easylist-downloads.adblockplus.org/easylist_noelemhide.txt
7720856eb1b1d15d0ab415b1f19d2345 assets/thirdparties/www.hufilter.hu/hufilter.txt
295bde39b70e1b3df7fc281d2d369ac7 assets/thirdparties/gnuzilla.gnu.org/filters/blacklist.txt
7755619a576f2479dcf26ef1c2753182 assets/thirdparties/spam404bl.com/spam404scamlist.txt
2ed9fcfc35ac135db1e2fda3cda8fcbc assets/thirdparties/easylist-downloads.adblockplus.org/easylistchina.txt
06ed68d31cf9460e58c0fa8ff8a61326 assets/thirdparties/easylist-downloads.adblockplus.org/easylistdutch.txt
4f2cb90acac064a6dec053a5c809c0a0 assets/thirdparties/easylist-downloads.adblockplus.org/easylistgermany.txt
3c4d23e31a12b822fd86975ab6f67e16 assets/thirdparties/easylist-downloads.adblockplus.org/easylistitaly.txt
3e07d4c7ee50251b950c392c8060b12d assets/thirdparties/easylist-downloads.adblockplus.org/easyprivacy.txt
44497cd06bf45dc738b7716ca1fa66b6 assets/thirdparties/easylist-downloads.adblockplus.org/fanboy-annoyance.txt
e0fcb343a05b399f0b0824f573c5015c assets/thirdparties/easylist-downloads.adblockplus.org/bitblock.txt
06ed68d31cf9460e58c0fa8ff8a61326 assets/thirdparties/easylist-downloads.adblockplus.org/easylistdutch.txt
3e07d4c7ee50251b950c392c8060b12d assets/thirdparties/easylist-downloads.adblockplus.org/easyprivacy.txt
f58ca4633056194a1350ebfa90a7f9d3 assets/thirdparties/easylist-downloads.adblockplus.org/easylist_noelemhide.txt
8d87a4fd68102ba59da4989cfd65ed05 assets/thirdparties/easylist-downloads.adblockplus.org/advblock.txt
def19717429aaeee2331cb797d5e240e assets/thirdparties/easylist-downloads.adblockplus.org/fanboy-social.txt
d7a5ce9f655e43cd6026fa31356b8161 assets/thirdparties/easylist-downloads.adblockplus.org/liste_fr.txt
c95fd7804f199ddc7aa5cd56a61a32ec assets/thirdparties/gitorious.org/adblock-latvian/adblock-latvian/raw/master_lists/latvian-list.txt
295bde39b70e1b3df7fc281d2d369ac7 assets/thirdparties/gnuzilla.gnu.org/filters/blacklist.txt
94c0a3eab74c42783855f07b22a429cf assets/thirdparties/home.fredfiber.no/langsholt/adblock.txt
c0b6983add2393e18defcbda28fecec1 assets/thirdparties/hosts-file.net/ad-servers
d12fbce40c55bdea5d8c7ef6b0d0202d assets/thirdparties/indonesianadblockrules.googlecode.com/hg/subscriptions/abpindo.txt
cb26265deac870beef7133660bff9704 assets/thirdparties/liste-ar-adblock.googlecode.com/hg/Liste_AR.txt
dea183551bca4d87e6c5ecee3bbe0c82 assets/thirdparties/margevicius.lt/easylistlithuania.txt
3c4d23e31a12b822fd86975ab6f67e16 assets/thirdparties/easylist-downloads.adblockplus.org/easylistitaly.txt
4f2cb90acac064a6dec053a5c809c0a0 assets/thirdparties/easylist-downloads.adblockplus.org/easylistgermany.txt
674e58227219e0e477dc5b6f664388ed assets/thirdparties/easylist-downloads.adblockplus.org/easylist.txt
36a285b3ef9aadad4529d40153364013 assets/thirdparties/adblock-chinalist.googlecode.com/svn/trunk/adblock.txt
b2823c07be0286782cc72c2355425a67 assets/thirdparties/dl.dropboxusercontent.com/u/1289327/abpxfiles/filtri.txt
adc945b82aec20f53ea31c36468b3759 assets/thirdparties/mirror1.malwaredomains.com/files/immortal_domains.txt
8732c6da425f67017af76a40f33ef0b4 assets/thirdparties/mirror1.malwaredomains.com/files/justdomains
7f0443f3dcc9abfd47cfbc95ce824ddb assets/thirdparties/mirror1.malwaredomains.com/files/README.md
5b8e13b618c68293430913029118781a assets/thirdparties/pgl.yoyo.org/as/README.md
b68842b8073833ff04414d8141986ac7 assets/thirdparties/pgl.yoyo.org/as/serverlist
8732c6da425f67017af76a40f33ef0b4 assets/thirdparties/mirror1.malwaredomains.com/files/justdomains
e63f51b30304ddca0337c8eba3bff101 assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat
eef36569f78b6a2401c1e228f7761bc7 assets/thirdparties/raw.githubusercontent.com/AdBlockPlusIsrael/EasyListHebrew/master/EasyListHebrew.txt
cbde6559e6100aca9690fd01f2a93ad3 assets/thirdparties/raw.githubusercontent.com/adblockpolska/Adblock_PL_List/master/adblock_polska.txt
0b1e52e8e0bb5f0458e2d315f0fd96de assets/thirdparties/raw.githubusercontent.com/cjx82630/cjxlist/master/cjxlist.txt
041f42feff1f0046a350506207d83a4c assets/thirdparties/raw.githubusercontent.com/k2jp/abp-japanese-filters/master/abp_jp.txt
aaaefbe31ac8c7d2c32e684ffdca2bff assets/thirdparties/raw.githubusercontent.com/reek/anti-adblock-killer/master/anti-adblock-killer-filters.txt
068ceb3b1d8ce5ef155d6bdd30ec9d53 assets/thirdparties/raw.githubusercontent.com/szpeter80/hufilter/master/hufilter.txt
5471b40cfefa128a133b993e5ef2f555 assets/thirdparties/raw.githubusercontent.com/tomasko126/easylistczechandslovak/master/filters.txt
24231e63eeab50ad6116218aa1dc3b5b assets/thirdparties/raw.githubusercontent.com/wiltteri/wiltteri.txt/master/wiltteri.txt
0d707b2cee4a38988662107971137d13 assets/thirdparties/abp.mozilla-hispano.org/nauscopio/filtros.txt
dea183551bca4d87e6c5ecee3bbe0c82 assets/thirdparties/margevicius.lt/easylistlithuania.txt
5933a3c9059d10ef13544013e0d6e658 assets/thirdparties/someonewhocares.org/hosts/hosts
7755619a576f2479dcf26ef1c2753182 assets/thirdparties/spam404bl.com/spam404scamlist.txt
72ae2048c0201c940978d27cda361891 assets/thirdparties/stanev.org/abp/adblock_bg.txt
d960765d2489db4f6038d3032cbd988c assets/thirdparties/winhelp2002.mvps.org/hosts.txt
81fb33375814d8fb1653ba721ffd6a01 assets/thirdparties/adblock.schack.dk/block.txt
7c0d09fbccd87b5252d3bab3e822f2e4 assets/thirdparties/adblock-czechoslovaklist.googlecode.com/svn/filters.txt
cb26265deac870beef7133660bff9704 assets/thirdparties/liste-ar-adblock.googlecode.com/hg/Liste_AR.txt
e68acbf09f22a8ac0650907fdd45a669 assets/thirdparties/www.fanboy.co.nz/fanboy-korean.txt
38517bc04606b74d18cd2b48cfb74c62 assets/thirdparties/www.fanboy.co.nz/enhancedstats.txt
2a9d105b544c47c55a3a22bb536db2bc assets/thirdparties/www.fanboy.co.nz/fanboy-antifacebook.txt
e68acbf09f22a8ac0650907fdd45a669 assets/thirdparties/www.fanboy.co.nz/fanboy-korean.txt
5de29853cdd54c919c283c9fdb0f3998 assets/thirdparties/www.fanboy.co.nz/fanboy-swedish.txt
dbdc8f71338ca741a6d1e23440960965 assets/thirdparties/www.fanboy.co.nz/fanboy-ultimate.txt
faf479000090920441f1f01a664e0aca assets/thirdparties/www.fanboy.co.nz/fanboy-vietnam.txt
7720856eb1b1d15d0ab415b1f19d2345 assets/thirdparties/www.hufilter.hu/hufilter.txt
01ef1940551cff40fd1011f08f990d04 assets/thirdparties/www.malwaredomainlist.com/hostslist/hosts.txt
dbdc8f71338ca741a6d1e23440960965 assets/thirdparties/www.fanboy.co.nz/fanboy-ultimate.txt
5de29853cdd54c919c283c9fdb0f3998 assets/thirdparties/www.fanboy.co.nz/fanboy-swedish.txt
042419405031f0fcfac3735bf4f05e21 assets/thirdparties/www.malwaredomainlist.com/hostslist/README.md
01ef1940551cff40fd1011f08f990d04 assets/thirdparties/www.malwaredomainlist.com/hostslist/hosts.txt
c95fd7804f199ddc7aa5cd56a61a32ec assets/thirdparties/gitorious.org/adblock-latvian/adblock-latvian/raw/master_lists/latvian-list.txt
d12fbce40c55bdea5d8c7ef6b0d0202d assets/thirdparties/indonesianadblockrules.googlecode.com/hg/subscriptions/abpindo.txt
72ae2048c0201c940978d27cda361891 assets/thirdparties/stanev.org/abp/adblock_bg.txt
c0b6983add2393e18defcbda28fecec1 assets/thirdparties/hosts-file.net/ad-servers
b68842b8073833ff04414d8141986ac7 assets/thirdparties/pgl.yoyo.org/as/serverlist
5b8e13b618c68293430913029118781a assets/thirdparties/pgl.yoyo.org/as/README.md
342eccafdb7e6ef7b9aacd33d1442ed1 assets/thirdparties/www.void.gr/kargig/void-gr-filters.txt
1888a9521b2a138699ec01c654e35118 assets/thirdparties/www.zoso.ro/pages/rolist.txt

View File

@ -313,3 +313,7 @@ deviantart.com##.dp-ad-chrome.dp-ad-visible
# To deal with https://github.com/chrisaljoudi/uBlock/issues/1013
||mac-system-alert.com^
# Video playback broken on lastminutegoals.org
# To counter `intergi.com` in Peter Lowe's
@@||cdn.phoenix.intergi.com^$domain=lastminutegoals.org

View File

@ -22,6 +22,7 @@
<script src="js/messaging.js"></script>
<script src="js/profiler.js"></script>
<script src="js/storage.js"></script>
<script src="js/logger.js"></script>
<script src="js/pagestore.js"></script>
<script src="js/tab.js"></script>
<script src="js/traffic.js"></script>

View File

@ -68,18 +68,32 @@ input:focus {
direction: ltr;
width: 100%;
}
#content table tr.docBoundary {
background-color: #666;
color: white;
text-align: center;
}
#content table tr.docBoundary > td:first-child {
padding: 1em 0;
white-space: normal;
word-break: break-all;
word-wrap: break-word;
}
#content table tr.blocked {
background-color: rgba(192, 0, 0, 0.1)
background-color: rgba(192, 0, 0, 0.1);
}
#content table tr.allowed {
background-color: rgba(0, 160, 0, 0.1)
background-color: rgba(0, 160, 0, 0.1);
}
#content table tr.allowed.mirrored {
background-color: rgba(255, 255, 0, 0.3)
background-color: rgba(255, 255, 0, 0.3);
}
#content table tr.maindoc {
background-color: #eee;
}
#content table tr.cosmetic {
background-color: rgba(255, 255, 0, 0.1);
}
body:not(.filterOff) #content table tr.hidden {
display: none;
}

View File

@ -55,6 +55,7 @@ if ( vAPI.contentscriptEndInjected ) {
return;
}
vAPI.contentscriptEndInjected = true;
vAPI.styles = vAPI.styles || [];
/******************************************************************************/
/******************************************************************************/
@ -187,7 +188,7 @@ var uBlockCollapser = (function() {
}
if ( selectors.length !== 0 ) {
messager.send({
what: 'injectedSelectors',
what: 'cosmeticFiltersInjected',
type: 'net',
hostname: window.location.hostname,
selectors: selectors
@ -385,7 +386,7 @@ var uBlockCollapser = (function() {
processHighHighGenericsAsync();
}
}
if ( hideSelectors.length ) {
if ( hideSelectors.length !== 0 ) {
addStyleTag(hideSelectors);
}
contextNodes.length = 0;
@ -400,7 +401,7 @@ var uBlockCollapser = (function() {
// - Injecting a style tag
var addStyleTag = function(selectors) {
var selectorStr = selectors.toString();
var selectorStr = selectors.join(',\n');
hideElements(selectorStr);
var style = document.createElement('style');
// The linefeed before the style block is very important: do no remove!
@ -408,9 +409,10 @@ var uBlockCollapser = (function() {
var parent = document.body || document.documentElement;
if ( parent ) {
parent.appendChild(style);
vAPI.styles.push(style);
}
messager.send({
what: 'injectedSelectors',
what: 'cosmeticFiltersInjected',
type: 'cosmetic',
hostname: window.location.hostname,
selectors: selectors
@ -701,6 +703,7 @@ var uBlockCollapser = (function() {
idsFromNodeList(selectNodes('[id]'));
classesFromNodeList(selectNodes('[class]'));
retrieveGenericSelectors();
messager.send({ what: 'cosmeticFiltersActivated' });
}
};

View File

@ -53,6 +53,7 @@ if ( vAPI.contentscriptStartInjected ) {
return;
}
vAPI.contentscriptStartInjected = true;
vAPI.styles = vAPI.styles || [];
/******************************************************************************/
@ -99,6 +100,7 @@ var cosmeticFilters = function(details) {
var parent = document.head || document.documentElement;
if ( parent ) {
parent.appendChild(style);
vAPI.styles.push(style);
}
}
vAPI.donthideCosmeticFilters = donthideCosmeticFilters;
@ -121,6 +123,8 @@ var netFilters = function(details) {
};
var filteringHandler = function(details) {
var styleTagCount = vAPI.styles.length;
vAPI.skipCosmeticFiltering = !details || details.skipCosmeticFiltering;
if ( details ) {
if ( details.cosmeticHide.length !== 0 || details.cosmeticDonthide.length !== 0 ) {
@ -129,8 +133,12 @@ var filteringHandler = function(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.
}
// This is just to inform the background process that cosmetic filters were
// actually injected.
if ( vAPI.styles.length !== styleTagCount ) {
localMessager.send({ what: 'cosmeticFiltersActivated' });
}
// https://github.com/chrisaljoudi/uBlock/issues/587
@ -139,7 +147,8 @@ var filteringHandler = function(details) {
// cleaned right after browser launch.
vAPI.contentscriptStartInjected = details && details.ready;
// Cleanup before leaving
// The port will never be used again at this point, disconnecting allows
// the browser to flush this script from memory.
localMessager.close();
};

96
src/js/cosmetic-logger.js Normal file
View File

@ -0,0 +1,96 @@
/*******************************************************************************
uBlock - a browser extension to block requests.
Copyright (C) 2015 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 vAPI, HTMLDocument */
/******************************************************************************/
(function() {
'use strict';
/******************************************************************************/
// https://github.com/gorhill/uBlock/issues/464
if ( document instanceof HTMLDocument === false ) {
//console.debug('cosmetic-logger.js > not a HTLMDocument');
return;
}
// This can happen
if ( !vAPI ) {
//console.debug('cosmetic-logger.js > vAPI not found');
return;
}
/******************************************************************************/
var loggedSelectors = vAPI.loggedSelectors || {};
var injectedSelectors = [];
var reProperties = /\s*\{[^}]+\}\s*/;
var i;
var styles = vAPI.styles || [];
i = styles.length;
while ( i-- ) {
injectedSelectors = injectedSelectors.concat(styles[i].textContent.replace(reProperties, '').split(/\s*,\n\s*/));
}
if ( injectedSelectors.length === 0 ) {
return;
}
var matchedSelectors = [];
var selector;
i = injectedSelectors.length;
while ( i-- ) {
selector = injectedSelectors[i];
if ( loggedSelectors.hasOwnProperty(selector) ) {
continue;
}
if ( document.querySelector(selector) === null ) {
continue;
}
loggedSelectors[selector] = true;
matchedSelectors.push(selector);
}
vAPI.loggedSelectors = loggedSelectors;
/******************************************************************************/
var localMessager = vAPI.messaging.channel('cosmetic-*.js');
localMessager.send({
what: 'logCosmeticFilteringData',
pageURL: window.location.href,
matchedSelectors: matchedSelectors
}, function() {
localMessager.close();
});
/******************************************************************************/
})();
/******************************************************************************/

View File

@ -37,7 +37,8 @@ var inspectedTabId = '';
var doc = document;
var body = doc.body;
var tbody = doc.querySelector('#content tbody');
var rowJunkyard = [];
var row1Junkyard = [];
var row4Junkyard = [];
var reFilter = null;
var filterTargetTestResult = true;
var maxEntries = 0;
@ -106,7 +107,7 @@ var renderURL = function(url, filter) {
/******************************************************************************/
var createRow = function() {
var tr = rowJunkyard.pop();
var tr = row4Junkyard.pop();
if ( tr ) {
tr.className = '';
return tr;
@ -121,8 +122,35 @@ var createRow = function() {
/******************************************************************************/
var createGap = function(url) {
var tr = row1Junkyard.pop();
if ( !tr ) {
tr = doc.createElement('tr');
tr.classList.add('docBoundary');
tr.appendChild(doc.createElement('td'));
tr.cells[0].setAttribute('colspan', '4');
}
tr.cells[0].textContent = url;
tbody.insertBefore(tr, tbody.firstChild);
};
/******************************************************************************/
var renderLogEntry = function(entry) {
var tr = createRow();
// If the request is that of a root frame, insert a gap in the table
// in order to visually separate entries for different documents.
if ( entry.type === 'main_frame' ) {
createGap(entry.url);
tr.classList.add('maindoc');
}
// Cosmetic filter?
if ( entry.result.charAt(0) === 'c' ) {
tr.classList.add('cosmetic');
}
if ( entry.result.charAt(1) === 'b' ) {
tr.classList.add('blocked');
tr.cells[0].textContent = ' -\u00A0';
@ -135,9 +163,11 @@ var renderLogEntry = function(entry) {
} else {
tr.cells[0].textContent = '';
}
if ( entry.type === 'main_frame' ) {
tr.classList.add('maindoc');
}
var filterText = entry.result.slice(3);
if ( entry.result.lastIndexOf('sa', 0) === 0 ) {
filterText = '@@' + filterText;
@ -198,8 +228,17 @@ var truncateLog = function(size) {
size = 25000;
}
size = Math.min(size, 25000);
var tr;
while ( tbody.childElementCount > size ) {
rowJunkyard.push(tbody.removeChild(tbody.lastElementChild));
tr = tbody.lastElementChild;
// https://github.com/gorhill/uBlock/issues/123
// Triage according to row type.
if ( tr.cells.length === 1 ) {
row1Junkyard.push(tr);
} else {
row4Junkyard.push(tr);
}
tbody.removeChild(tr);
}
};
@ -225,8 +264,17 @@ var readLogBuffer = function() {
/******************************************************************************/
var clearBuffer = function() {
var tr;
while ( tbody.firstChild !== null ) {
rowJunkyard.push(tbody.removeChild(tbody.firstChild));
tr = tbody.lastElementChild;
// https://github.com/gorhill/uBlock/issues/123
// Triage according to row type.
if ( tr.cells.length === 1 ) {
row1Junkyard.push(tr);
} else {
row4Junkyard.push(tr);
}
tbody.removeChild(tr);
}
};

214
src/js/logger.js Normal file
View File

@ -0,0 +1,214 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2015 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 µBlock */
/******************************************************************************/
/******************************************************************************/
µBlock.logger = (function() {
'use strict';
/******************************************************************************/
/******************************************************************************/
var LogEntry = function(details, result) {
this.init(details, result);
};
/******************************************************************************/
var logEntryFactory = function(details, result) {
var entry = logEntryJunkyard.pop();
if ( entry ) {
return entry.init(details, result);
}
return new LogEntry(details, result);
};
var logEntryJunkyard = [];
var logEntryJunkyardMax = 100;
/******************************************************************************/
LogEntry.prototype.init = function(details, result) {
this.tstamp = Date.now();
this.url = details.requestURL;
this.hostname = details.requestHostname;
this.type = details.requestType;
this.result = result;
return this;
};
/******************************************************************************/
LogEntry.prototype.dispose = function() {
this.url = this.hostname = this.type = this.result = '';
if ( logEntryJunkyard.length < logEntryJunkyardMax ) {
logEntryJunkyard.push(this);
}
};
/******************************************************************************/
/******************************************************************************/
var LogBuffer = function() {
this.lastReadTime = 0;
this.size = 50;
this.buffer = new Array(this.size);
this.readPtr = 0;
this.writePtr = 0;
};
/******************************************************************************/
LogBuffer.prototype.dispose = function() {
var entry;
var i = this.buffer.length;
while ( i-- ) {
entry = this.buffer[i];
if ( entry instanceof LogEntry ) {
entry.dispose();
}
}
this.buffer = null;
return null;
};
/******************************************************************************/
LogBuffer.prototype.writeOne = function(details, result) {
// Reusing log entry = less memory churning
var entry = this.buffer[this.writePtr];
if ( entry instanceof LogEntry === false ) {
this.buffer[this.writePtr] = logEntryFactory(details, result);
} else {
entry.init(details, result);
}
this.writePtr += 1;
if ( this.writePtr === this.size ) {
this.writePtr = 0;
}
// Grow the buffer between 1.5x-2x the current size
if ( this.writePtr === this.readPtr ) {
var toMove = this.buffer.slice(0, this.writePtr);
var minSize = Math.ceil(this.size * 1.5);
this.size += toMove.length;
if ( this.size < minSize ) {
this.buffer = this.buffer.concat(toMove, new Array(minSize - this.size));
this.writePtr = this.size;
} else {
this.buffer = this.buffer.concat(toMove);
this.writePtr = 0;
}
this.size = this.buffer.length;
}
};
/******************************************************************************/
LogBuffer.prototype.readAll = function() {
var out;
if ( this.readPtr < this.writePtr ) {
out = this.buffer.slice(this.readPtr, this.writePtr);
} else if ( this.writePtr < this.readPtr ) {
out = this.buffer.slice(this.readPtr).concat(this.buffer.slice(0, this.writePtr));
} else {
out = [];
}
this.readPtr = this.writePtr;
this.lastReadTime = Date.now();
return out;
};
/******************************************************************************/
/******************************************************************************/
// Tab id to log buffer instances
var logBuffers = {};
// After 30 seconds without being read, a buffer will be considered unused, and
// thus removed from memory.
var logBufferObsoleteAfter = 30 * 1000;
/******************************************************************************/
var writeOne = function(tabId, details, result) {
if ( logBuffers.hasOwnProperty(tabId) === false ) {
return;
}
var logBuffer = logBuffers[tabId];
logBuffer.writeOne(details, result);
};
/******************************************************************************/
var readAll = function(tabId) {
if ( logBuffers.hasOwnProperty(tabId) === false ) {
logBuffers[tabId] = new LogBuffer();
}
return logBuffers[tabId].readAll();
};
/******************************************************************************/
var isObserved = function(tabId) {
return logBuffers.hasOwnProperty(tabId);
};
/******************************************************************************/
var loggerJanitor = function() {
var logBuffer;
var obsolete = Date.now() - logBufferObsoleteAfter;
for ( var tabId in logBuffers ) {
if ( logBuffers.hasOwnProperty(tabId) === false ) {
continue;
}
logBuffer = logBuffers[tabId];
if ( logBuffer.lastReadTime < obsolete ) {
logBuffer.dispose();
delete logBuffers[tabId];
}
}
setTimeout(loggerJanitor, loggerJanitorPeriod);
};
// The janitor will look for stale log buffer every 2 minutes.
var loggerJanitorPeriod = 2 * 60 * 1000;
setTimeout(loggerJanitor, loggerJanitorPeriod);
/******************************************************************************/
return {
writeOne: writeOne,
readAll: readAll,
isObserved: isObserved
};
/******************************************************************************/
/******************************************************************************/
})();
/******************************************************************************/

View File

@ -50,6 +50,8 @@ var onMessage = function(request, sender, callback) {
break;
}
var tabId = sender && sender.tab ? sender.tab.id : 0;
// Sync
var response;
@ -59,6 +61,17 @@ var onMessage = function(request, sender, callback) {
µb.contextMenuClientY = request.clientY;
break;
case 'cosmeticFiltersInjected':
// Is this a request to cache selectors?
µb.cosmeticFilteringEngine.addToSelectorCache(request);
/* falls through */
case 'cosmeticFiltersActivated':
// Net-based cosmetic filters are of no interest for logging purpose.
if ( µb.logger.isObserved(tabId) && request.type !== 'net' ) {
µb.logCosmeticFilters(tabId);
}
break;
case 'forceUpdateAssets':
µb.assetUpdater.force();
break;
@ -463,9 +476,9 @@ var filterRequests = function(pageStore, details) {
/******************************************************************************/
var onMessage = function(details, sender, callback) {
var onMessage = function(request, sender, callback) {
// Async
switch ( details.what ) {
switch ( request.what ) {
default:
break;
}
@ -478,21 +491,17 @@ var onMessage = function(details, sender, callback) {
pageStore = µb.pageStoreFromTabId(sender.tab.id);
}
switch ( details.what ) {
switch ( request.what ) {
case 'retrieveGenericCosmeticSelectors':
response = {
shutdown: !pageStore || !pageStore.getNetFilteringSwitch(),
result: null
};
if ( !response.shutdown && pageStore.getGenericCosmeticFilteringSwitch() ) {
response.result = µb.cosmeticFilteringEngine.retrieveGenericSelectors(details);
response.result = µb.cosmeticFilteringEngine.retrieveGenericSelectors(request);
}
break;
case 'injectedSelectors':
µb.cosmeticFilteringEngine.addToSelectorCache(details);
break;
// Evaluate many requests
case 'filterRequests':
response = {
@ -500,7 +509,7 @@ var onMessage = function(details, sender, callback) {
result: null
};
if(!response.shutdown) {
response.result = filterRequests(pageStore, details);
response.result = filterRequests(pageStore, request);
}
break;
@ -520,6 +529,73 @@ vAPI.messaging.listen('contentscript-end.js', onMessage);
/******************************************************************************/
/******************************************************************************/
// cosmetic-*.js
(function() {
'use strict';
/******************************************************************************/
var µb = µBlock;
/******************************************************************************/
var logCosmeticFilters = function(tabId, details) {
if ( µb.logger.isObserved(tabId) === false ) {
return;
}
var context = {
requestURL: details.pageURL,
requestHostname: µb.URI.hostnameFromURI(details.pageURL),
requestType: 'dom'
};
var selectors = details.matchedSelectors;
selectors.sort();
for ( var i = 0; i < selectors.length; i++ ) {
µb.logger.writeOne(tabId, context, 'cb:##' + selectors[i]);
}
};
/******************************************************************************/
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
var tabId = sender && sender.tab ? sender.tab.id : 0;
switch ( request.what ) {
case 'logCosmeticFilteringData':
logCosmeticFilters(tabId, request);
break;
default:
return vAPI.messaging.UNHANDLED;
}
callback(response);
};
vAPI.messaging.listen('cosmetic-*.js', onMessage);
/******************************************************************************/
})();
/******************************************************************************/
/******************************************************************************/
// element-picker.js
(function() {
@ -1134,10 +1210,7 @@ var onMessage = function(request, sender, callback) {
switch ( request.what ) {
case 'readLogBuffer':
var pageStore = µb.pageStoreFromTabId(request.tabId);
if ( pageStore ) {
response = pageStore.logBuffer.readAll();
}
response = µb.logger.readAll(request.tabId);
break;
default:

View File

@ -45,178 +45,6 @@ var µb = µBlock;
/******************************************************************************/
/******************************************************************************/
var LogEntry = function(details, result) {
this.init(details, result);
};
/******************************************************************************/
var logEntryFactory = function(details, result) {
var entry = logEntryJunkyard.pop();
if ( entry ) {
return entry.init(details, result);
}
return new LogEntry(details, result);
};
var logEntryJunkyard = [];
var logEntryJunkyardMax = 100;
/******************************************************************************/
LogEntry.prototype.init = function(details, result) {
this.tstamp = Date.now();
this.url = details.requestURL;
this.hostname = details.requestHostname;
this.type = details.requestType;
this.result = result;
return this;
};
/******************************************************************************/
LogEntry.prototype.dispose = function() {
this.url = this.hostname = this.type = this.result = '';
if ( logEntryJunkyard.length < logEntryJunkyardMax ) {
logEntryJunkyard.push(this);
}
};
/******************************************************************************/
var LogBuffer = function() {
this.lastReadTime = 0;
this.size = 50;
this.buffer = null;
this.readPtr = 0;
this.writePtr = 0;
};
/******************************************************************************/
var logBufferFactory = function() {
return new LogBuffer();
};
var liveLogBuffers = [];
/******************************************************************************/
LogBuffer.prototype.dispose = function() {
if ( this.buffer === null ) {
return null;
}
var entry;
var i = this.buffer.length;
while ( i-- ) {
entry = this.buffer[i];
if ( entry instanceof LogEntry ) {
entry.dispose();
}
}
this.buffer = null;
return null;
};
/******************************************************************************/
LogBuffer.prototype.start = function() {
if ( this.buffer === null ) {
this.buffer = new Array(this.size);
this.readPtr = 0;
this.writePtr = 0;
liveLogBuffers.push(this);
}
};
/******************************************************************************/
LogBuffer.prototype.stop = function() {
this.dispose();
this.buffer = null;
// The janitor will remove us from the live pool eventually.
};
/******************************************************************************/
LogBuffer.prototype.writeOne = function(details, result) {
if ( this.buffer === null ) {
return;
}
// Reusing log entry = less memory churning
var entry = this.buffer[this.writePtr];
if ( entry instanceof LogEntry === false ) {
this.buffer[this.writePtr] = logEntryFactory(details, result);
} else {
entry.init(details, result);
}
this.writePtr += 1;
if ( this.writePtr === this.size ) {
this.writePtr = 0;
}
// Grow the buffer between 1.5x-2x the current size
if ( this.writePtr === this.readPtr ) {
var toMove = this.buffer.slice(0, this.writePtr);
var minSize = Math.ceil(this.size * 1.5);
this.size += toMove.length;
if ( this.size < minSize ) {
this.buffer = this.buffer.concat(toMove, new Array(minSize - this.size));
this.writePtr = this.size;
} else {
this.buffer = this.buffer.concat(toMove);
this.writePtr = 0;
}
this.size = this.buffer.length;
}
};
/******************************************************************************/
LogBuffer.prototype.readAll = function() {
var out;
if ( this.buffer === null ) {
this.start();
out = [];
} else if ( this.readPtr < this.writePtr ) {
out = this.buffer.slice(this.readPtr, this.writePtr);
} else if ( this.writePtr < this.readPtr ) {
out = this.buffer.slice(this.readPtr).concat(this.buffer.slice(0, this.writePtr));
} else {
out = [];
}
this.readPtr = this.writePtr;
this.lastReadTime = Date.now();
return out;
};
/******************************************************************************/
var logBufferJanitor = function() {
var logBuffer;
var obsolete = Date.now() - logBufferObsoleteAfter;
var i = liveLogBuffers.length;
while ( i-- ) {
logBuffer = liveLogBuffers[i];
if ( logBuffer.lastReadTime < obsolete ) {
logBuffer.stop();
liveLogBuffers.splice(i, 1);
}
}
setTimeout(logBufferJanitor, logBufferJanitorPeriod);
};
// The janitor will look for stale log buffer every 2 minutes.
var logBufferJanitorPeriod = 2 * 60 * 1000;
// After 30 seconds without being read, a buffer will be considered unused, and
// thus removed from memory.
var logBufferObsoleteAfter = 30 * 1000;
setTimeout(logBufferJanitor, logBufferJanitorPeriod);
/******************************************************************************/
/******************************************************************************/
// To mitigate memory churning
var netFilteringResultCacheEntryJunkyard = [];
var netFilteringResultCacheEntryJunkyardMax = 200;
@ -483,12 +311,6 @@ PageStore.prototype.init = function(tabId) {
.matchStringExactType(context, tabContext.normalURL, 'cosmetic-filtering')
.charAt(1) === 'b';
// Preserve old buffer if there is one already, it may be in use, and
// overwritting it would required another read to restart it.
if ( this.logBuffer instanceof LogBuffer === false ) {
this.logBuffer = logBufferFactory();
}
return this;
};
@ -538,7 +360,6 @@ PageStore.prototype.dispose = function() {
this.hostnameToCountMap = null;
this.disposeFrameStores();
this.netFilteringCache = this.netFilteringCache.dispose();
this.logBuffer = this.logBuffer.dispose();
if ( pageStoreJunkyard.length < pageStoreJunkyardMax ) {
pageStoreJunkyard.push(this);
}
@ -703,7 +524,6 @@ PageStore.prototype.filterRequest = function(context) {
return result;
};
// Cache only what is worth it if logging is disabled
// http://jsperf.com/string-indexof-vs-object
var collapsibleRequestTypes = 'image sub_frame object';
@ -766,7 +586,6 @@ PageStore.prototype.logRequest = function(context, result) {
µb.localSettings.blockedRequestCount++;
}
µb.localSettingsModifyTime = now;
this.logBuffer.writeOne(context, result);
};
/******************************************************************************/

View File

@ -159,7 +159,7 @@
var µb = this;
var onCompiledListLoaded = function() {
var onSaved = function() {
var compiledFilters = µb.compileFilters(filters);
var snfe = µb.staticNetFilteringEngine;
var cfe = µb.cosmeticFilteringEngine;
@ -176,13 +176,6 @@
µb.cosmeticFilteringEngine.freeze();
};
var onSaved = function(details) {
if ( details.error ) {
return;
}
µb.getCompiledFilterList(µb.userFiltersPath, onCompiledListLoaded);
};
var onLoaded = function(details) {
if ( details.error ) {
return;

View File

@ -470,6 +470,7 @@ vAPI.tabs.onPopup = function(details) {
if ( pageStore ) {
pageStore.logRequest(context, result);
}
µb.logger.writeOne(details.openerTabId, context, result);
// Not blocked
if ( µb.isAllowResult(result) ) {

View File

@ -92,6 +92,7 @@ var onBeforeRequest = function(details) {
// Possible outcomes: blocked, allowed-passthru, allowed-mirror
pageStore.logRequest(requestContext, result);
µb.logger.writeOne(tabId, requestContext, result);
// Not blocked
if ( µb.isAllowResult(result) ) {
@ -152,7 +153,7 @@ var onBeforeRootFrameRequest = function(details) {
if ( pageStore ) {
pageStore.logRequest(context, result);
}
return;
µb.logger.writeOne(tabId, context, result);
};
/******************************************************************************/
@ -225,6 +226,7 @@ var onBeforeBehindTheSceneRequest = function(details) {
}
pageStore.logRequest(context, result);
µb.logger.writeOne(vAPI.noTabId, context, result);
// Not blocked
if ( µb.isAllowResult(result) ) {
@ -272,6 +274,7 @@ var onHeadersReceived = function(details) {
var result = pageStore.filterRequestNoCache(context);
pageStore.logRequest(context, result);
µb.logger.writeOne(tabId, context, result);
// Don't block
if ( µb.isAllowResult(result) ) {
@ -318,6 +321,7 @@ var onRootFrameHeadersReceived = function(details) {
var result = pageStore.filterRequest(context);
pageStore.logRequest(context, result);
µb.logger.writeOne(tabId, context, result);
// Don't block
if ( µb.isAllowResult(result) ) {

View File

@ -318,4 +318,27 @@ var matchWhitelistDirective = function(url, hostname, directive) {
/******************************************************************************/
µBlock.logCosmeticFilters = (function() {
var tabIdToTimerMap = {};
var injectNow = function(tabId) {
delete tabIdToTimerMap[tabId];
vAPI.tabs.injectScript(tabId, { file: 'js/cosmetic-logger.js' });
};
var injectAsync = function(tabId) {
if ( tabIdToTimerMap.hasOwnProperty(tabId) ) {
return;
}
tabIdToTimerMap[tabId] = setTimeout(
injectNow.bind(null, tabId),
100
);
};
return injectAsync;
})();
/******************************************************************************/
})();