Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: include.preload.js

Issue 29670575: Issue 5899 - Use CSS attribute selectors for collapsing media elements (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Use CSS attribute selectors for collapsing Created Feb. 22, 2018, 6:49 a.m.
Right Patch Set: Compare child name directly Created April 19, 2018, 9:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | lib/cssInjection.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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("./adblockpluscore/lib/common");
21 let {ElemHideEmulation} = require("content_elemHideEmulation"); 21 let {ElemHideEmulation} =
22 require("./adblockpluscore/lib/content/elemHideEmulation");
22 23
23 // This variable is also used by our other content scripts. 24 // This variable is also used by our other content scripts.
24 let elemhide; 25 let elemhide;
25 26
26 const typeMap = new Map([ 27 const typeMap = new Map([
27 ["img", "IMAGE"], 28 ["img", "IMAGE"],
28 ["input", "IMAGE"], 29 ["input", "IMAGE"],
29 ["picture", "IMAGE"], 30 ["picture", "IMAGE"],
30 ["audio", "MEDIA"], 31 ["audio", "MEDIA"],
31 ["video", "MEDIA"], 32 ["video", "MEDIA"],
32 ["frame", "SUBDOCUMENT"], 33 ["frame", "SUBDOCUMENT"],
33 ["iframe", "SUBDOCUMENT"], 34 ["iframe", "SUBDOCUMENT"],
34 ["object", "OBJECT"], 35 ["object", "OBJECT"],
35 ["embed", "OBJECT"] 36 ["embed", "OBJECT"]
36 ]); 37 ]);
37 38
38 let collapsingSelectors = new Set(); 39 let collapsingSelectors = new Set();
kzar 2018/02/26 17:18:58 I guess we'll slowly leak memory here, since we ne
Manish Jethani 2018/02/27 13:11:49 I wouldn't call that "leaking memory" since we do
39 40
40 function getURLsFromObjectElement(element) 41 function getURLsFromObjectElement(element)
41 { 42 {
42 let url = element.getAttribute("data"); 43 let url = element.getAttribute("data");
43 if (url) 44 if (url)
44 return [url]; 45 return [url];
45 46
46 for (let child of element.children) 47 for (let child of element.children)
47 { 48 {
48 if (child.localName != "param") 49 if (child.localName != "param")
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 138
138 if (!element.getAttribute("src")) 139 if (!element.getAttribute("src"))
139 return false; 140 return false;
140 141
141 for (let child of element.children) 142 for (let child of element.children)
142 { 143 {
143 // If the <video> or <audio> element contains any <source> or <track> 144 // If the <video> or <audio> element contains any <source> or <track>
144 // children, we cannot address it in CSS by the source URL; in that case we 145 // children, we cannot address it in CSS by the source URL; in that case we
145 // don't "collapse" it using a CSS selector but rather hide it directly by 146 // don't "collapse" it using a CSS selector but rather hide it directly by
146 // setting the style="..." attribute. 147 // setting the style="..." attribute.
147 if (["source", "track"].includes(child.localName)) 148 if (child.localName == "source" || child.localName == "track")
148 return false; 149 return false;
149 } 150 }
150 151
151 return true; 152 return true;
152 } 153 }
153 154
154 function collapseMediaElement(element, srcValue) 155 function collapseMediaElement(element, srcValue)
155 { 156 {
156 let selector = srcValue ? 157 if (!srcValue)
kzar 2018/02/26 17:18:58 Nit: It would be clearer to do this IMO: if (!src
Manish Jethani 2018/02/27 13:11:49 Ah, you're right. Done.
157 element.localName + "[src=" + CSS.escape(srcValue) + "]" : 158 return;
kzar 2018/02/26 17:18:58 Maybe a dumb question but could this end up matchi
Manish Jethani 2018/02/27 13:11:49 No, that's a good question. In isCollapsibleMedia
158 null; 159
160 let selector = element.localName + "[src=" + CSS.escape(srcValue) + "]";
161
159 // Adding selectors is expensive so do it only if we really have a new 162 // Adding selectors is expensive so do it only if we really have a new
160 // selector. 163 // selector.
161 if (selector && !collapsingSelectors.has(selector)) 164 if (!collapsingSelectors.has(selector))
162 { 165 {
163 collapsingSelectors.add(selector); 166 collapsingSelectors.add(selector);
164 elemhide.addSelectors(Array.from(collapsingSelectors), null, "collapsing"); 167 elemhide.addSelectors([selector], null, "collapsing", true);
Manish Jethani 2018/02/22 06:55:06 There's a new "collapsing" group for these selecto
kzar 2018/02/27 14:18:38 Good idea, let's do that now?
Manish Jethani 2018/02/27 16:00:22 Done.
165 } 168 }
166 } 169 }
167 170
168 function hideElement(element) 171 function hideElement(element)
169 { 172 {
170 function doHide() 173 function doHide()
171 { 174 {
172 let propertyName = "display"; 175 let propertyName = "display";
173 let propertyValue = "none"; 176 let propertyValue = "none";
174 if (element.localName == "frame") 177 if (element.localName == "frame")
(...skipping 20 matching lines...) Expand all
195 function checkCollapse(element) 198 function checkCollapse(element)
196 { 199 {
197 let mediatype = typeMap.get(element.localName); 200 let mediatype = typeMap.get(element.localName);
198 if (!mediatype) 201 if (!mediatype)
199 return; 202 return;
200 203
201 let urls = getURLsFromElement(element); 204 let urls = getURLsFromElement(element);
202 if (urls.length == 0) 205 if (urls.length == 0)
203 return; 206 return;
204 207
205 let collapsible = isCollapsibleMediaElement(element, mediatype); 208 let collapsibleMediaElement = isCollapsibleMediaElement(element, mediatype);
kzar 2018/02/26 17:18:58 IMO this variable name is misleading since non-med
Manish Jethani 2018/02/27 13:11:49 Yeah, I suppose you're right. Updated.
206 209
207 // Save the value of the src attribute because it can change between now and 210 // Save the value of the src attribute because it can change between now and
208 // when we get the response from the background page. 211 // when we get the response from the background page.
209 let srcValue = collapsible ? element.getAttribute("src") : null; 212 let srcValue = collapsibleMediaElement ? element.getAttribute("src") : null;
Manish Jethani 2018/02/22 06:55:06 If the pre-roll ad is blocked then pretty quickly
kzar 2018/02/26 17:18:58 Acknowledged.
210 213
211 browser.runtime.sendMessage( 214 browser.runtime.sendMessage(
212 { 215 {
213 type: "filters.collapse", 216 type: "filters.collapse",
214 urls, 217 urls,
215 mediatype, 218 mediatype,
216 baseURL: document.location.href 219 baseURL: document.location.href
217 }, 220 },
218 collapse => 221 collapse =>
219 { 222 {
220 if (collapse) 223 if (collapse)
221 { 224 {
222 if (collapsible) 225 if (collapsibleMediaElement)
223 collapseMediaElement(element, srcValue); 226 collapseMediaElement(element, srcValue);
224 else 227 else
225 hideElement(element); 228 hideElement(element);
226 } 229 }
227 } 230 }
228 ); 231 );
229 } 232 }
230 233
231 function checkSitekey() 234 function checkSitekey()
232 { 235 {
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 } 387 }
385 }; 388 };
386 389
387 function ElemHide() 390 function ElemHide()
388 { 391 {
389 this.shadow = this.createShadowTree(); 392 this.shadow = this.createShadowTree();
390 this.styles = new Map(); 393 this.styles = new Map();
391 this.tracer = null; 394 this.tracer = null;
392 this.inline = true; 395 this.inline = true;
393 this.inlineEmulated = true; 396 this.inlineEmulated = true;
394 this.emulatedPatterns = null;
395 397
396 this.elemHideEmulation = new ElemHideEmulation( 398 this.elemHideEmulation = new ElemHideEmulation(
397 this.addSelectors.bind(this), 399 this.addSelectors.bind(this),
398 this.hideElements.bind(this) 400 this.hideElements.bind(this)
399 ); 401 );
400 } 402 }
401 ElemHide.prototype = { 403 ElemHide.prototype = {
402 selectorGroupSize: 1024, 404 selectorGroupSize: 1024,
403 405
404 createShadowTree() 406 createShadowTree()
405 { 407 {
406 // Use Shadow DOM if available as to not mess with with web pages that 408 // Use Shadow DOM if available as to not mess with with web pages that
407 // rely on the order of their own <style> tags (#309). However, creating 409 // rely on the order of their own <style> tags (#309). However, creating
408 // a shadow root breaks running CSS transitions. So we have to create 410 // a shadow root breaks running CSS transitions. So we have to create
409 // the shadow root before transistions might start (#452). 411 // the shadow root before transistions might start (#452).
410 if (!("createShadowRoot" in document.documentElement)) 412 if (!("createShadowRoot" in document.documentElement))
411 return null; 413 return null;
412 414
415 // Both Firefox and Chrome 66+ support user style sheets, so we can avoid
416 // creating an unnecessary shadow root on these platforms.
417 let match = /\bChrome\/(\d+)/.exec(navigator.userAgent);
418 if (!match || match[1] >= 66)
419 return null;
420
413 // Using shadow DOM causes issues on some Google websites, 421 // Using shadow DOM causes issues on some Google websites,
414 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). 422 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687).
415 if (/\.(?:google|blogger)\.com$/.test(document.domain)) 423 if (/\.(?:google|blogger)\.com$/.test(document.domain))
416 return null; 424 return null;
417 425
418 // Finally since some users have both AdBlock and Adblock Plus installed we 426 // Finally since some users have both AdBlock and Adblock Plus installed we
419 // have to consider how the two extensions interact. For example we want to 427 // have to consider how the two extensions interact. For example we want to
420 // avoid creating the shadowRoot twice. 428 // avoid creating the shadowRoot twice.
421 let shadow = document.documentElement.shadowRoot || 429 let shadow = document.documentElement.shadowRoot ||
422 document.documentElement.createShadowRoot(); 430 document.documentElement.createShadowRoot();
423 shadow.appendChild(document.createElement("shadow")); 431 shadow.appendChild(document.createElement("content"));
424 432
425 return shadow; 433 return shadow;
426 }, 434 },
427 435
428 addSelectorsInline(selectors, groupName) 436 addSelectorsInline(selectors, groupName, appendOnly = false)
429 { 437 {
430 let style = this.styles.get(groupName); 438 let style = this.styles.get(groupName);
431 439
432 if (style) 440 if (style && !appendOnly)
433 { 441 {
434 while (style.sheet.cssRules.length > 0) 442 while (style.sheet.cssRules.length > 0)
435 style.sheet.deleteRule(0); 443 style.sheet.deleteRule(0);
436 } 444 }
437 445
438 if (selectors.length == 0) 446 if (selectors.length == 0)
439 return; 447 return;
440 448
441 if (!style) 449 if (!style)
442 { 450 {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) 498 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize)
491 { 499 {
492 let selector = preparedSelectors.slice( 500 let selector = preparedSelectors.slice(
493 i, i + this.selectorGroupSize 501 i, i + this.selectorGroupSize
494 ).join(", "); 502 ).join(", ");
495 style.sheet.insertRule(selector + "{display: none !important;}", 503 style.sheet.insertRule(selector + "{display: none !important;}",
496 style.sheet.cssRules.length); 504 style.sheet.cssRules.length);
497 } 505 }
498 }, 506 },
499 507
500 addSelectors(selectors, filters, groupName = "emulated") 508 addSelectors(selectors, filters, groupName = "emulated", appendOnly = false)
Manish Jethani 2018/02/22 06:55:06 Using a default value for groupName here (instead
kzar 2018/02/26 17:18:59 Acknowledged.
501 { 509 {
502 if (this.inline || this.inlineEmulated) 510 if (this.inline || this.inlineEmulated)
503 { 511 {
504 // Insert the style rules inline if we have been instructed by the 512 // Insert the style rules inline if we have been instructed by the
505 // background page to do so. This is usually the case, except on platforms 513 // background page to do so. This is usually the case, except on platforms
506 // that do support user stylesheets via the browser.tabs.insertCSS API 514 // that do support user stylesheets via the browser.tabs.insertCSS API
507 // (Firefox 53 onwards for now and possibly Chrome in the near future). 515 // (Firefox 53 onwards for now and possibly Chrome in the near future).
508 // Once all supported platforms have implemented this API, we can remove 516 // Once all supported platforms have implemented this API, we can remove
509 // the code below. See issue #5090. 517 // the code below. See issue #5090.
510 // Related Chrome and Firefox issues: 518 // Related Chrome and Firefox issues:
511 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 519 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009
512 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 520 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026
513 this.addSelectorsInline(selectors, groupName); 521 this.addSelectorsInline(selectors, groupName, appendOnly);
514 } 522 }
515 else 523 else
516 { 524 {
517 browser.runtime.sendMessage({ 525 browser.runtime.sendMessage({
518 type: "elemhide.injectSelectors", 526 type: "elemhide.injectSelectors",
519 selectors, 527 selectors,
520 groupName 528 groupName,
529 appendOnly
521 }); 530 });
522 } 531 }
523 532
524 if (this.tracer) 533 // Only trace selectors that are based directly on hiding filters
534 // (i.e. leave out collapsing selectors).
535 if (this.tracer && groupName != "collapsing")
525 this.tracer.addSelectors(selectors, filters); 536 this.tracer.addSelectors(selectors, filters);
526 }, 537 },
527 538
528 hideElements(elements, filters) 539 hideElements(elements, filters)
529 { 540 {
530 for (let element of elements) 541 for (let element of elements)
531 hideElement(element); 542 hideElement(element);
532 543
533 if (this.tracer) 544 if (this.tracer)
534 { 545 {
(...skipping 18 matching lines...) Expand all
553 564
554 this.inline = response.inline; 565 this.inline = response.inline;
555 this.inlineEmulated = !!response.inlineEmulated; 566 this.inlineEmulated = !!response.inlineEmulated;
556 567
557 if (this.inline) 568 if (this.inline)
558 this.addSelectorsInline(response.selectors, "standard"); 569 this.addSelectorsInline(response.selectors, "standard");
559 570
560 if (this.tracer) 571 if (this.tracer)
561 this.tracer.addSelectors(response.selectors); 572 this.tracer.addSelectors(response.selectors);
562 573
574 // Prefer CSS selectors for -abp-has and -abp-contains unless the
575 // background page has asked us to use inline styles.
576 this.elemHideEmulation.useInlineStyles = this.inline ||
577 this.inlineEmulated;
578
563 this.elemHideEmulation.apply(response.emulatedPatterns); 579 this.elemHideEmulation.apply(response.emulatedPatterns);
564 }); 580 });
565 } 581 }
566 }; 582 };
567 583
568 if (document instanceof HTMLDocument) 584 if (document instanceof HTMLDocument)
569 { 585 {
570 checkSitekey(); 586 checkSitekey();
571 587
572 elemhide = new ElemHide(); 588 elemhide = new ElemHide();
573 elemhide.apply(); 589 elemhide.apply();
574 590
575 document.addEventListener("error", event => 591 document.addEventListener("error", event =>
576 { 592 {
577 checkCollapse(event.target); 593 checkCollapse(event.target);
578 }, true); 594 }, true);
579 595
580 document.addEventListener("load", event => 596 document.addEventListener("load", event =>
581 { 597 {
582 let element = event.target; 598 let element = event.target;
583 if (/^i?frame$/.test(element.localName)) 599 if (/^i?frame$/.test(element.localName))
584 checkCollapse(element); 600 checkCollapse(element);
585 }, true); 601 }, true);
586 } 602 }
587 603
588 window.checkCollapse = checkCollapse; 604 window.checkCollapse = checkCollapse;
589 window.elemhide = elemhide; 605 window.elemhide = elemhide;
590 window.typeMap = typeMap; 606 window.typeMap = typeMap;
591 window.getURLsFromElement = getURLsFromElement; 607 window.getURLsFromElement = getURLsFromElement;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld