Skip to content
This repository was archived by the owner on Nov 15, 2017. It is now read-only.

Commit fded043

Browse files
committed
this addresses #276: now filters applied when DOM mutates
1 parent 9a9d112 commit fded043

File tree

4 files changed

+137
-112
lines changed

4 files changed

+137
-112
lines changed

js/abp-hide-filters.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ var FilterParser = function() {
170170
this.hostnames = [];
171171
this.invalid = false;
172172
this.unsupported = false;
173-
this.rePlain = /#[#@]([#.][\w-]+)$/;
173+
this.rePlain = /([#.][\w-]+)$/;
174+
this.rePlainMore = /^[#.][\w-]+[^\w-]/;
174175
};
175176

176177
/******************************************************************************/
@@ -222,6 +223,12 @@ FilterParser.prototype.parse = function(s) {
222223

223224
/******************************************************************************/
224225

226+
FilterParser.prototype.isPlainMore = function() {
227+
return this.rePlainMore.test(this.f);
228+
};
229+
230+
/******************************************************************************/
231+
225232
FilterParser.prototype.extractPlain = function() {
226233
var matches = this.rePlain.exec(this.f);
227234
if ( matches && matches.length === 2 ) {
@@ -237,8 +244,6 @@ var FilterContainer = function() {
237244
this.acceptedCount = 0;
238245
this.rejectedCount = 0;
239246
this.filters = {};
240-
this.rePlain = /^##[#.][\w-]+$/;
241-
this.rePlainMore = /^##[#.][\w-]+[^\w-]/;
242247
};
243248

244249
/******************************************************************************/
@@ -256,6 +261,10 @@ FilterContainer.prototype.reset = function() {
256261
FilterContainer.prototype.add = function(s) {
257262
var parsed = this.filterParser.parse(s);
258263

264+
//if ( s === 'mail.google.com##.nH.adC > .nH > .nH > .u5 > .azN' ) {
265+
// debugger;
266+
//}
267+
259268
if ( parsed.invalid ) {
260269
return false;
261270
}
@@ -308,7 +317,7 @@ FilterContainer.prototype.makeHash = function(filterType, selector, domain) {
308317

309318
FilterContainer.prototype.addPlainFilter = function(parsed) {
310319
// Verify whether the plain selector is followed by extra selector stuff
311-
if ( this.rePlainMore.test(parsed.f) ) {
320+
if ( parsed.isPlainMore() ) {
312321
return this.addPlainMoreFilter(parsed);
313322
}
314323
if ( parsed.hostnames.length ) {

js/contentscript-elemhide.js

Lines changed: 0 additions & 101 deletions
This file was deleted.

js/contentscript.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,120 @@
2121

2222
// Injected into content pages
2323

24+
/******************************************************************************/
25+
26+
// ABP cosmetic filters
27+
28+
var CosmeticFiltering = function() {
29+
this.classSelectors = null;
30+
this.idSelectors = null;
31+
this.classesFromNodeList(document.querySelectorAll('*[class]'));
32+
this.idsFromNodeList(document.querySelectorAll('*[id]'));
33+
this.retrieve();
34+
};
35+
36+
CosmeticFiltering.prototype.retrieve = function() {
37+
var a1 = this.classSelectors !== null ? Object.keys(this.classSelectors) : [];
38+
var a2 = this.idSelectors !== null ? this.idSelectors : [];
39+
var selectors = a1.concat(a2);
40+
if ( selectors.length > 0 ) {
41+
chrome.runtime.sendMessage({
42+
what: 'retrieveABPHideSelectors',
43+
selectors: selectors,
44+
locationURL: window.location.href
45+
}, this.retrieveHandler);
46+
}
47+
this.idSelectors = null;
48+
this.classSelectors = null;
49+
};
50+
51+
CosmeticFiltering.prototype.retrieveHandler = function(selectors) {
52+
if ( !selectors ) {
53+
return;
54+
}
55+
var styleText = [];
56+
if ( selectors.hide.length > 0 ) {
57+
var hideStyleText = '{{hideSelectors}} {display:none;}'
58+
.replace('{{hideSelectors}}', selectors.hide);
59+
styleText.push(hideStyleText);
60+
console.log('HTTPSB> ABP cosmetic filters: injecting CSS rules:', hideStyleText);
61+
}
62+
if ( selectors.donthide.length > 0 ) {
63+
var dontHideStyleText = '{{donthideSelectors}} {display:initial;}'
64+
.replace('{{donthideSelectors}}', selectors.donthide);
65+
styleText.push(donthideStyleText);
66+
console.log('HTTPSB> ABP cosmetic filters: injecting CSS rules:', donthideStyleText);
67+
}
68+
if ( styleText.length > 0 ) {
69+
var style = document.createElement('style');
70+
style.appendChild(document.createTextNode(styleText.join('')));
71+
document.documentElement.appendChild(style);
72+
}
73+
};
74+
75+
CosmeticFiltering.prototype.classesFromNodeList = function(nodes) {
76+
if ( !nodes ) {
77+
return;
78+
}
79+
if ( this.classSelectors === null ) {
80+
this.classSelectors = {};
81+
}
82+
var classNames, className, j;
83+
var i = nodes.length;
84+
while ( i-- ) {
85+
className = nodes[i].className;
86+
if ( typeof className !== 'string' ) {
87+
continue;
88+
}
89+
className = className.trim();
90+
if ( className === '' ) {
91+
continue;
92+
}
93+
if ( className.indexOf(' ') < 0 ) {
94+
this.classSelectors['.' + className] = true;
95+
continue;
96+
}
97+
classNames = className.trim().split(/\s+/);
98+
j = classNames.length;
99+
while ( j-- ) {
100+
className = classNames[j];
101+
if ( className !== '' ) {
102+
this.classSelectors['.' + className] = true;
103+
}
104+
}
105+
}
106+
};
107+
108+
CosmeticFiltering.prototype.idsFromNodeList = function(nodes) {
109+
if ( !nodes ) {
110+
return;
111+
}
112+
if ( this.idSelectors === null ) {
113+
this.idSelectors = [];
114+
}
115+
var i = nodes.length;
116+
while ( i-- ) {
117+
id = nodes[i].id;
118+
if ( typeof id !== 'string' ) {
119+
continue;
120+
}
121+
id = id.trim();
122+
if ( id === '' ) {
123+
continue;
124+
}
125+
this.idSelectors[i] = '#' + id;
126+
}
127+
};
128+
129+
CosmeticFiltering.prototype.allFromNodeList = function(nodes) {
130+
this.classesFromNodeList(nodes);
131+
this.idsFromNodeList(nodes);
132+
};
133+
134+
135+
var adbCosmeticFiltering = new CosmeticFiltering();
136+
137+
/******************************************************************************/
24138
/******************************************************************************/
25139
/*------------[ Unrendered Noscript (because CSP) Workaround ]----------------*/
26140

@@ -50,6 +164,7 @@ var checkScriptBlacklisted = function() {
50164
}, checkScriptBlacklistedHandler);
51165
};
52166

167+
/******************************************************************************/
53168
/******************************************************************************/
54169

55170
var localStorageHandler = function(mustRemove) {
@@ -59,6 +174,7 @@ var localStorageHandler = function(mustRemove) {
59174
}
60175
};
61176

177+
/******************************************************************************/
62178
/******************************************************************************/
63179

64180
var nodesAddedHandler = function(nodeList, summary) {
@@ -114,6 +230,9 @@ var nodesAddedHandler = function(nodeList, summary) {
114230
break;
115231
}
116232
}
233+
234+
// ABP cosmetic filters:
235+
adbCosmeticFiltering.allFromNodeList(nodeList);
117236
};
118237

119238
/******************************************************************************/
@@ -141,6 +260,8 @@ var mutationObservedHandler = function(mutations) {
141260
if ( summary.mustReport ) {
142261
chrome.runtime.sendMessage(summary);
143262
}
263+
264+
adbCosmeticFiltering.retrieve();
144265
};
145266

146267
/******************************************************************************/
@@ -196,6 +317,7 @@ var firstObservationHandler = function() {
196317
chrome.runtime.sendMessage(summary);
197318
};
198319

320+
/******************************************************************************/
199321
/******************************************************************************/
200322

201323
var loadHandler = function() {
@@ -239,3 +361,4 @@ if ( /^https?:\/\/./.test(window.location.href) ) {
239361
}
240362
}
241363

364+
/******************************************************************************/

manifest.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "__MSG_extName__",
44
"short_name": "HTTPSB",
5-
"version": "0.9.4.1",
5+
"version": "0.9.4.2",
66
"description": "__MSG_extShortDesc__",
77
"icons": {
88
"16": "icon_16.png",
@@ -57,12 +57,6 @@
5757
"js": ["js/contentscript-uaspoof.js"],
5858
"run_at": "document_start",
5959
"all_frames": true
60-
},
61-
{
62-
"matches": ["http://*/*", "https://*/*"],
63-
"js": ["js/contentscript-elemhide.js"],
64-
"run_at": "document_end",
65-
"all_frames": true
6660
}
6761
],
6862
"default_locale": "en",

0 commit comments

Comments
 (0)