Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 22 matching lines...) Expand all Loading... | |
33 */ | 33 */ |
34 var filterByKey = Object.create(null); | 34 var filterByKey = Object.create(null); |
35 | 35 |
36 /** | 36 /** |
37 * Lookup table, keys of the filters by filter text | 37 * Lookup table, keys of the filters by filter text |
38 * @type Object | 38 * @type Object |
39 */ | 39 */ |
40 var keyByFilter = Object.create(null); | 40 var keyByFilter = Object.create(null); |
41 | 41 |
42 /** | 42 /** |
43 * Nested lookup table, filter (or false if inactive) by filter text by domain | |
44 * @type Object | |
45 */ | |
46 var filtersByDomain = Object.create(null); | |
47 | |
48 /** | |
49 * Indicates whether we are using (and maintaining) the filtersByDomain lookup. | |
50 * (Will be false for Firefox) | |
51 * @type Boolean | |
52 */ | |
53 var usingFiltersByDomain = !("nsIStyleSheetService" in Ci); | |
54 | |
55 /** | |
43 * Lookup table, keys are known element hiding exceptions | 56 * Lookup table, keys are known element hiding exceptions |
44 * @type Object | 57 * @type Object |
45 */ | 58 */ |
46 var knownExceptions = Object.create(null); | 59 var knownExceptions = Object.create(null); |
47 | 60 |
48 /** | 61 /** |
49 * Lookup table, lists of element hiding exceptions by selector | 62 * Lookup table, lists of element hiding exceptions by selector |
50 * @type Object | 63 * @type Object |
51 */ | 64 */ |
52 var exceptions = Object.create(null); | 65 var exceptions = Object.create(null); |
(...skipping 10 matching lines...) Expand all Loading... | |
63 */ | 76 */ |
64 var ElemHide = exports.ElemHide = | 77 var ElemHide = exports.ElemHide = |
65 { | 78 { |
66 /** | 79 /** |
67 * Indicates whether filters have been added or removed since the last apply() call. | 80 * Indicates whether filters have been added or removed since the last apply() call. |
68 * @type Boolean | 81 * @type Boolean |
69 */ | 82 */ |
70 isDirty: false, | 83 isDirty: false, |
71 | 84 |
72 /** | 85 /** |
73 * Inidicates whether the element hiding stylesheet is currently applied. | 86 * Indicates whether the element hiding stylesheet is currently applied. |
74 * @type Boolean | 87 * @type Boolean |
75 */ | 88 */ |
76 applied: false, | 89 applied: false, |
77 | 90 |
78 /** | 91 /** |
79 * Called on module startup. | 92 * Called on module startup. |
80 */ | 93 */ |
81 init: function() | 94 init: function() |
82 { | 95 { |
83 Prefs.addListener(function(name) | 96 Prefs.addListener(function(name) |
84 { | 97 { |
85 if (name == "enabled") | 98 if (name == "enabled") |
86 ElemHide.apply(); | 99 ElemHide.apply(); |
87 }); | 100 }); |
88 onShutdown.add(() => ElemHide.unapply()); | 101 onShutdown.add(() => ElemHide.unapply()); |
89 | 102 |
90 let styleFile = IO.resolveFilePath(Prefs.data_directory); | 103 let styleFile = IO.resolveFilePath(Prefs.data_directory); |
91 styleFile.append("elemhide.css"); | 104 styleFile.append("elemhide.css"); |
92 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); | 105 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); |
93 }, | 106 }, |
94 | 107 |
95 /** | 108 /** |
96 * Removes all known filters | 109 * Removes all known filters |
97 */ | 110 */ |
98 clear: function() | 111 clear: function() |
99 { | 112 { |
100 filterByKey = Object.create(null); | 113 filterByKey = Object.create(null); |
101 keyByFilter = Object.create(null); | 114 keyByFilter = Object.create(null); |
115 filtersByDomain = Object.create(null); | |
102 knownExceptions = Object.create(null); | 116 knownExceptions = Object.create(null); |
103 exceptions = Object.create(null); | 117 exceptions = Object.create(null); |
104 ElemHide.isDirty = false; | 118 ElemHide.isDirty = false; |
105 ElemHide.unapply(); | 119 ElemHide.unapply(); |
106 }, | 120 }, |
107 | 121 |
108 /** | 122 /** |
109 * Add a new element hiding filter | 123 * Add a new element hiding filter |
110 * @param {ElemHideFilter} filter | 124 * @param {ElemHideFilter} filter |
111 */ | 125 */ |
(...skipping 15 matching lines...) Expand all Loading... | |
127 if (filter.text in keyByFilter) | 141 if (filter.text in keyByFilter) |
128 return; | 142 return; |
129 | 143 |
130 let key; | 144 let key; |
131 do { | 145 do { |
132 key = Math.random().toFixed(15).substr(5); | 146 key = Math.random().toFixed(15).substr(5); |
133 } while (key in filterByKey); | 147 } while (key in filterByKey); |
134 | 148 |
135 filterByKey[key] = filter; | 149 filterByKey[key] = filter; |
136 keyByFilter[filter.text] = key; | 150 keyByFilter[filter.text] = key; |
151 | |
152 if (usingFiltersByDomain) | |
153 { | |
154 let domainMatches = filter.domains || {"": true}; | |
155 for (let domain in domainMatches) | |
156 { | |
157 domain = domain.toUpperCase(); | |
158 if (!(domain in filtersByDomain)) | |
Sebastian Noack
2016/05/17 09:47:49
I think we should cache the lookup here:
let fi
kzar
2016/05/17 10:05:45
Done.
| |
159 filtersByDomain[domain] = Object.create(null); | |
160 filtersByDomain[domain][filter.text] = domainMatches[domain] && filter ; | |
161 } | |
162 } | |
163 | |
137 ElemHide.isDirty = true; | 164 ElemHide.isDirty = true; |
138 } | 165 } |
139 }, | 166 }, |
140 | 167 |
141 /** | 168 /** |
142 * Removes an element hiding filter | 169 * Removes an element hiding filter |
143 * @param {ElemHideFilter} filter | 170 * @param {ElemHideFilter} filter |
144 */ | 171 */ |
145 remove: function(filter) | 172 remove: function(filter) |
146 { | 173 { |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
361 | 388 |
362 /** | 389 /** |
363 * Retrieves an element hiding filter by the corresponding protocol key | 390 * Retrieves an element hiding filter by the corresponding protocol key |
364 */ | 391 */ |
365 getFilterByKey: function(/**String*/ key) /**Filter*/ | 392 getFilterByKey: function(/**String*/ key) /**Filter*/ |
366 { | 393 { |
367 return (key in filterByKey ? filterByKey[key] : null); | 394 return (key in filterByKey ? filterByKey[key] : null); |
368 }, | 395 }, |
369 | 396 |
370 /** | 397 /** |
371 * Returns a list of all selectors active on a particular domain (currently | 398 * Returns a list of all selectors active on a particular domain, must not be |
372 * used only in Chrome, Opera and Safari). | 399 * used in Firefox (when usingFiltersByDomain is false). |
373 */ | 400 */ |
374 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) | 401 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) |
375 { | 402 { |
376 let result = []; | 403 if (!usingFiltersByDomain) |
377 let keys = Object.getOwnPropertyNames(filterByKey); | 404 throw new Error("getSelectorsForDomain can not be used in Firefox!"); |
378 for (let key of keys) | 405 |
406 let selectors = []; | |
407 | |
408 let seenFilters = Object.create(null); | |
409 let currentDomain = domain ? domain.toUpperCase() : ""; | |
410 while (true) | |
379 { | 411 { |
380 let filter = filterByKey[key]; | 412 if (specificOnly && currentDomain == "") |
381 if (specificOnly && (!filter.domains || filter.domains[""])) | 413 break; |
382 continue; | |
383 | 414 |
384 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) | 415 let filters = filtersByDomain[currentDomain]; |
385 result.push(filter.selector); | 416 if (filters) |
417 { | |
418 for (let filterText in filters) | |
419 { | |
420 if (filterText in seenFilters) | |
Sebastian Noack
2016/05/17 09:47:49
Is this some kind of micro optimization or an atte
kzar
2016/05/17 10:05:45
There's a comment in the old codereview about how
| |
421 continue; | |
422 seenFilters[filterText] = true; | |
423 | |
424 let filter = filters[filterText]; | |
425 if (filter && !this.getException(filter, domain)) | |
Sebastian Noack
2016/05/17 09:47:49
I still have a hard time to understand how/whether
kzar
2016/05/17 10:05:45
For ~example.com##div there should be two entries
| |
426 selectors.push(filter.selector); | |
427 } | |
428 } | |
429 | |
430 if (currentDomain == "") | |
431 break; | |
432 | |
433 let nextDot = currentDomain.indexOf("."); | |
434 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | |
386 } | 435 } |
387 return result; | 436 |
437 return selectors; | |
388 } | 438 } |
389 }; | 439 }; |
OLD | NEW |