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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 120 |
121 for (let i = 0; i < urls.length; i++) | 121 for (let i = 0; i < urls.length; i++) |
122 { | 122 { |
123 if (/^(?!https?:)[\w-]+:/i.test(urls[i])) | 123 if (/^(?!https?:)[\w-]+:/i.test(urls[i])) |
124 urls.splice(i--, 1); | 124 urls.splice(i--, 1); |
125 } | 125 } |
126 | 126 |
127 return urls; | 127 return urls; |
128 } | 128 } |
129 | 129 |
| 130 function hideElement(element) |
| 131 { |
| 132 function doHide(element) |
| 133 { |
| 134 let propertyName = "display"; |
| 135 let propertyValue = "none"; |
| 136 if (element.localName == "frame") |
| 137 { |
| 138 propertyName = "visibility"; |
| 139 propertyValue = "hidden"; |
| 140 } |
| 141 |
| 142 if (element.style.getPropertyValue(propertyName) != propertyValue || |
| 143 element.style.getPropertyPriority(propertyName) != "important") |
| 144 element.style.setProperty(propertyName, propertyValue, "important"); |
| 145 } |
| 146 |
| 147 doHide(element); |
| 148 |
| 149 new MutationObserver(doHide).observe( |
| 150 element, { |
| 151 attributes: true, |
| 152 attributeFilter: ["style"] |
| 153 } |
| 154 ); |
| 155 } |
| 156 |
130 function checkCollapse(element) | 157 function checkCollapse(element) |
131 { | 158 { |
132 let mediatype = typeMap.get(element.localName); | 159 let mediatype = typeMap.get(element.localName); |
133 if (!mediatype) | 160 if (!mediatype) |
134 return; | 161 return; |
135 | 162 |
136 let urls = getURLsFromElement(element); | 163 let urls = getURLsFromElement(element); |
137 if (urls.length == 0) | 164 if (urls.length == 0) |
138 return; | 165 return; |
139 | 166 |
140 ext.backgroundPage.sendMessage( | 167 ext.backgroundPage.sendMessage( |
141 { | 168 { |
142 type: "filters.collapse", | 169 type: "filters.collapse", |
143 urls, | 170 urls, |
144 mediatype, | 171 mediatype, |
145 baseURL: document.location.href | 172 baseURL: document.location.href |
146 }, | 173 }, |
147 | 174 |
148 collapse => | 175 collapse => |
149 { | 176 { |
150 function collapseElement() | |
151 { | |
152 let propertyName = "display"; | |
153 let propertyValue = "none"; | |
154 if (element.localName == "frame") | |
155 { | |
156 propertyName = "visibility"; | |
157 propertyValue = "hidden"; | |
158 } | |
159 | |
160 if (element.style.getPropertyValue(propertyName) != propertyValue || | |
161 element.style.getPropertyPriority(propertyName) != "important") | |
162 element.style.setProperty(propertyName, propertyValue, "important"); | |
163 } | |
164 | |
165 if (collapse) | 177 if (collapse) |
166 { | 178 { |
167 collapseElement(); | 179 hideElement(element); |
168 | |
169 new MutationObserver(collapseElement).observe( | |
170 element, { | |
171 attributes: true, | |
172 attributeFilter: ["style"] | |
173 } | |
174 ); | |
175 } | 180 } |
176 } | 181 } |
177 ); | 182 ); |
178 } | 183 } |
179 | 184 |
180 function checkSitekey() | 185 function checkSitekey() |
181 { | 186 { |
182 let attr = document.documentElement.getAttribute("data-adblockkey"); | 187 let attr = document.documentElement.getAttribute("data-adblockkey"); |
183 if (attr) | 188 if (attr) |
184 ext.backgroundPage.sendMessage({type: "filters.addKey", token: attr}); | 189 ext.backgroundPage.sendMessage({type: "filters.addKey", token: attr}); |
(...skipping 18 matching lines...) Expand all Loading... |
203 ElementHidingTracer.prototype = { | 208 ElementHidingTracer.prototype = { |
204 addSelectors(selectors, filters) | 209 addSelectors(selectors, filters) |
205 { | 210 { |
206 if (document.readyState != "loading") | 211 if (document.readyState != "loading") |
207 this.checkNodes([document], selectors, filters); | 212 this.checkNodes([document], selectors, filters); |
208 | 213 |
209 this.selectors.push(...selectors); | 214 this.selectors.push(...selectors); |
210 this.filters.push(...filters); | 215 this.filters.push(...filters); |
211 }, | 216 }, |
212 | 217 |
| 218 reportFilters(filters) |
| 219 { |
| 220 let matchedSelectors = []; |
| 221 for (let filter of filters) |
| 222 matchedSelectors.push(filter.replace(/^.*?##/, "")); |
| 223 |
| 224 ext.backgroundPage.sendMessage({ |
| 225 type: "devtools.traceElemHide", |
| 226 selectors: matchedSelectors |
| 227 }); |
| 228 }, |
| 229 |
213 checkNodes(nodes, selectors, filters) | 230 checkNodes(nodes, selectors, filters) |
214 { | 231 { |
215 let matchedSelectors = []; | 232 let matchedSelectors = []; |
216 | 233 |
217 for (let i = 0; i < selectors.length; i++) | 234 for (let i = 0; i < selectors.length; i++) |
218 { | 235 { |
219 nodes: for (let node of nodes) | 236 nodes: for (let node of nodes) |
220 { | 237 { |
221 let elements = node.querySelectorAll(selectors[i]); | 238 let elements = node.querySelectorAll(selectors[i]); |
222 | 239 |
223 for (let element of elements) | 240 for (let element of elements) |
224 { | 241 { |
225 // Only consider selectors that actually have an effect on the | 242 // Only consider selectors that actually have an effect on the |
226 // computed styles, and aren't overridden by rules with higher | 243 // computed styles, and aren't overridden by rules with higher |
227 // priority, or haven't been circumvented in a different way. | 244 // priority, or haven't been circumvented in a different way. |
228 if (getComputedStyle(element).display == "none") | 245 if (getComputedStyle(element).display == "none") |
229 { | 246 { |
230 matchedSelectors.push(filters[i].replace(/^.*?##/, "")); | 247 matchedSelectors.push(filters[i]); |
231 break nodes; | 248 break nodes; |
232 } | 249 } |
233 } | 250 } |
234 } | 251 } |
235 } | 252 } |
236 | 253 |
237 if (matchedSelectors.length > 0) | 254 if (matchedSelectors.length > 0) |
238 { | 255 this.reportFilters(matchedSelectors); |
239 ext.backgroundPage.sendMessage({ | |
240 type: "devtools.traceElemHide", | |
241 selectors: matchedSelectors | |
242 }); | |
243 } | |
244 }, | 256 }, |
245 | 257 |
246 onTimeout() | 258 onTimeout() |
247 { | 259 { |
248 this.checkNodes(this.changedNodes, this.selectors, this.filters); | 260 this.checkNodes(this.changedNodes, this.selectors, this.filters); |
249 this.changedNodes = []; | 261 this.changedNodes = []; |
250 this.timeout = null; | 262 this.timeout = null; |
251 }, | 263 }, |
252 | 264 |
253 observe(mutations) | 265 observe(mutations) |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 | 435 |
424 this.elemHideEmulation = new ElemHideEmulation( | 436 this.elemHideEmulation = new ElemHideEmulation( |
425 window, | 437 window, |
426 callback => | 438 callback => |
427 { | 439 { |
428 ext.backgroundPage.sendMessage({ | 440 ext.backgroundPage.sendMessage({ |
429 type: "filters.get", | 441 type: "filters.get", |
430 what: "elemhideemulation" | 442 what: "elemhideemulation" |
431 }, callback); | 443 }, callback); |
432 }, | 444 }, |
433 this.addSelectors.bind(this) | 445 this.addSelectors.bind(this), |
| 446 this.hideElements.bind(this) |
434 ); | 447 ); |
435 } | 448 } |
436 ElemHide.prototype = { | 449 ElemHide.prototype = { |
437 selectorGroupSize: 200, | 450 selectorGroupSize: 200, |
438 | 451 |
439 createShadowTree() | 452 createShadowTree() |
440 { | 453 { |
441 // Use Shadow DOM if available as to not mess with with web pages that | 454 // Use Shadow DOM if available as to not mess with with web pages that |
442 // rely on the order of their own <style> tags (#309). However, creating | 455 // rely on the order of their own <style> tags (#309). However, creating |
443 // a shadow root breaks running CSS transitions. So we have to create | 456 // a shadow root breaks running CSS transitions. So we have to create |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 i, i + this.selectorGroupSize | 547 i, i + this.selectorGroupSize |
535 ).join(", "); | 548 ).join(", "); |
536 this.style.sheet.insertRule(selector + "{display: none !important;}", | 549 this.style.sheet.insertRule(selector + "{display: none !important;}", |
537 this.style.sheet.cssRules.length); | 550 this.style.sheet.cssRules.length); |
538 } | 551 } |
539 | 552 |
540 if (this.tracer) | 553 if (this.tracer) |
541 this.tracer.addSelectors(selectors, filters || selectors); | 554 this.tracer.addSelectors(selectors, filters || selectors); |
542 }, | 555 }, |
543 | 556 |
| 557 hideElements(elements, filters) |
| 558 { |
| 559 for (let element of elements) |
| 560 hideElement(element); |
| 561 |
| 562 if (this.tracer) |
| 563 this.tracer.reportFilters(filters); |
| 564 }, |
| 565 |
544 apply() | 566 apply() |
545 { | 567 { |
546 ext.backgroundPage.sendMessage({type: "get-selectors"}, response => | 568 ext.backgroundPage.sendMessage({type: "get-selectors"}, response => |
547 { | 569 { |
548 if (this.tracer) | 570 if (this.tracer) |
549 this.tracer.disconnect(); | 571 this.tracer.disconnect(); |
550 this.tracer = null; | 572 this.tracer = null; |
551 | 573 |
552 if (this.style && this.style.parentElement) | 574 if (this.style && this.style.parentElement) |
553 this.style.parentElement.removeChild(this.style); | 575 this.style.parentElement.removeChild(this.style); |
(...skipping 21 matching lines...) Expand all Loading... |
575 checkCollapse(event.target); | 597 checkCollapse(event.target); |
576 }, true); | 598 }, true); |
577 | 599 |
578 document.addEventListener("load", event => | 600 document.addEventListener("load", event => |
579 { | 601 { |
580 let element = event.target; | 602 let element = event.target; |
581 if (/^i?frame$/.test(element.localName)) | 603 if (/^i?frame$/.test(element.localName)) |
582 checkCollapse(element); | 604 checkCollapse(element); |
583 }, true); | 605 }, true); |
584 } | 606 } |
OLD | NEW |