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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 function checkSitekey() | 180 function checkSitekey() |
181 { | 181 { |
182 let attr = document.documentElement.getAttribute("data-adblockkey"); | 182 let attr = document.documentElement.getAttribute("data-adblockkey"); |
183 if (attr) | 183 if (attr) |
184 ext.backgroundPage.sendMessage({type: "filters.addKey", token: attr}); | 184 ext.backgroundPage.sendMessage({type: "filters.addKey", token: attr}); |
185 } | 185 } |
186 | 186 |
187 function ElementHidingTracer() | 187 function ElementHidingTracer() |
188 { | 188 { |
189 this.selectors = []; | 189 this.selectors = []; |
190 this.filters = []; | |
191 | |
192 this.changedNodes = []; | 190 this.changedNodes = []; |
193 this.timeout = null; | 191 this.timeout = null; |
194 | |
195 this.observer = new MutationObserver(this.observe.bind(this)); | 192 this.observer = new MutationObserver(this.observe.bind(this)); |
196 this.trace = this.trace.bind(this); | 193 this.trace = this.trace.bind(this); |
197 | 194 |
198 if (document.readyState == "loading") | 195 if (document.readyState == "loading") |
199 document.addEventListener("DOMContentLoaded", this.trace); | 196 document.addEventListener("DOMContentLoaded", this.trace); |
200 else | 197 else |
201 this.trace(); | 198 this.trace(); |
202 } | 199 } |
203 ElementHidingTracer.prototype = { | 200 ElementHidingTracer.prototype = { |
204 addSelectors(selectors, filters) | 201 addSelectors(selectors, filters) |
205 { | 202 { |
| 203 let pairs = selectors.map((sel, i) => [sel, filters && filters[i]]); |
| 204 |
206 if (document.readyState != "loading") | 205 if (document.readyState != "loading") |
207 this.checkNodes([document], selectors, filters); | 206 this.checkNodes([document], pairs); |
208 | 207 |
209 this.selectors.push(...selectors); | 208 this.selectors.push(...pairs); |
210 this.filters.push(...filters); | |
211 }, | 209 }, |
212 | 210 |
213 checkNodes(nodes, selectors, filters) | 211 checkNodes(nodes, pairs) |
214 { | 212 { |
215 let matchedSelectors = []; | 213 let selectors = []; |
| 214 let filters = []; |
216 | 215 |
217 for (let i = 0; i < selectors.length; i++) | 216 for (let [selector, filter] of pairs) |
218 { | 217 { |
219 nodes: for (let node of nodes) | 218 nodes: for (let node of nodes) |
220 { | 219 { |
221 let elements = node.querySelectorAll(selectors[i]); | 220 for (let element of node.querySelectorAll(selector)) |
222 | |
223 for (let element of elements) | |
224 { | 221 { |
225 // Only consider selectors that actually have an effect on the | 222 // Only consider selectors that actually have an effect on the |
226 // computed styles, and aren't overridden by rules with higher | 223 // computed styles, and aren't overridden by rules with higher |
227 // priority, or haven't been circumvented in a different way. | 224 // priority, or haven't been circumvented in a different way. |
228 if (getComputedStyle(element).display == "none") | 225 if (getComputedStyle(element).display == "none") |
229 { | 226 { |
230 matchedSelectors.push(filters[i].replace(/^.*?##/, "")); | 227 // For regular element hiding, we don't know the exact filter, |
| 228 // but the background page can find it with the given selector. |
| 229 // In case of element hiding emulation, the generated selector |
| 230 // we got here is different from the selector part of the filter, |
| 231 // but in this case we can send the whole filter text instead. |
| 232 if (filter) |
| 233 filters.push(filter); |
| 234 else |
| 235 selectors.push(selector); |
| 236 |
231 break nodes; | 237 break nodes; |
232 } | 238 } |
233 } | 239 } |
234 } | 240 } |
235 } | 241 } |
236 | 242 |
237 if (matchedSelectors.length > 0) | 243 if (selectors.length > 0 || filters.length > 0) |
238 { | 244 { |
239 ext.backgroundPage.sendMessage({ | 245 ext.backgroundPage.sendMessage({ |
240 type: "devtools.traceElemHide", | 246 type: "devtools.traceElemHide", |
241 selectors: matchedSelectors | 247 selectors, filters |
242 }); | 248 }); |
243 } | 249 } |
244 }, | 250 }, |
245 | 251 |
246 onTimeout() | 252 onTimeout() |
247 { | 253 { |
248 this.checkNodes(this.changedNodes, this.selectors, this.filters); | 254 this.checkNodes(this.changedNodes, this.selectors); |
249 this.changedNodes = []; | 255 this.changedNodes = []; |
250 this.timeout = null; | 256 this.timeout = null; |
251 }, | 257 }, |
252 | 258 |
253 observe(mutations) | 259 observe(mutations) |
254 { | 260 { |
255 // Forget previously changed nodes that are no longer in the DOM. | 261 // Forget previously changed nodes that are no longer in the DOM. |
256 for (let i = 0; i < this.changedNodes.length; i++) | 262 for (let i = 0; i < this.changedNodes.length; i++) |
257 { | 263 { |
258 if (!document.contains(this.changedNodes[i])) | 264 if (!document.contains(this.changedNodes[i])) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 | 306 |
301 // Check only nodes whose descendants have changed, and not more often | 307 // Check only nodes whose descendants have changed, and not more often |
302 // than once a second. Otherwise large pages with a lot of DOM mutations | 308 // than once a second. Otherwise large pages with a lot of DOM mutations |
303 // (like YouTube) freeze when the devtools panel is active. | 309 // (like YouTube) freeze when the devtools panel is active. |
304 if (this.timeout == null) | 310 if (this.timeout == null) |
305 this.timeout = setTimeout(this.onTimeout.bind(this), 1000); | 311 this.timeout = setTimeout(this.onTimeout.bind(this), 1000); |
306 }, | 312 }, |
307 | 313 |
308 trace() | 314 trace() |
309 { | 315 { |
310 this.checkNodes([document], this.selectors, this.filters); | 316 this.checkNodes([document], this.selectors); |
311 | 317 |
312 this.observer.observe( | 318 this.observer.observe( |
313 document, | 319 document, |
314 { | 320 { |
315 childList: true, | 321 childList: true, |
316 attributes: true, | 322 attributes: true, |
317 subtree: true | 323 subtree: true |
318 } | 324 } |
319 ); | 325 ); |
320 }, | 326 }, |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) | 537 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) |
532 { | 538 { |
533 let selector = preparedSelectors.slice( | 539 let selector = preparedSelectors.slice( |
534 i, i + this.selectorGroupSize | 540 i, i + this.selectorGroupSize |
535 ).join(", "); | 541 ).join(", "); |
536 this.style.sheet.insertRule(selector + "{display: none !important;}", | 542 this.style.sheet.insertRule(selector + "{display: none !important;}", |
537 this.style.sheet.cssRules.length); | 543 this.style.sheet.cssRules.length); |
538 } | 544 } |
539 | 545 |
540 if (this.tracer) | 546 if (this.tracer) |
541 this.tracer.addSelectors(selectors, filters || selectors); | 547 this.tracer.addSelectors(selectors, filters); |
542 }, | 548 }, |
543 | 549 |
544 apply() | 550 apply() |
545 { | 551 { |
546 ext.backgroundPage.sendMessage({type: "get-selectors"}, response => | 552 ext.backgroundPage.sendMessage({type: "get-selectors"}, response => |
547 { | 553 { |
548 if (this.tracer) | 554 if (this.tracer) |
549 this.tracer.disconnect(); | 555 this.tracer.disconnect(); |
550 this.tracer = null; | 556 this.tracer = null; |
551 | 557 |
(...skipping 23 matching lines...) Expand all Loading... |
575 checkCollapse(event.target); | 581 checkCollapse(event.target); |
576 }, true); | 582 }, true); |
577 | 583 |
578 document.addEventListener("load", event => | 584 document.addEventListener("load", event => |
579 { | 585 { |
580 let element = event.target; | 586 let element = event.target; |
581 if (/^i?frame$/.test(element.localName)) | 587 if (/^i?frame$/.test(element.localName)) |
582 checkCollapse(element); | 588 checkCollapse(element); |
583 }, true); | 589 }, true); |
584 } | 590 } |
OLD | NEW |