LEFT | RIGHT |
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 let {splitSelector} = require("common"); | 20 let {splitSelector} = require("common"); |
21 let {ElemHideEmulation} = require("content_elemHideEmulation"); | 21 let {ElemHideEmulation} = require("content_elemHideEmulation"); |
22 | 22 |
| 23 const {hash} = require("hash"); |
| 24 |
23 // This variable is also used by our other content scripts. | 25 // This variable is also used by our other content scripts. |
24 let elemhide; | 26 let elemhide; |
25 | 27 |
26 const typeMap = new Map([ | 28 const typeMap = new Map([ |
27 ["img", "IMAGE"], | 29 ["img", "IMAGE"], |
28 ["input", "IMAGE"], | 30 ["input", "IMAGE"], |
29 ["picture", "IMAGE"], | 31 ["picture", "IMAGE"], |
30 ["audio", "MEDIA"], | 32 ["audio", "MEDIA"], |
31 ["video", "MEDIA"], | 33 ["video", "MEDIA"], |
32 ["frame", "SUBDOCUMENT"], | 34 ["frame", "SUBDOCUMENT"], |
33 ["iframe", "SUBDOCUMENT"], | 35 ["iframe", "SUBDOCUMENT"], |
34 ["object", "OBJECT"], | 36 ["object", "OBJECT"], |
35 ["embed", "OBJECT"] | 37 ["embed", "OBJECT"] |
36 ]); | 38 ]); |
37 | 39 |
38 function haveSelectorsChanged(selectors, oldSelectors) | 40 function hashSelectors(selectors) |
39 { | 41 { |
40 if (selectors.length != oldSelectors.length) | 42 if (!selectors || selectors.length == 0) |
41 return true; | 43 return 0; |
42 | 44 return hash(new TextEncoder("utf-8").encode(selectors.join())); |
43 return !selectors.every((selector, index) => selector == oldSelectors[index]); | |
44 } | 45 } |
45 | 46 |
46 function getURLsFromObjectElement(element) | 47 function getURLsFromObjectElement(element) |
47 { | 48 { |
48 let url = element.getAttribute("data"); | 49 let url = element.getAttribute("data"); |
49 if (url) | 50 if (url) |
50 return [url]; | 51 return [url]; |
51 | 52 |
52 for (let child of element.children) | 53 for (let child of element.children) |
53 { | 54 { |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 }; | 349 }; |
349 | 350 |
350 function ElemHide() | 351 function ElemHide() |
351 { | 352 { |
352 this.shadow = this.createShadowTree(); | 353 this.shadow = this.createShadowTree(); |
353 this.styles = new Map(); | 354 this.styles = new Map(); |
354 this.tracer = null; | 355 this.tracer = null; |
355 this.inline = true; | 356 this.inline = true; |
356 this.inlineEmulated = true; | 357 this.inlineEmulated = true; |
357 this.emulatedPatterns = null; | 358 this.emulatedPatterns = null; |
358 this.emulatedSelectors = []; | 359 this.digests = new Map(); |
359 | 360 |
360 this.elemHideEmulation = new ElemHideEmulation( | 361 this.elemHideEmulation = new ElemHideEmulation( |
361 this.addSelectors.bind(this), | 362 this.addSelectors.bind(this), |
362 this.hideElements.bind(this) | 363 this.hideElements.bind(this) |
363 ); | 364 ); |
364 } | 365 } |
365 ElemHide.prototype = { | 366 ElemHide.prototype = { |
366 selectorGroupSize: 1024, | 367 selectorGroupSize: 1024, |
| 368 |
| 369 selectorsChanged(selectors, groupName) |
| 370 { |
| 371 let digest = hashSelectors(selectors); |
| 372 if (digest == this.digests.get(groupName)) |
| 373 return false; |
| 374 this.digests.set(groupName, digest); |
| 375 return true; |
| 376 }, |
367 | 377 |
368 createShadowTree() | 378 createShadowTree() |
369 { | 379 { |
370 // Use Shadow DOM if available as to not mess with with web pages that | 380 // Use Shadow DOM if available as to not mess with with web pages that |
371 // rely on the order of their own <style> tags (#309). However, creating | 381 // rely on the order of their own <style> tags (#309). However, creating |
372 // a shadow root breaks running CSS transitions. So we have to create | 382 // a shadow root breaks running CSS transitions. So we have to create |
373 // the shadow root before transistions might start (#452). | 383 // the shadow root before transistions might start (#452). |
374 if (!("createShadowRoot" in document.documentElement)) | 384 if (!("createShadowRoot" in document.documentElement)) |
375 return null; | 385 return null; |
376 | 386 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 let selector = preparedSelectors.slice( | 466 let selector = preparedSelectors.slice( |
457 i, i + this.selectorGroupSize | 467 i, i + this.selectorGroupSize |
458 ).join(", "); | 468 ).join(", "); |
459 style.sheet.insertRule(selector + "{display: none !important;}", | 469 style.sheet.insertRule(selector + "{display: none !important;}", |
460 style.sheet.cssRules.length); | 470 style.sheet.cssRules.length); |
461 } | 471 } |
462 }, | 472 }, |
463 | 473 |
464 addSelectors(selectors, filters) | 474 addSelectors(selectors, filters) |
465 { | 475 { |
466 if (!haveSelectorsChanged(selectors, this.emulatedSelectors)) | 476 if (!this.selectorsChanged(selectors, "emulated")) |
467 return; | 477 return; |
468 | |
469 this.emulatedSelectors = selectors; | |
470 | 478 |
471 if (this.inline || this.inlineEmulated) | 479 if (this.inline || this.inlineEmulated) |
472 { | 480 { |
473 // Insert the style rules inline if we have been instructed by the | 481 // Insert the style rules inline if we have been instructed by the |
474 // background page to do so. This is usually the case, except on platforms | 482 // background page to do so. This is usually the case, except on platforms |
475 // that do support user stylesheets via the browser.tabs.insertCSS API | 483 // that do support user stylesheets via the browser.tabs.insertCSS API |
476 // (Firefox 53 onwards for now and possibly Chrome in the near future). | 484 // (Firefox 53 onwards for now and possibly Chrome in the near future). |
477 // Once all supported platforms have implemented this API, we can remove | 485 // Once all supported platforms have implemented this API, we can remove |
478 // the code below. See issue #5090. | 486 // the code below. See issue #5090. |
479 // Related Chrome and Firefox issues: | 487 // Related Chrome and Firefox issues: |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 let element = event.target; | 559 let element = event.target; |
552 if (/^i?frame$/.test(element.localName)) | 560 if (/^i?frame$/.test(element.localName)) |
553 checkCollapse(element); | 561 checkCollapse(element); |
554 }, true); | 562 }, true); |
555 } | 563 } |
556 | 564 |
557 window.checkCollapse = checkCollapse; | 565 window.checkCollapse = checkCollapse; |
558 window.elemhide = elemhide; | 566 window.elemhide = elemhide; |
559 window.typeMap = typeMap; | 567 window.typeMap = typeMap; |
560 window.getURLsFromElement = getURLsFromElement; | 568 window.getURLsFromElement = getURLsFromElement; |
LEFT | RIGHT |