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 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 let thisShadow = shadowRoot(this); | 475 let thisShadow = shadowRoot(this); |
476 return thisShadow == ourShadowRoot ? null : thisShadow; | 476 return thisShadow == ourShadowRoot ? null : thisShadow; |
477 } | 477 } |
478 }); | 478 }); |
479 }, null); | 479 }, null); |
480 } | 480 } |
481 | 481 |
482 return shadow; | 482 return shadow; |
483 }, | 483 }, |
484 | 484 |
| 485 _insertRuleForSelectors(selectors) |
| 486 { |
| 487 const properties = " {display: none !important;}"; |
| 488 |
| 489 try |
| 490 { |
| 491 this.style.sheet.insertRule( |
| 492 selectors.join(", ") + properties, |
| 493 this.style.sheet.cssRules.length |
| 494 ); |
| 495 } |
| 496 catch (batchInsertException) |
| 497 { |
| 498 // There is a syntax error in one or more of the selectors. Rather than |
| 499 // dropping all of them let's add them invdividually, so that only the |
| 500 // broken selectors are skipped. |
| 501 for (let selector of selectors) |
| 502 { |
| 503 try |
| 504 { |
| 505 this.style.sheet.insertRule( |
| 506 selector + properties, |
| 507 this.style.sheet.cssRules.length |
| 508 ); |
| 509 } |
| 510 catch (individualInsertException) {} |
| 511 } |
| 512 } |
| 513 }, |
| 514 |
485 addSelectors(selectors, filters) | 515 addSelectors(selectors, filters) |
486 { | 516 { |
487 if (selectors.length == 0) | 517 if (selectors.length == 0) |
488 return; | 518 return; |
489 | 519 |
490 if (!this.style) | 520 if (!this.style) |
491 { | 521 { |
492 // Create <style> element lazily, only if we add styles. Add it to | 522 // Create <style> element lazily, only if we add styles. Add it to |
493 // the shadow DOM if possible. Otherwise fallback to the <head> or | 523 // the shadow DOM if possible. Otherwise fallback to the <head> or |
494 // <html> element. If we have injected a style element before that | 524 // <html> element. If we have injected a style element before that |
(...skipping 26 matching lines...) Expand all Loading... |
521 else | 551 else |
522 preparedSelectors = selectors; | 552 preparedSelectors = selectors; |
523 | 553 |
524 // Safari only allows 8192 primitive selectors to be injected at once[1], we | 554 // Safari only allows 8192 primitive selectors to be injected at once[1], we |
525 // therefore chunk the inserted selectors into groups of 200 to be safe. | 555 // therefore chunk the inserted selectors into groups of 200 to be safe. |
526 // (Chrome also has a limit, larger... but we're not certain exactly what it | 556 // (Chrome also has a limit, larger... but we're not certain exactly what it |
527 // is! Edge apparently has no such limit.) | 557 // is! Edge apparently has no such limit.) |
528 // [1] - https://github.com/WebKit/webkit/blob/1cb2227f6b2a1035f7bdc46e5ab69
debb75fc1de/Source/WebCore/css/RuleSet.h#L68 | 558 // [1] - https://github.com/WebKit/webkit/blob/1cb2227f6b2a1035f7bdc46e5ab69
debb75fc1de/Source/WebCore/css/RuleSet.h#L68 |
529 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) | 559 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) |
530 { | 560 { |
531 let selector = preparedSelectors.slice( | 561 this._insertRuleForSelectors( |
532 i, i + this.selectorGroupSize | 562 preparedSelectors.slice(i, i + this.selectorGroupSize) |
533 ).join(", "); | 563 ); |
534 this.style.sheet.insertRule(selector + "{display: none !important;}", | |
535 this.style.sheet.cssRules.length); | |
536 } | 564 } |
537 | 565 |
538 if (this.tracer) | 566 if (this.tracer) |
539 this.tracer.addSelectors(selectors, filters || selectors); | 567 this.tracer.addSelectors(selectors, filters || selectors); |
540 }, | 568 }, |
541 | 569 |
542 apply() | 570 apply() |
543 { | 571 { |
544 ext.backgroundPage.sendMessage({type: "get-selectors"}, response => | 572 ext.backgroundPage.sendMessage({type: "get-selectors"}, response => |
545 { | 573 { |
(...skipping 27 matching lines...) Expand all Loading... |
573 checkCollapse(event.target); | 601 checkCollapse(event.target); |
574 }, true); | 602 }, true); |
575 | 603 |
576 document.addEventListener("load", event => | 604 document.addEventListener("load", event => |
577 { | 605 { |
578 let element = event.target; | 606 let element = event.target; |
579 if (/^i?frame$/.test(element.localName)) | 607 if (/^i?frame$/.test(element.localName)) |
580 checkCollapse(element); | 608 checkCollapse(element); |
581 }, true); | 609 }, true); |
582 } | 610 } |
OLD | NEW |