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

Side by Side Diff: lib/devtools.js

Issue 29374674: Issue 4864 - Start using ESLint for adblockpluschrome (Closed)
Patch Set: Fixed typo with shadowRoot getter Created March 14, 2017, 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
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 26 matching lines...) Expand all
37 let panels = Object.create(null); 37 let panels = Object.create(null);
38 38
39 function hasPanels() 39 function hasPanels()
40 { 40 {
41 return Object.keys(panels).length > 0; 41 return Object.keys(panels).length > 0;
42 } 42 }
43 43
44 function getActivePanel(page) 44 function getActivePanel(page)
45 { 45 {
46 let panel = panels[page.id]; 46 let panel = panels[page.id];
47 if(panel && !panel.reload && !panel.reloading) 47 if (panel && !panel.reload && !panel.reloading)
48 return panel; 48 return panel;
49 return null; 49 return null;
50 } 50 }
51 51
52 function getFilterInfo(filter) 52 function getFilterInfo(filter)
53 { 53 {
54 if (!filter) 54 if (!filter)
55 return null; 55 return null;
56 56
57 let userDefined = false; 57 let userDefined = false;
58 let subscriptionTitle = null; 58 let subscriptionTitle = null;
59 59
60 for (let subscription of filter.subscriptions) 60 for (let subscription of filter.subscriptions)
61 { 61 {
62 if (!subscription.disabled) 62 if (!subscription.disabled)
63 { 63 {
64 if (subscription instanceof SpecialSubscription) 64 if (subscription instanceof SpecialSubscription)
65 userDefined = true; 65 userDefined = true;
66 else 66 else
67 subscriptionTitle = subscription.title; 67 subscriptionTitle = subscription.title;
68 } 68 }
69 } 69 }
70 70
71 return { 71 return {
72 text: filter.text, 72 text: filter.text,
73 whitelisted: filter instanceof WhitelistFilter, 73 whitelisted: filter instanceof WhitelistFilter,
74 userDefined: userDefined, 74 userDefined,
75 subscription: subscriptionTitle 75 subscription: subscriptionTitle
76 }; 76 };
77 } 77 }
78 78
79 function hasRecord(panel, request, filter) 79 function hasRecord(panel, request, filter)
80 { 80 {
81 return panel.records.some(record => 81 return panel.records.some(record =>
82 record.request.url == request.url && 82 record.request.url == request.url &&
83 record.request.docDomain == request.docDomain && 83 record.request.docDomain == request.docDomain &&
84 84
85 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already 85 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already
86 // a DOCUMENT exception which disables all means of blocking. 86 // a DOCUMENT exception which disables all means of blocking.
87 (record.request.type == "DOCUMENT" ? nonRequestTypes.indexOf(request.type) ! = -1 87 (record.request.type == "DOCUMENT"
88 : record.request.type == request.type) && 88 ? nonRequestTypes.indexOf(request.type) != -1
89 : record.request.type == request.type) &&
Wladimir Palant 2017/03/14 13:03:27 Please don't use one-space indentations. Besides,
kzar 2017/03/15 04:57:47 It turns out we're using the default configuration
89 90
90 // Matched element hiding filters don't relate to a particular request, 91 // Matched element hiding filters don't relate to a particular request,
91 // so we also have to match the CSS selector in order to distinguish them. 92 // so we also have to match the CSS selector in order to distinguish them.
92 (record.filter && record.filter.selector) == (filter && filter.selector) 93 (record.filter && record.filter.selector) == (filter && filter.selector)
93 ); 94 );
94 } 95 }
95 96
96 function addRecord(panel, request, filter) 97 function addRecord(panel, request, filter)
97 { 98 {
98 if (!hasRecord(panel, request, filter)) 99 if (!hasRecord(panel, request, filter))
99 { 100 {
100 panel.port.postMessage({ 101 panel.port.postMessage({
101 type: "add-record", 102 type: "add-record",
102 request: request, 103 request,
103 filter: getFilterInfo(filter) 104 filter: getFilterInfo(filter)
104 }); 105 });
105 106
106 panel.records.push({ 107 panel.records.push({request, filter});
107 request: request,
108 filter: filter
109 });
110 } 108 }
111 } 109 }
112 110
113 function matchRequest(request) 111 function matchRequest(request)
114 { 112 {
115 return defaultMatcher.matchesAny( 113 return defaultMatcher.matchesAny(
116 request.url, 114 request.url,
117 RegExpFilter.typeMap[request.type], 115 RegExpFilter.typeMap[request.type],
118 request.docDomain, 116 request.docDomain,
119 request.thirdParty, 117 request.thirdParty,
120 request.sitekey, 118 request.sitekey,
121 request.specificOnly 119 request.specificOnly
122 ); 120 );
123 } 121 }
124 122
125 /** 123 /**
126 * Logs a request to the devtools panel. 124 * Logs a request to the devtools panel.
127 * 125 *
128 * @param {Page} page The page the request occured on 126 * @param {Page} page The page the request occured on
129 * @param {string} url The URL of the request 127 * @param {string} url The URL of the request
130 * @param {string} type The request type 128 * @param {string} type The request type
131 * @param {string} docDomain The IDN-decoded hostname of the document 129 * @param {string} docDomain The IDN-decoded hostname of the document
132 * @param {boolean} thirdParty Whether the origin of the request and documen t differs 130 * @param {boolean} thirdParty Whether the origin of the request and
131 * document differs
133 * @param {?string} sitekey The active sitekey if there is any 132 * @param {?string} sitekey The active sitekey if there is any
134 * @param {?boolean} specificOnly Whether generic filters should be ignored 133 * @param {?boolean} specificOnly Whether generic filters should be ignored
135 * @param {?BlockingFilter} filter The matched filter or null if there is no mat ch 134 * @param {?BlockingFilter} filter The matched filter or null if there is no
135 * match
136 */ 136 */
137 exports.logRequest = function(page, url, type, docDomain, 137 exports.logRequest = function(page, url, type, docDomain,
138 thirdParty, sitekey, 138 thirdParty, sitekey,
139 specificOnly, filter) 139 specificOnly, filter)
140 { 140 {
141 let panel = getActivePanel(page); 141 let panel = getActivePanel(page);
142 if (panel) 142 if (panel)
143 { 143 {
144 let request = { 144 addRecord(panel, {url, type, docDomain, thirdParty, sitekey, specificOnly},
145 url: url, 145 filter);
146 type: type,
147 docDomain: docDomain,
148 thirdParty: thirdParty,
149 sitekey: sitekey,
150 specificOnly: specificOnly
151 };
152
153 addRecord(panel, request, filter);
154 } 146 }
155 }; 147 };
156 148
157 /** 149 /**
158 * Logs active element hiding filters to the devtools panel. 150 * Logs active element hiding filters to the devtools panel.
159 * 151 *
160 * @param {Page} page The page the elements were hidden on 152 * @param {Page} page The page the elements were hidden on
161 * @param {string[]} selectors The CSS selectors of active elemhide filters 153 * @param {string[]} selectors The CSS selectors of active elemhide filters
162 * @param {string} docDomain The IDN-decoded hostname of the document 154 * @param {string} docDomain The IDN-decoded hostname of the document
163 */ 155 */
(...skipping 10 matching lines...) Expand all
174 for (let filter of subscription.filters) 166 for (let filter of subscription.filters)
175 { 167 {
176 if (!(filter instanceof ElemHideFilter) && 168 if (!(filter instanceof ElemHideFilter) &&
177 !(filter instanceof ElemHideEmulationFilter)) 169 !(filter instanceof ElemHideEmulationFilter))
178 continue; 170 continue;
179 if (selectors.indexOf(filter.selector) == -1) 171 if (selectors.indexOf(filter.selector) == -1)
180 continue; 172 continue;
181 if (!filter.isActiveOnDomain(docDomain)) 173 if (!filter.isActiveOnDomain(docDomain))
182 continue; 174 continue;
183 175
184 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); 176 addRecord(panel, {type: "ELEMHIDE", docDomain}, filter);
185 } 177 }
186 } 178 }
187 } 179 }
188 }; 180 }
189 181
190 /** 182 /**
191 * Logs a whitelisting filter, that disables (some kind of) 183 * Logs a whitelisting filter, that disables (some kind of)
192 * blocking for a particular document, to the devtools panel. 184 * blocking for a particular document, to the devtools panel.
193 * 185 *
194 * @param {Page} page The page the whitelisting is active on 186 * @param {Page} page The page the whitelisting is active on
195 * @param {string} url The url of the whitelisted document 187 * @param {string} url The url of the whitelisted document
196 * @param {number} typeMask The bit mask of whitelisting types checked fo r 188 * @param {number} typeMask The bit mask of whitelisting types checked
197 * @param {string} docDomain The IDN-decoded hostname of the parent docume nt 189 * for
190 * @param {string} docDomain The IDN-decoded hostname of the parent
191 * document
198 * @param {WhitelistFilter} filter The matched whitelisting filter 192 * @param {WhitelistFilter} filter The matched whitelisting filter
199 */ 193 */
200 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter ) 194 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain,
195 filter)
201 { 196 {
202 let panel = getActivePanel(page); 197 let panel = getActivePanel(page);
203 if (panel) 198 if (panel)
204 { 199 {
205 for (let type of nonRequestTypes) 200 for (let type of nonRequestTypes)
206 { 201 {
207 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) 202 if (typeMask & filter.contentType & RegExpFilter.typeMap[type])
208 addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter); 203 addRecord(panel, {url, type, docDomain}, filter);
209 } 204 }
210 } 205 }
211 }; 206 };
212 207
213 /** 208 /**
214 * Checks whether a page is inspected by the devtools panel. 209 * Checks whether a page is inspected by the devtools panel.
215 * 210 *
216 * @param {Page} page 211 * @param {Page} page
217 * @return {boolean} 212 * @return {boolean}
218 */ 213 */
219 exports.hasPanel = function(page) 214 exports.hasPanel = function(page)
220 { 215 {
221 return page.id in panels; 216 return page.id in panels;
222 }; 217 };
223 218
224 function onBeforeRequest(details) 219 function onBeforeRequest(details)
225 { 220 {
226 let panel = panels[details.tabId]; 221 let panel = panels[details.tabId];
227 222
228 // Clear the devtools panel and reload the inspected tab without caching 223 // Clear the devtools panel and reload the inspected tab without caching
229 // when a new request is issued. However, make sure that we don't end up 224 // when a new request is issued. However, make sure that we don't end up
230 // in an infinite recursion if we already triggered a reload. 225 // in an infinite recursion if we already triggered a reload.
231 if (panel.reloading) 226 if (panel.reloading)
232 {
233 panel.reloading = false; 227 panel.reloading = false;
234 }
235 else 228 else
236 { 229 {
237 panel.records = []; 230 panel.records = [];
238 panel.port.postMessage({type: "reset"}); 231 panel.port.postMessage({type: "reset"});
239 232
240 // We can't repeat the request if it isn't a GET request. Chrome would 233 // We can't repeat the request if it isn't a GET request. Chrome would
241 // prompt the user to confirm reloading the page, and POST requests are 234 // prompt the user to confirm reloading the page, and POST requests are
242 // known to cause issues on many websites if repeated. 235 // known to cause issues on many websites if repeated.
243 if (details.method == "GET") 236 if (details.method == "GET")
244 panel.reload = true; 237 panel.reload = true;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 { 325 {
333 updateFilters([filter], false); 326 updateFilters([filter], false);
334 } 327 }
335 328
336 function onSubscriptionAdded(subscription) 329 function onSubscriptionAdded(subscription)
337 { 330 {
338 if (subscription instanceof SpecialSubscription) 331 if (subscription instanceof SpecialSubscription)
339 updateFilters(subscription.filters, true); 332 updateFilters(subscription.filters, true);
340 } 333 }
341 334
342 chrome.runtime.onConnect.addListener(port => 335 chrome.runtime.onConnect.addListener(newPort =>
343 { 336 {
344 let match = port.name.match(/^devtools-(\d+)$/); 337 let match = newPort.name.match(/^devtools-(\d+)$/);
345 if (!match) 338 if (!match)
346 return; 339 return;
347 340
348 let inspectedTabId = parseInt(match[1], 10); 341 let inspectedTabId = parseInt(match[1], 10);
349 let localOnBeforeRequest = onBeforeRequest.bind(); 342 let localOnBeforeRequest = onBeforeRequest.bind();
350 343
351 chrome.webRequest.onBeforeRequest.addListener( 344 chrome.webRequest.onBeforeRequest.addListener(
352 localOnBeforeRequest, 345 localOnBeforeRequest,
353 { 346 {
354 urls: ["<all_urls>"], 347 urls: ["<all_urls>"],
355 types: ["main_frame"], 348 types: ["main_frame"],
356 tabId: inspectedTabId 349 tabId: inspectedTabId
357 } 350 }
358 ); 351 );
359 352
360 if (!hasPanels()) 353 if (!hasPanels())
361 { 354 {
362 ext.pages.onLoading.addListener(onLoading); 355 ext.pages.onLoading.addListener(onLoading);
363 FilterNotifier.on("filter.added", onFilterAdded); 356 FilterNotifier.on("filter.added", onFilterAdded);
364 FilterNotifier.on("filter.removed", onFilterRemoved); 357 FilterNotifier.on("filter.removed", onFilterRemoved);
365 FilterNotifier.on("subscription.added", onSubscriptionAdded); 358 FilterNotifier.on("subscription.added", onSubscriptionAdded);
366 } 359 }
367 360
368 port.onDisconnect.addListener(() => 361 newPort.onDisconnect.addListener(() =>
369 { 362 {
370 delete panels[inspectedTabId]; 363 delete panels[inspectedTabId];
371 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); 364 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
372 365
373 if (!hasPanels()) 366 if (!hasPanels())
374 { 367 {
375 ext.pages.onLoading.removeListener(onLoading); 368 ext.pages.onLoading.removeListener(onLoading);
376 FilterNotifier.off("filter.added", onFilterAdded); 369 FilterNotifier.off("filter.added", onFilterAdded);
377 FilterNotifier.off("filter.removed", onFilterRemoved); 370 FilterNotifier.off("filter.removed", onFilterRemoved);
378 FilterNotifier.off("subscription.added", onSubscriptionAdded); 371 FilterNotifier.off("subscription.added", onSubscriptionAdded);
379 } 372 }
380 }); 373 });
381 374
382 panels[inspectedTabId] = {port: port, records: []}; 375 panels[inspectedTabId] = {port: newPort, records: []};
383 }); 376 });
384 377
385 port.on("devtools.traceElemHide", (message, sender) => 378 port.on("devtools.traceElemHide", (message, sender) =>
386 { 379 {
387 logHiddenElements( 380 logHiddenElements(
388 sender.page, message.selectors, 381 sender.page, message.selectors,
389 extractHostFromFrame(sender.frame) 382 extractHostFromFrame(sender.frame)
390 ); 383 );
391 }); 384 });
OLDNEW

Powered by Google App Engine
This is Rietveld