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

Side by Side Diff: include.preload.js

Issue 29670575: Issue 5899 - Use CSS attribute selectors for collapsing media elements (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Use CSS attribute selectors for collapsing Created Feb. 22, 2018, 6:49 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 17 matching lines...) Expand all
28 ["input", "IMAGE"], 28 ["input", "IMAGE"],
29 ["picture", "IMAGE"], 29 ["picture", "IMAGE"],
30 ["audio", "MEDIA"], 30 ["audio", "MEDIA"],
31 ["video", "MEDIA"], 31 ["video", "MEDIA"],
32 ["frame", "SUBDOCUMENT"], 32 ["frame", "SUBDOCUMENT"],
33 ["iframe", "SUBDOCUMENT"], 33 ["iframe", "SUBDOCUMENT"],
34 ["object", "OBJECT"], 34 ["object", "OBJECT"],
35 ["embed", "OBJECT"] 35 ["embed", "OBJECT"]
36 ]); 36 ]);
37 37
38 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
38 function getURLsFromObjectElement(element) 40 function getURLsFromObjectElement(element)
39 { 41 {
40 let url = element.getAttribute("data"); 42 let url = element.getAttribute("data");
41 if (url) 43 if (url)
42 return [url]; 44 return [url];
43 45
44 for (let child of element.children) 46 for (let child of element.children)
45 { 47 {
46 if (child.localName != "param") 48 if (child.localName != "param")
47 continue; 49 continue;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 123
122 for (let i = 0; i < urls.length; i++) 124 for (let i = 0; i < urls.length; i++)
123 { 125 {
124 if (/^(?!https?:)[\w-]+:/i.test(urls[i])) 126 if (/^(?!https?:)[\w-]+:/i.test(urls[i]))
125 urls.splice(i--, 1); 127 urls.splice(i--, 1);
126 } 128 }
127 129
128 return urls; 130 return urls;
129 } 131 }
130 132
133 function isCollapsibleMediaElement(element, mediatype)
134 {
135 if (mediatype != "MEDIA")
136 return false;
137
138 if (!element.getAttribute("src"))
139 return false;
140
141 for (let child of element.children)
142 {
143 // 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 // don't "collapse" it using a CSS selector but rather hide it directly by
146 // setting the style="..." attribute.
147 if (["source", "track"].includes(child.localName))
148 return false;
149 }
150
151 return true;
152 }
153
154 function collapseMediaElement(element, srcValue)
155 {
156 let selector = 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) + "]" :
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 // Adding selectors is expensive so do it only if we really have a new
160 // selector.
161 if (selector && !collapsingSelectors.has(selector))
162 {
163 collapsingSelectors.add(selector);
164 elemhide.addSelectors(Array.from(collapsingSelectors), null, "collapsing");
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 }
166 }
167
131 function hideElement(element) 168 function hideElement(element)
132 { 169 {
133 function doHide() 170 function doHide()
134 { 171 {
135 let propertyName = "display"; 172 let propertyName = "display";
136 let propertyValue = "none"; 173 let propertyValue = "none";
137 if (element.localName == "frame") 174 if (element.localName == "frame")
138 { 175 {
139 propertyName = "visibility"; 176 propertyName = "visibility";
140 propertyValue = "hidden"; 177 propertyValue = "hidden";
(...skipping 17 matching lines...) Expand all
158 function checkCollapse(element) 195 function checkCollapse(element)
159 { 196 {
160 let mediatype = typeMap.get(element.localName); 197 let mediatype = typeMap.get(element.localName);
161 if (!mediatype) 198 if (!mediatype)
162 return; 199 return;
163 200
164 let urls = getURLsFromElement(element); 201 let urls = getURLsFromElement(element);
165 if (urls.length == 0) 202 if (urls.length == 0)
166 return; 203 return;
167 204
205 let collapsible = 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
207 // Save the value of the src attribute because it can change between now and
208 // when we get the response from the background page.
209 let srcValue = collapsible ? 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
168 browser.runtime.sendMessage( 211 browser.runtime.sendMessage(
169 { 212 {
170 type: "filters.collapse", 213 type: "filters.collapse",
171 urls, 214 urls,
172 mediatype, 215 mediatype,
173 baseURL: document.location.href 216 baseURL: document.location.href
174 }, 217 },
175
176 collapse => 218 collapse =>
177 { 219 {
178 if (collapse) 220 if (collapse)
179 { 221 {
180 hideElement(element); 222 if (collapsible)
223 collapseMediaElement(element, srcValue);
224 else
225 hideElement(element);
181 } 226 }
182 } 227 }
183 ); 228 );
184 } 229 }
185 230
186 function checkSitekey() 231 function checkSitekey()
187 { 232 {
188 let attr = document.documentElement.getAttribute("data-adblockkey"); 233 let attr = document.documentElement.getAttribute("data-adblockkey");
189 if (attr) 234 if (attr)
190 browser.runtime.sendMessage({type: "filters.addKey", token: attr}); 235 browser.runtime.sendMessage({type: "filters.addKey", token: attr});
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) 490 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize)
446 { 491 {
447 let selector = preparedSelectors.slice( 492 let selector = preparedSelectors.slice(
448 i, i + this.selectorGroupSize 493 i, i + this.selectorGroupSize
449 ).join(", "); 494 ).join(", ");
450 style.sheet.insertRule(selector + "{display: none !important;}", 495 style.sheet.insertRule(selector + "{display: none !important;}",
451 style.sheet.cssRules.length); 496 style.sheet.cssRules.length);
452 } 497 }
453 }, 498 },
454 499
455 addSelectors(selectors, filters) 500 addSelectors(selectors, filters, groupName = "emulated")
Manish Jethani 2018/02/22 06:55:06 Using a default value for groupName here (instead
kzar 2018/02/26 17:18:59 Acknowledged.
456 { 501 {
457 if (this.inline || this.inlineEmulated) 502 if (this.inline || this.inlineEmulated)
458 { 503 {
459 // Insert the style rules inline if we have been instructed by the 504 // 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 505 // 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 506 // that do support user stylesheets via the browser.tabs.insertCSS API
462 // (Firefox 53 onwards for now and possibly Chrome in the near future). 507 // (Firefox 53 onwards for now and possibly Chrome in the near future).
463 // Once all supported platforms have implemented this API, we can remove 508 // Once all supported platforms have implemented this API, we can remove
464 // the code below. See issue #5090. 509 // the code below. See issue #5090.
465 // Related Chrome and Firefox issues: 510 // Related Chrome and Firefox issues:
466 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 511 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009
467 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 512 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026
468 this.addSelectorsInline(selectors, "emulated"); 513 this.addSelectorsInline(selectors, groupName);
469 } 514 }
470 else 515 else
471 { 516 {
472 browser.runtime.sendMessage({ 517 browser.runtime.sendMessage({
473 type: "elemhide.injectSelectors", 518 type: "elemhide.injectSelectors",
474 selectors, 519 selectors,
475 groupName: "emulated" 520 groupName
476 }); 521 });
477 } 522 }
478 523
479 if (this.tracer) 524 if (this.tracer)
480 this.tracer.addSelectors(selectors, filters); 525 this.tracer.addSelectors(selectors, filters);
481 }, 526 },
482 527
483 hideElements(elements, filters) 528 hideElements(elements, filters)
484 { 529 {
485 for (let element of elements) 530 for (let element of elements)
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 let element = event.target; 582 let element = event.target;
538 if (/^i?frame$/.test(element.localName)) 583 if (/^i?frame$/.test(element.localName))
539 checkCollapse(element); 584 checkCollapse(element);
540 }, true); 585 }, true);
541 } 586 }
542 587
543 window.checkCollapse = checkCollapse; 588 window.checkCollapse = checkCollapse;
544 window.elemhide = elemhide; 589 window.elemhide = elemhide;
545 window.typeMap = typeMap; 590 window.typeMap = typeMap;
546 window.getURLsFromElement = getURLsFromElement; 591 window.getURLsFromElement = getURLsFromElement;
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld