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: Created Jan. 16, 2018, 10:28 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 collapsedElements = new WeakMap();
Manish Jethani 2018/01/16 10:47:27 A weak map of collapsed elements to their original
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 if (child.localName == "source" || child.localName == "track") 94 if (child.localName == "source" || child.localName == "track")
93 urls.push(...getURLsFromAttributes(child)); 95 urls.push(...getURLsFromAttributes(child));
94 } 96 }
95 97
96 if (element.poster) 98 if (element.poster)
97 urls.push(element.poster); 99 urls.push(element.poster);
98 100
99 return urls; 101 return urls;
100 } 102 }
101 103
102 function getURLsFromElement(element) 104 function getURLsFromElement(element, all)
103 { 105 {
104 let urls; 106 let urls;
105 switch (element.localName) 107 switch (element.localName)
106 { 108 {
107 case "object": 109 case "object":
108 urls = getURLsFromObjectElement(element); 110 urls = getURLsFromObjectElement(element);
109 break; 111 break;
110 112
111 case "video": 113 case "video":
112 case "audio": 114 case "audio":
113 case "picture": 115 case "picture":
114 urls = getURLsFromMediaElement(element); 116 urls = getURLsFromMediaElement(element);
115 break; 117 break;
116 118
117 default: 119 default:
118 urls = getURLsFromAttributes(element); 120 urls = getURLsFromAttributes(element);
119 break; 121 break;
120 } 122 }
121 123
124 if (all)
125 return urls;
126
122 for (let i = 0; i < urls.length; i++) 127 for (let i = 0; i < urls.length; i++)
123 { 128 {
124 if (/^(?!https?:)[\w-]+:/i.test(urls[i])) 129 if (/^(?!https?:)[\w-]+:/i.test(urls[i]))
125 urls.splice(i--, 1); 130 urls.splice(i--, 1);
126 } 131 }
127 132
128 return urls; 133 return urls;
129 } 134 }
130 135
136 function collapseElement(element)
Manish Jethani 2018/01/16 10:47:27 This is basically what hideElement does, except it
137 {
138 let value = element.style.getPropertyValue("display");
139 let priority = element.style.getPropertyPriority("display");
140 if (value != "none" || priority != "important")
141 {
142 element.style.setProperty("display", "none", "important");
143 collapsedElements.set(element, {value, priority});
144 }
145 }
146
147 function restoreCollapsedElement(element)
148 {
149 let display = collapsedElements.get(element);
150 if (display)
Manish Jethani 2018/01/16 10:47:27 Restore the original display settings.
151 {
152 element.style.setProperty("display", display.value, display.priority);
153 collapsedElements.delete(element);
154 }
155 }
156
131 function hideElement(element) 157 function hideElement(element)
132 { 158 {
133 function doHide() 159 function doHide()
134 { 160 {
135 let propertyName = "display"; 161 let propertyName = "display";
136 let propertyValue = "none"; 162 let propertyValue = "none";
137 if (element.localName == "frame") 163 if (element.localName == "frame")
138 { 164 {
139 propertyName = "visibility"; 165 propertyName = "visibility";
140 propertyValue = "hidden"; 166 propertyValue = "hidden";
(...skipping 13 matching lines...) Expand all
154 } 180 }
155 ); 181 );
156 } 182 }
157 183
158 function checkCollapse(element) 184 function checkCollapse(element)
159 { 185 {
160 let mediatype = typeMap.get(element.localName); 186 let mediatype = typeMap.get(element.localName);
161 if (!mediatype) 187 if (!mediatype)
162 return; 188 return;
163 189
164 let urls = getURLsFromElement(element); 190 let urls = getURLsFromElement(element, mediatype == "MEDIA");
Manish Jethani 2018/01/16 10:47:27 We have to pass true here as the second argument,
165 if (urls.length == 0) 191 if (urls.length == 0)
166 return; 192 return;
167 193
168 browser.runtime.sendMessage( 194 browser.runtime.sendMessage(
169 { 195 {
170 type: "filters.collapse", 196 type: "filters.collapse",
171 urls, 197 urls,
172 mediatype, 198 mediatype,
173 baseURL: document.location.href 199 baseURL: document.location.href
174 }, 200 },
175 201
176 collapse => 202 collapse =>
177 { 203 {
178 if (collapse) 204 if (mediatype == "MEDIA")
205 {
206 if (collapse)
Manish Jethani 2018/01/16 10:47:27 I'm distinguishing "collapsing" from "hiding" here
207 collapseElement(element);
208 else
209 restoreCollapsedElement(element);
210 }
211 else if (collapse)
179 { 212 {
180 hideElement(element); 213 hideElement(element);
181 } 214 }
182 } 215 }
183 ); 216 );
184 } 217 }
185 218
186 function checkSitekey() 219 function checkSitekey()
187 { 220 {
188 let attr = document.documentElement.getAttribute("data-adblockkey"); 221 let attr = document.documentElement.getAttribute("data-adblockkey");
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 { 548 {
516 checkCollapse(event.target); 549 checkCollapse(event.target);
517 }, true); 550 }, true);
518 551
519 document.addEventListener("load", event => 552 document.addEventListener("load", event =>
520 { 553 {
521 let element = event.target; 554 let element = event.target;
522 if (/^i?frame$/.test(element.localName)) 555 if (/^i?frame$/.test(element.localName))
523 checkCollapse(element); 556 checkCollapse(element);
524 }, true); 557 }, true);
558
559 document.addEventListener("loadstart", event =>
Manish Jethani 2018/01/16 10:47:27 The "loadstart" event is dispatched by VIDEO and A
560 {
561 let element = event.target;
562 if (typeMap.get(element.localName) == "MEDIA")
Manish Jethani 2018/01/16 10:47:27 We have to do this check because IMG and PICTURE e
563 checkCollapse(element);
564 }, true);
525 } 565 }
526 566
527 window.checkCollapse = checkCollapse; 567 window.checkCollapse = checkCollapse;
528 window.elemhide = elemhide; 568 window.elemhide = elemhide;
529 window.typeMap = typeMap; 569 window.typeMap = typeMap;
530 window.getURLsFromElement = getURLsFromElement; 570 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