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-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 |
| 40 function hashSelectors(selectors) |
| 41 { |
| 42 if (!selectors || selectors.length == 0) |
| 43 return 0; |
| 44 return hash(new TextEncoder("utf-8").encode(selectors.join())); |
| 45 } |
| 46 |
38 function getURLsFromObjectElement(element) | 47 function getURLsFromObjectElement(element) |
39 { | 48 { |
40 let url = element.getAttribute("data"); | 49 let url = element.getAttribute("data"); |
41 if (url) | 50 if (url) |
42 return [url]; | 51 return [url]; |
43 | 52 |
44 for (let child of element.children) | 53 for (let child of element.children) |
45 { | 54 { |
46 if (child.localName != "param") | 55 if (child.localName != "param") |
47 continue; | 56 continue; |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 }; | 349 }; |
341 | 350 |
342 function ElemHide() | 351 function ElemHide() |
343 { | 352 { |
344 this.shadow = this.createShadowTree(); | 353 this.shadow = this.createShadowTree(); |
345 this.styles = new Map(); | 354 this.styles = new Map(); |
346 this.tracer = null; | 355 this.tracer = null; |
347 this.inline = true; | 356 this.inline = true; |
348 this.inlineEmulated = true; | 357 this.inlineEmulated = true; |
349 this.emulatedPatterns = null; | 358 this.emulatedPatterns = null; |
| 359 this.digests = new Map(); |
350 | 360 |
351 this.elemHideEmulation = new ElemHideEmulation( | 361 this.elemHideEmulation = new ElemHideEmulation( |
352 this.addSelectors.bind(this), | 362 this.addSelectors.bind(this), |
353 this.hideElements.bind(this) | 363 this.hideElements.bind(this) |
354 ); | 364 ); |
355 } | 365 } |
356 ElemHide.prototype = { | 366 ElemHide.prototype = { |
357 selectorGroupSize: 1024, | 367 selectorGroupSize: 1024, |
358 | 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 }, |
| 377 |
359 createShadowTree() | 378 createShadowTree() |
360 { | 379 { |
361 // 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 |
362 // 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 |
363 // 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 |
364 // the shadow root before transistions might start (#452). | 383 // the shadow root before transistions might start (#452). |
365 if (!("createShadowRoot" in document.documentElement)) | 384 if (!("createShadowRoot" in document.documentElement)) |
366 return null; | 385 return null; |
367 | 386 |
368 // Using shadow DOM causes issues on some Google websites, | 387 // Using shadow DOM causes issues on some Google websites, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 let selector = preparedSelectors.slice( | 466 let selector = preparedSelectors.slice( |
448 i, i + this.selectorGroupSize | 467 i, i + this.selectorGroupSize |
449 ).join(", "); | 468 ).join(", "); |
450 style.sheet.insertRule(selector + "{display: none !important;}", | 469 style.sheet.insertRule(selector + "{display: none !important;}", |
451 style.sheet.cssRules.length); | 470 style.sheet.cssRules.length); |
452 } | 471 } |
453 }, | 472 }, |
454 | 473 |
455 addSelectors(selectors, filters) | 474 addSelectors(selectors, filters) |
456 { | 475 { |
| 476 if (!this.selectorsChanged(selectors, "emulated")) |
| 477 return; |
| 478 |
457 if (this.inline || this.inlineEmulated) | 479 if (this.inline || this.inlineEmulated) |
458 { | 480 { |
459 // 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 |
460 // 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 |
461 // that do support user stylesheets via the browser.tabs.insertCSS API | 483 // that do support user stylesheets via the browser.tabs.insertCSS API |
462 // (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). |
463 // Once all supported platforms have implemented this API, we can remove | 485 // Once all supported platforms have implemented this API, we can remove |
464 // the code below. See issue #5090. | 486 // the code below. See issue #5090. |
465 // Related Chrome and Firefox issues: | 487 // Related Chrome and Firefox issues: |
466 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 | 488 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 let element = event.target; | 559 let element = event.target; |
538 if (/^i?frame$/.test(element.localName)) | 560 if (/^i?frame$/.test(element.localName)) |
539 checkCollapse(element); | 561 checkCollapse(element); |
540 }, true); | 562 }, true); |
541 } | 563 } |
542 | 564 |
543 window.checkCollapse = checkCollapse; | 565 window.checkCollapse = checkCollapse; |
544 window.elemhide = elemhide; | 566 window.elemhide = elemhide; |
545 window.typeMap = typeMap; | 567 window.typeMap = typeMap; |
546 window.getURLsFromElement = getURLsFromElement; | 568 window.getURLsFromElement = getURLsFromElement; |
OLD | NEW |