| 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 /** |
| 56 * Object to be used instead when a filter has a blank domains property. |
| 57 */ |
| 58 var defaultDomains = Object.create(null); |
| 59 defaultDomains[""] = true; |
| 60 |
| 61 /** |
| 43 * Lookup table, keys are known element hiding exceptions | 62 * Lookup table, keys are known element hiding exceptions |
| 44 * @type Object | 63 * @type Object |
| 45 */ | 64 */ |
| 46 var knownExceptions = Object.create(null); | 65 var knownExceptions = Object.create(null); |
| 47 | 66 |
| 48 /** | 67 /** |
| 49 * Lookup table, lists of element hiding exceptions by selector | 68 * Lookup table, lists of element hiding exceptions by selector |
| 50 * @type Object | 69 * @type Object |
| 51 */ | 70 */ |
| 52 var exceptions = Object.create(null); | 71 var exceptions = Object.create(null); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 63 */ | 82 */ |
| 64 var ElemHide = exports.ElemHide = | 83 var ElemHide = exports.ElemHide = |
| 65 { | 84 { |
| 66 /** | 85 /** |
| 67 * Indicates whether filters have been added or removed since the last apply()
call. | 86 * Indicates whether filters have been added or removed since the last apply()
call. |
| 68 * @type Boolean | 87 * @type Boolean |
| 69 */ | 88 */ |
| 70 isDirty: false, | 89 isDirty: false, |
| 71 | 90 |
| 72 /** | 91 /** |
| 73 * Inidicates whether the element hiding stylesheet is currently applied. | 92 * Indicates whether the element hiding stylesheet is currently applied. |
| 74 * @type Boolean | 93 * @type Boolean |
| 75 */ | 94 */ |
| 76 applied: false, | 95 applied: false, |
| 77 | 96 |
| 78 /** | 97 /** |
| 79 * Called on module startup. | 98 * Called on module startup. |
| 80 */ | 99 */ |
| 81 init: function() | 100 init: function() |
| 82 { | 101 { |
| 83 Prefs.addListener(function(name) | 102 Prefs.addListener(function(name) |
| 84 { | 103 { |
| 85 if (name == "enabled") | 104 if (name == "enabled") |
| 86 ElemHide.apply(); | 105 ElemHide.apply(); |
| 87 }); | 106 }); |
| 88 onShutdown.add(() => ElemHide.unapply()); | 107 onShutdown.add(() => ElemHide.unapply()); |
| 89 | 108 |
| 90 let styleFile = IO.resolveFilePath(Prefs.data_directory); | 109 let styleFile = IO.resolveFilePath(Prefs.data_directory); |
| 91 styleFile.append("elemhide.css"); | 110 styleFile.append("elemhide.css"); |
| 92 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); | 111 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); |
| 93 }, | 112 }, |
| 94 | 113 |
| 95 /** | 114 /** |
| 96 * Removes all known filters | 115 * Removes all known filters |
| 97 */ | 116 */ |
| 98 clear: function() | 117 clear: function() |
| 99 { | 118 { |
| 100 filterByKey = Object.create(null); | 119 filterByKey = Object.create(null); |
| 101 keyByFilter = Object.create(null); | 120 keyByFilter = Object.create(null); |
| 121 filtersByDomain = Object.create(null); |
| 102 knownExceptions = Object.create(null); | 122 knownExceptions = Object.create(null); |
| 103 exceptions = Object.create(null); | 123 exceptions = Object.create(null); |
| 104 ElemHide.isDirty = false; | 124 ElemHide.isDirty = false; |
| 105 ElemHide.unapply(); | 125 ElemHide.unapply(); |
| 106 }, | 126 }, |
| 107 | 127 |
| 108 /** | 128 /** |
| 109 * Add a new element hiding filter | 129 * Add a new element hiding filter |
| 110 * @param {ElemHideFilter} filter | 130 * @param {ElemHideFilter} filter |
| 111 */ | 131 */ |
| (...skipping 15 matching lines...) Expand all Loading... |
| 127 if (filter.text in keyByFilter) | 147 if (filter.text in keyByFilter) |
| 128 return; | 148 return; |
| 129 | 149 |
| 130 let key; | 150 let key; |
| 131 do { | 151 do { |
| 132 key = Math.random().toFixed(15).substr(5); | 152 key = Math.random().toFixed(15).substr(5); |
| 133 } while (key in filterByKey); | 153 } while (key in filterByKey); |
| 134 | 154 |
| 135 filterByKey[key] = filter; | 155 filterByKey[key] = filter; |
| 136 keyByFilter[filter.text] = key; | 156 keyByFilter[filter.text] = key; |
| 157 |
| 158 if (usingFiltersByDomain) |
| 159 { |
| 160 let domainMatches = filter.domains || defaultDomains; |
| 161 for (let domain in domainMatches) |
| 162 { |
| 163 let filters = filtersByDomain[domain]; |
| 164 if (!filters) |
| 165 filters = filtersByDomain[domain] = Object.create(null); |
| 166 |
| 167 if (domainMatches[domain]) |
| 168 filters[filter.text] = filter; |
| 169 else |
| 170 filters[filter.text] = false; |
| 171 } |
| 172 } |
| 173 |
| 137 ElemHide.isDirty = true; | 174 ElemHide.isDirty = true; |
| 138 } | 175 } |
| 139 }, | 176 }, |
| 140 | 177 |
| 141 /** | 178 /** |
| 142 * Removes an element hiding filter | 179 * Removes an element hiding filter |
| 143 * @param {ElemHideFilter} filter | 180 * @param {ElemHideFilter} filter |
| 144 */ | 181 */ |
| 145 remove: function(filter) | 182 remove: function(filter) |
| 146 { | 183 { |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 | 398 |
| 362 /** | 399 /** |
| 363 * Retrieves an element hiding filter by the corresponding protocol key | 400 * Retrieves an element hiding filter by the corresponding protocol key |
| 364 */ | 401 */ |
| 365 getFilterByKey: function(/**String*/ key) /**Filter*/ | 402 getFilterByKey: function(/**String*/ key) /**Filter*/ |
| 366 { | 403 { |
| 367 return (key in filterByKey ? filterByKey[key] : null); | 404 return (key in filterByKey ? filterByKey[key] : null); |
| 368 }, | 405 }, |
| 369 | 406 |
| 370 /** | 407 /** |
| 371 * Returns a list of all selectors active on a particular domain (currently | 408 * Returns a list of all selectors active on a particular domain, must not be |
| 372 * used only in Chrome, Opera and Safari). | 409 * used in Firefox (when usingFiltersByDomain is false). |
| 373 */ | 410 */ |
| 374 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) | 411 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) |
| 375 { | 412 { |
| 376 let result = []; | 413 if (!usingFiltersByDomain) |
| 377 let keys = Object.getOwnPropertyNames(filterByKey); | 414 throw new Error("getSelectorsForDomain can not be used in Firefox!"); |
| 378 for (let key of keys) | 415 |
| 416 let selectors = []; |
| 417 |
| 418 let seenFilters = Object.create(null); |
| 419 let currentDomain = domain ? domain.toUpperCase() : ""; |
| 420 while (true) |
| 379 { | 421 { |
| 380 let filter = filterByKey[key]; | 422 if (specificOnly && currentDomain == "") |
| 381 if (specificOnly && (!filter.domains || filter.domains[""])) | 423 break; |
| 382 continue; | |
| 383 | 424 |
| 384 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) | 425 let filters = filtersByDomain[currentDomain]; |
| 385 result.push(filter.selector); | 426 if (filters) |
| 427 { |
| 428 for (let filterText in filters) |
| 429 { |
| 430 if (filterText in seenFilters) |
| 431 continue; |
| 432 seenFilters[filterText] = true; |
| 433 |
| 434 let filter = filters[filterText]; |
| 435 if (filter && !this.getException(filter, domain)) |
| 436 selectors.push(filter.selector); |
| 437 } |
| 438 } |
| 439 |
| 440 if (currentDomain == "") |
| 441 break; |
| 442 |
| 443 let nextDot = currentDomain.indexOf("."); |
| 444 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
| 386 } | 445 } |
| 387 return result; | 446 |
| 447 return selectors; |
| 388 } | 448 } |
| 389 }; | 449 }; |
| OLD | NEW |