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: Tidy up hasRecord and some notification logic Created March 31, 2017, 3:36 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-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 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 for (let record of panel.records)
82 record.request.url == request.url && 82 {
83 record.request.docDomain == request.docDomain && 83 if (record.request.url != request.url ||
84 record.request.docDomain != request.docDomain)
85 continue;
84 86
85 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already 87 if (record.request.type == "DOCUMENT")
86 // a DOCUMENT exception which disables all means of blocking. 88 {
87 (record.request.type == "DOCUMENT" ? nonRequestTypes.indexOf(request.type) ! = -1 89 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already
88 : record.request.type == request.type) && 90 // a DOCUMENT exception which disables all means of blocking.
91 if (!nonRequestTypes.includes(request.type))
92 continue;
93 }
94 else if (record.request.type != request.type)
95 continue;
89 96
90 // Matched element hiding filters don't relate to a particular request, 97 // 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. 98 // so we also have to match the CSS selector in order to distinguish them.
Sebastian Noack 2017/03/31 07:59:37 While thinking about a nicer way to do these check
kzar 2017/03/31 08:27:38 Done.
92 (record.filter && record.filter.selector) == (filter && filter.selector) 99 if ((record.filter && record.filter.selector) ==
93 ); 100 (filter && filter.selector))
101 return true;
102 }
103
104 return false;
94 } 105 }
95 106
96 function addRecord(panel, request, filter) 107 function addRecord(panel, request, filter)
97 { 108 {
98 if (!hasRecord(panel, request, filter)) 109 if (!hasRecord(panel, request, filter))
99 { 110 {
100 panel.port.postMessage({ 111 panel.port.postMessage({
101 type: "add-record", 112 type: "add-record",
102 request: request, 113 request,
103 filter: getFilterInfo(filter) 114 filter: getFilterInfo(filter)
104 }); 115 });
105 116
106 panel.records.push({ 117 panel.records.push({request, filter});
107 request: request,
108 filter: filter
109 });
110 } 118 }
111 } 119 }
112 120
113 function matchRequest(request) 121 function matchRequest(request)
114 { 122 {
115 return defaultMatcher.matchesAny( 123 return defaultMatcher.matchesAny(
116 request.url, 124 request.url,
117 RegExpFilter.typeMap[request.type], 125 RegExpFilter.typeMap[request.type],
118 request.docDomain, 126 request.docDomain,
119 request.thirdParty, 127 request.thirdParty,
120 request.sitekey, 128 request.sitekey,
121 request.specificOnly 129 request.specificOnly
122 ); 130 );
123 } 131 }
124 132
125 /** 133 /**
126 * Logs a request to the devtools panel. 134 * Logs a request to the devtools panel.
127 * 135 *
128 * @param {Page} page The page the request occured on 136 * @param {Page} page The page the request occured on
129 * @param {string} url The URL of the request 137 * @param {string} url The URL of the request
130 * @param {string} type The request type 138 * @param {string} type The request type
131 * @param {string} docDomain The IDN-decoded hostname of the document 139 * @param {string} docDomain The IDN-decoded hostname of the document
132 * @param {boolean} thirdParty Whether the origin of the request and documen t differs 140 * @param {boolean} thirdParty Whether the origin of the request and
141 * document differs
133 * @param {?string} sitekey The active sitekey if there is any 142 * @param {?string} sitekey The active sitekey if there is any
134 * @param {?boolean} specificOnly Whether generic filters should be ignored 143 * @param {?boolean} specificOnly Whether generic filters should be ignored
135 * @param {?BlockingFilter} filter The matched filter or null if there is no mat ch 144 * @param {?BlockingFilter} filter The matched filter or null if there is no
145 * match
136 */ 146 */
137 exports.logRequest = function(page, url, type, docDomain, 147 exports.logRequest = function(page, url, type, docDomain,
138 thirdParty, sitekey, 148 thirdParty, sitekey,
139 specificOnly, filter) 149 specificOnly, filter)
140 { 150 {
141 let panel = getActivePanel(page); 151 let panel = getActivePanel(page);
142 if (panel) 152 if (panel)
143 { 153 {
144 let request = { 154 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly};
145 url: url,
146 type: type,
147 docDomain: docDomain,
148 thirdParty: thirdParty,
149 sitekey: sitekey,
150 specificOnly: specificOnly
151 };
152
153 addRecord(panel, request, filter); 155 addRecord(panel, request, filter);
154 } 156 }
155 }; 157 };
156 158
157 /** 159 /**
158 * Logs active element hiding filters to the devtools panel. 160 * Logs active element hiding filters to the devtools panel.
159 * 161 *
160 * @param {Page} page The page the elements were hidden on 162 * @param {Page} page The page the elements were hidden on
161 * @param {string[]} selectors The CSS selectors of active elemhide filters 163 * @param {string[]} selectors The CSS selectors of active elemhide filters
162 * @param {string} docDomain The IDN-decoded hostname of the document 164 * @param {string} docDomain The IDN-decoded hostname of the document
(...skipping 11 matching lines...) Expand all
174 for (let filter of subscription.filters) 176 for (let filter of subscription.filters)
175 { 177 {
176 if (!(filter instanceof ElemHideFilter) && 178 if (!(filter instanceof ElemHideFilter) &&
177 !(filter instanceof ElemHideEmulationFilter)) 179 !(filter instanceof ElemHideEmulationFilter))
178 continue; 180 continue;
179 if (selectors.indexOf(filter.selector) == -1) 181 if (selectors.indexOf(filter.selector) == -1)
180 continue; 182 continue;
181 if (!filter.isActiveOnDomain(docDomain)) 183 if (!filter.isActiveOnDomain(docDomain))
182 continue; 184 continue;
183 185
184 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); 186 addRecord(panel, {type: "ELEMHIDE", docDomain}, filter);
185 } 187 }
186 } 188 }
187 } 189 }
188 }; 190 }
189 191
190 /** 192 /**
191 * Logs a whitelisting filter, that disables (some kind of) 193 * Logs a whitelisting filter, that disables (some kind of)
192 * blocking for a particular document, to the devtools panel. 194 * blocking for a particular document, to the devtools panel.
193 * 195 *
194 * @param {Page} page The page the whitelisting is active on 196 * @param {Page} page The page the whitelisting is active on
195 * @param {string} url The url of the whitelisted document 197 * @param {string} url The url of the whitelisted document
196 * @param {number} typeMask The bit mask of whitelisting types checked fo r 198 * @param {number} typeMask The bit mask of whitelisting types checked
197 * @param {string} docDomain The IDN-decoded hostname of the parent docume nt 199 * for
200 * @param {string} docDomain The IDN-decoded hostname of the parent
201 * document
198 * @param {WhitelistFilter} filter The matched whitelisting filter 202 * @param {WhitelistFilter} filter The matched whitelisting filter
199 */ 203 */
200 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter ) 204 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain,
205 filter)
201 { 206 {
202 let panel = getActivePanel(page); 207 let panel = getActivePanel(page);
203 if (panel) 208 if (panel)
204 { 209 {
205 for (let type of nonRequestTypes) 210 for (let type of nonRequestTypes)
206 { 211 {
207 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) 212 if (typeMask & filter.contentType & RegExpFilter.typeMap[type])
208 addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter); 213 addRecord(panel, {url, type, docDomain}, filter);
209 } 214 }
210 } 215 }
211 }; 216 };
212 217
213 /** 218 /**
214 * Checks whether a page is inspected by the devtools panel. 219 * Checks whether a page is inspected by the devtools panel.
215 * 220 *
216 * @param {Page} page 221 * @param {Page} page
217 * @return {boolean} 222 * @return {boolean}
218 */ 223 */
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 { 337 {
333 updateFilters([filter], false); 338 updateFilters([filter], false);
334 } 339 }
335 340
336 function onSubscriptionAdded(subscription) 341 function onSubscriptionAdded(subscription)
337 { 342 {
338 if (subscription instanceof SpecialSubscription) 343 if (subscription instanceof SpecialSubscription)
339 updateFilters(subscription.filters, true); 344 updateFilters(subscription.filters, true);
340 } 345 }
341 346
342 chrome.runtime.onConnect.addListener(port => 347 chrome.runtime.onConnect.addListener(newPort =>
343 { 348 {
344 let match = port.name.match(/^devtools-(\d+)$/); 349 let match = newPort.name.match(/^devtools-(\d+)$/);
345 if (!match) 350 if (!match)
346 return; 351 return;
347 352
348 let inspectedTabId = parseInt(match[1], 10); 353 let inspectedTabId = parseInt(match[1], 10);
349 let localOnBeforeRequest = onBeforeRequest.bind(); 354 let localOnBeforeRequest = onBeforeRequest.bind();
350 355
351 chrome.webRequest.onBeforeRequest.addListener( 356 chrome.webRequest.onBeforeRequest.addListener(
352 localOnBeforeRequest, 357 localOnBeforeRequest,
353 { 358 {
354 urls: ["<all_urls>"], 359 urls: ["<all_urls>"],
355 types: ["main_frame"], 360 types: ["main_frame"],
356 tabId: inspectedTabId 361 tabId: inspectedTabId
357 } 362 }
358 ); 363 );
359 364
360 if (!hasPanels()) 365 if (!hasPanels())
361 { 366 {
362 ext.pages.onLoading.addListener(onLoading); 367 ext.pages.onLoading.addListener(onLoading);
363 FilterNotifier.on("filter.added", onFilterAdded); 368 FilterNotifier.on("filter.added", onFilterAdded);
364 FilterNotifier.on("filter.removed", onFilterRemoved); 369 FilterNotifier.on("filter.removed", onFilterRemoved);
365 FilterNotifier.on("subscription.added", onSubscriptionAdded); 370 FilterNotifier.on("subscription.added", onSubscriptionAdded);
366 } 371 }
367 372
368 port.onDisconnect.addListener(() => 373 newPort.onDisconnect.addListener(() =>
369 { 374 {
370 delete panels[inspectedTabId]; 375 delete panels[inspectedTabId];
371 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); 376 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
372 377
373 if (!hasPanels()) 378 if (!hasPanels())
374 { 379 {
375 ext.pages.onLoading.removeListener(onLoading); 380 ext.pages.onLoading.removeListener(onLoading);
376 FilterNotifier.off("filter.added", onFilterAdded); 381 FilterNotifier.off("filter.added", onFilterAdded);
377 FilterNotifier.off("filter.removed", onFilterRemoved); 382 FilterNotifier.off("filter.removed", onFilterRemoved);
378 FilterNotifier.off("subscription.added", onSubscriptionAdded); 383 FilterNotifier.off("subscription.added", onSubscriptionAdded);
379 } 384 }
380 }); 385 });
381 386
382 panels[inspectedTabId] = {port: port, records: []}; 387 panels[inspectedTabId] = {port: newPort, records: []};
383 }); 388 });
384 389
385 port.on("devtools.traceElemHide", (message, sender) => 390 port.on("devtools.traceElemHide", (message, sender) =>
386 { 391 {
387 logHiddenElements( 392 logHiddenElements(
388 sender.page, message.selectors, 393 sender.page, message.selectors,
389 extractHostFromFrame(sender.frame) 394 extractHostFromFrame(sender.frame)
390 ); 395 );
391 }); 396 });
OLDNEW
« no previous file with comments | « lib/csp.js ('k') | lib/filterComposer.js » ('j') | notification.js » ('J')

Powered by Google App Engine
This is Rietveld