Skip to content

Commit 2dde6f1

Browse files
committed
new switch: toggle cosmetic filtering on/off for a site
1 parent 6eb6d2b commit 2dde6f1

20 files changed

Lines changed: 461 additions & 41 deletions

platform/chromium/vapi-background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ vAPI.tabs = {};
6565

6666
/******************************************************************************/
6767

68-
vAPI.isNoTabId = function(tabId) {
68+
vAPI.isBehindTheSceneTabId = function(tabId) {
6969
return tabId.toString() === '-1';
7070
};
7171

platform/firefox/vapi-background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ var tabWatcher = {
333333

334334
/******************************************************************************/
335335

336-
vAPI.isNoTabId = function(tabId) {
336+
vAPI.isBehindTheSceneTabId = function(tabId) {
337337
return tabId.toString() === '-1';
338338
};
339339

platform/safari/vapi-background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239

240240
/******************************************************************************/
241241

242-
vAPI.isNoTabId = function(tabId) {
242+
vAPI.isBehindTheSceneTabId = function(tabId) {
243243
return tabId.toString() === this.noTabId;
244244
};
245245

src/_locales/en/messages.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@
7676
"description":"English: Go to request log"
7777
},
7878
"popupTipNoPopups":{
79-
"message":"No popups for this site",
80-
"description":"English: No popups for this site"
79+
"message":"Toggle the blocking of all popups for this site",
80+
"description":"English: Toggle the blocking of all popups for this site"
8181
},
8282
"popupTipNoStrictBlocking":{
83-
"message":"No strict blocking for this site",
84-
"description":"English: No strict blocking for this site"
83+
"message":"Toggle strict blocking for this site",
84+
"description":"English: Toggle strict blocking for this site"
85+
},
86+
"popupTipNoCosmeticFiltering":{
87+
"message":"Toggle cosmetic filtering for this site",
88+
"description":"English: Toggle cosmetic filtering for this site"
8589
},
8690
"popupAnyRulePrompt":{
8791
"message":"all",

src/css/popup.css

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,14 @@ body.off #switch .fa {
148148
margin: 0 0.5em;
149149
position: relative;
150150
}
151-
#extraTools > span.on > span {
151+
#extraTools > span > span.badge {
152+
color: #000;
153+
bottom: -2px;
154+
font: 10px sans-serif;
155+
left: 100%;
156+
position: absolute;
157+
}
158+
#extraTools > span.on > span:last-of-type {
152159
color: #e00;
153160
font-size: 20px;
154161
left: 0;
@@ -157,7 +164,7 @@ body.off #switch .fa {
157164
top: 0;
158165
width: 100%;
159166
}
160-
#extraTools > span.on > span:after {
167+
#extraTools > span.on > span:last-of-type:after {
161168
content: '\2715';
162169
}
163170

@@ -176,7 +183,7 @@ body.advancedUser #panes.dfEnabled h2:before {
176183
background-color: #ffe;
177184
border: 1px solid #eec;
178185
border-radius: 4px;
179-
bottom: 4px;
186+
bottom: 0.7em;
180187
color: #888;
181188
cursor: pointer;
182189
display: none;

src/js/async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ return asyncJobManager;
187187
};
188188

189189
var updateBadgeAsync = function(tabId) {
190-
if ( vAPI.isNoTabId(tabId) ) {
190+
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
191191
return;
192192
}
193193
µb.asyncJobs.add('updateBadge-' + tabId, tabId, updateBadge, 250);

src/js/contentscript-end.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ if ( vAPI.contentscriptEndInjected ) {
5555
return;
5656
}
5757
vAPI.contentscriptEndInjected = true;
58+
vAPI.styles = vAPI.styles || [];
5859

5960
/******************************************************************************/
6061

@@ -326,12 +327,12 @@ var uBlockCollapser = (function() {
326327
nextRetrieveHandler(response);
327328
};
328329

329-
var nextRetrieveHandler = function(selectors) {
330+
var nextRetrieveHandler = function(response) {
330331
//var tStart = timer.now();
331332
//console.debug('µBlock> contextNodes = %o', contextNodes);
332333
var hideSelectors = [];
333-
if ( selectors && selectors.hide.length ) {
334-
processLowGenerics(selectors.hide, hideSelectors);
334+
if ( response && response.hide.length ) {
335+
processLowGenerics(response.hide, hideSelectors);
335336
}
336337
if ( highGenerics ) {
337338
if ( highGenerics.hideLowCount ) {
@@ -360,14 +361,15 @@ var uBlockCollapser = (function() {
360361

361362
var addStyleTag = function(selectors) {
362363
var selectorStr = selectors.toString();
363-
hideElements(selectorStr);
364364
var style = document.createElement('style');
365365
// The linefeed before the style block is very important: do no remove!
366366
style.appendChild(document.createTextNode(selectorStr + '\n{display:none !important;}'));
367367
var parent = document.body || document.documentElement;
368368
if ( parent ) {
369369
parent.appendChild(style);
370+
vAPI.styles.push(style);
370371
}
372+
hideElements(selectorStr);
371373
messager.send({
372374
what: 'injectedSelectors',
373375
type: 'cosmetic',

src/js/contentscript-start.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ if ( vAPI.contentscriptStartInjected ) {
5353
return;
5454
}
5555
vAPI.contentscriptStartInjected = true;
56+
vAPI.styles = vAPI.styles || [];
5657

5758
/******************************************************************************/
5859

@@ -91,15 +92,16 @@ var cosmeticFilters = function(details) {
9192
}
9293
if ( hide.length !== 0 ) {
9394
var text = hide.join(',\n');
94-
hideElements(text);
9595
var style = vAPI.specificHideStyle = document.createElement('style');
9696
// The linefeed before the style block is very important: do not remove!
9797
style.appendChild(document.createTextNode(text + '\n{display:none !important;}'));
9898
//console.debug('µBlock> "%s" cosmetic filters: injecting %d CSS rules:', details.domain, details.hide.length, hideStyleText);
9999
var parent = document.head || document.documentElement;
100100
if ( parent ) {
101101
parent.appendChild(style);
102+
vAPI.styles.push(style);
102103
}
104+
hideElements(text);
103105
}
104106
vAPI.donthideCosmeticFilters = donthideCosmeticFilters;
105107
vAPI.hideCosmeticFilters = hideCosmeticFilters;

src/js/cosmetic-count.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*******************************************************************************
2+
3+
uBlock - a browser extension to block requests.
4+
Copyright (C) 2015 Raymond Hill
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see {http://www.gnu.org/licenses/}.
18+
19+
Home: https://github.com/gorhill/uBlock
20+
*/
21+
22+
/* global vAPI, HTMLDocument */
23+
24+
/******************************************************************************/
25+
26+
(function() {
27+
28+
'use strict';
29+
30+
/******************************************************************************/
31+
32+
// https://github.com/gorhill/uBlock/issues/464
33+
if ( document instanceof HTMLDocument === false ) {
34+
//console.debug('cosmetic-on.js > not a HTLMDocument');
35+
return;
36+
}
37+
38+
// Because in case
39+
if ( !vAPI ) {
40+
//console.debug('cosmetic-on.js > vAPI not found');
41+
return;
42+
}
43+
44+
/******************************************************************************/
45+
46+
// Insert all cosmetic filtering-related style tags in the DOM
47+
48+
var selectors = [];
49+
var reProperties = /\s*\{[^}]+\}\s*/;
50+
var i;
51+
52+
var styles = vAPI.styles || [];
53+
i = styles.length;
54+
while ( i-- ) {
55+
selectors.push(styles[i].textContent.replace(reProperties, ''));
56+
}
57+
58+
var elems = [];
59+
60+
if ( selectors.length !== 0 ) {
61+
try {
62+
elems = document.querySelectorAll(selectors.join(','));
63+
} catch (e) {
64+
}
65+
}
66+
67+
/******************************************************************************/
68+
69+
var localMessager = vAPI.messaging.channel('cosmetic-*.js');
70+
71+
localMessager.send({
72+
what: 'hiddenElementCount',
73+
count: elems.length
74+
}, function() {
75+
localMessager.close();
76+
});
77+
78+
/******************************************************************************/
79+
80+
})();
81+
82+
/******************************************************************************/

src/js/cosmetic-off.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*******************************************************************************
2+
3+
uBlock - a browser extension to block requests.
4+
Copyright (C) 2015 Raymond Hill
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see {http://www.gnu.org/licenses/}.
18+
19+
Home: https://github.com/gorhill/uBlock
20+
*/
21+
22+
/* global vAPI, HTMLDocument */
23+
24+
/******************************************************************************/
25+
26+
(function() {
27+
28+
'use strict';
29+
30+
/******************************************************************************/
31+
32+
// https://github.com/gorhill/uBlock/issues/464
33+
if ( document instanceof HTMLDocument === false ) {
34+
//console.debug('cosmetic-off.js > not a HTLMDocument');
35+
return;
36+
}
37+
38+
// Because in case
39+
if ( !vAPI ) {
40+
//console.debug('cosmetic-off.js > vAPI not found');
41+
return;
42+
}
43+
44+
/******************************************************************************/
45+
46+
var styles = vAPI.styles;
47+
48+
if ( Array.isArray(styles) === false ) {
49+
return;
50+
}
51+
52+
/******************************************************************************/
53+
54+
// Remove all cosmetic filtering-related styles from the DOM
55+
56+
var selectors = [];
57+
var reProperties = /\s*\{[^}]+\}\s*/;
58+
var style, i;
59+
60+
i = styles.length;
61+
while ( i-- ) {
62+
style = styles[i];
63+
if ( style.parentElement === null ) {
64+
continue;
65+
}
66+
style.parentElement.removeChild(style);
67+
selectors.push(style.textContent.replace(reProperties, ''));
68+
}
69+
70+
// Remove `display: none !important` attribute
71+
72+
if ( selectors.length === 0 ) {
73+
return;
74+
}
75+
76+
var elems = [];
77+
try {
78+
elems = document.querySelectorAll(selectors.join(','));
79+
} catch (e) {
80+
}
81+
82+
i = elems.length;
83+
while ( i-- ) {
84+
style = elems[i].style;
85+
if ( typeof style === 'object' || typeof style.removeProperty === 'function' ) {
86+
style.removeProperty('display');
87+
}
88+
}
89+
90+
/******************************************************************************/
91+
92+
})();
93+
94+
/******************************************************************************/

0 commit comments

Comments
 (0)