| Left: | ||
| Right: |
| 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 let emulatedSelectors = []; | 40 function hashSelectors(selectors) |
|
Sebastian Noack
2018/03/06 20:17:39
Shouldn't this rather be a property on the ElemHid
Manish Jethani
2018/03/07 06:23:04
Done.
| |
| 39 | 41 { |
| 40 function haveSelectorsChanged(selectors, oldSelectors) | 42 if (!selectors || selectors.length == 0) |
| 41 { | 43 return 0; |
| 42 if (selectors.length != oldSelectors.length) | 44 return hash(new TextEncoder("utf-8").encode(selectors.join())); |
| 43 return true; | |
| 44 | |
| 45 return !selectors.every((selector, index) => selector == oldSelectors[index]); | |
| 46 } | 45 } |
| 47 | 46 |
| 48 function getURLsFromObjectElement(element) | 47 function getURLsFromObjectElement(element) |
| 49 { | 48 { |
| 50 let url = element.getAttribute("data"); | 49 let url = element.getAttribute("data"); |
| 51 if (url) | 50 if (url) |
| 52 return [url]; | 51 return [url]; |
| 53 | 52 |
| 54 for (let child of element.children) | 53 for (let child of element.children) |
| 55 { | 54 { |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 350 }; | 349 }; |
| 351 | 350 |
| 352 function ElemHide() | 351 function ElemHide() |
| 353 { | 352 { |
| 354 this.shadow = this.createShadowTree(); | 353 this.shadow = this.createShadowTree(); |
| 355 this.styles = new Map(); | 354 this.styles = new Map(); |
| 356 this.tracer = null; | 355 this.tracer = null; |
| 357 this.inline = true; | 356 this.inline = true; |
| 358 this.inlineEmulated = true; | 357 this.inlineEmulated = true; |
| 359 this.emulatedPatterns = null; | 358 this.emulatedPatterns = null; |
| 359 this.digests = new Map(); | |
| 360 | 360 |
| 361 this.elemHideEmulation = new ElemHideEmulation( | 361 this.elemHideEmulation = new ElemHideEmulation( |
| 362 this.addSelectors.bind(this), | 362 this.addSelectors.bind(this), |
| 363 this.hideElements.bind(this) | 363 this.hideElements.bind(this) |
| 364 ); | 364 ); |
| 365 } | 365 } |
| 366 ElemHide.prototype = { | 366 ElemHide.prototype = { |
| 367 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 }, | |
| 368 | 377 |
| 369 createShadowTree() | 378 createShadowTree() |
| 370 { | 379 { |
| 371 // 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 |
| 372 // 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 |
| 373 // 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 |
| 374 // the shadow root before transistions might start (#452). | 383 // the shadow root before transistions might start (#452). |
| 375 if (!("createShadowRoot" in document.documentElement)) | 384 if (!("createShadowRoot" in document.documentElement)) |
| 376 return null; | 385 return null; |
| 377 | 386 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 457 let selector = preparedSelectors.slice( | 466 let selector = preparedSelectors.slice( |
| 458 i, i + this.selectorGroupSize | 467 i, i + this.selectorGroupSize |
| 459 ).join(", "); | 468 ).join(", "); |
| 460 style.sheet.insertRule(selector + "{display: none !important;}", | 469 style.sheet.insertRule(selector + "{display: none !important;}", |
| 461 style.sheet.cssRules.length); | 470 style.sheet.cssRules.length); |
| 462 } | 471 } |
| 463 }, | 472 }, |
| 464 | 473 |
| 465 addSelectors(selectors, filters) | 474 addSelectors(selectors, filters) |
| 466 { | 475 { |
| 467 if (!haveSelectorsChanged(selectors, emulatedSelectors)) | 476 if (!this.selectorsChanged(selectors, "emulated")) |
|
Manish Jethani
2018/03/07 16:03:22
This will go well with the collapsing selectors:
| |
| 468 return; | 477 return; |
| 469 | |
| 470 emulatedSelectors = selectors; | |
| 471 | 478 |
| 472 if (this.inline || this.inlineEmulated) | 479 if (this.inline || this.inlineEmulated) |
| 473 { | 480 { |
| 474 // 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 |
| 475 // 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 |
| 476 // that do support user stylesheets via the browser.tabs.insertCSS API | 483 // that do support user stylesheets via the browser.tabs.insertCSS API |
| 477 // (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). |
| 478 // Once all supported platforms have implemented this API, we can remove | 485 // Once all supported platforms have implemented this API, we can remove |
| 479 // the code below. See issue #5090. | 486 // the code below. See issue #5090. |
| 480 // Related Chrome and Firefox issues: | 487 // Related Chrome and Firefox issues: |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 552 let element = event.target; | 559 let element = event.target; |
| 553 if (/^i?frame$/.test(element.localName)) | 560 if (/^i?frame$/.test(element.localName)) |
| 554 checkCollapse(element); | 561 checkCollapse(element); |
| 555 }, true); | 562 }, true); |
| 556 } | 563 } |
| 557 | 564 |
| 558 window.checkCollapse = checkCollapse; | 565 window.checkCollapse = checkCollapse; |
| 559 window.elemhide = elemhide; | 566 window.elemhide = elemhide; |
| 560 window.typeMap = typeMap; | 567 window.typeMap = typeMap; |
| 561 window.getURLsFromElement = getURLsFromElement; | 568 window.getURLsFromElement = getURLsFromElement; |
| LEFT | RIGHT |