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: Use .includes again Created March 31, 2017, 8:37 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 | « lib/csp.js ('k') | lib/filterComposer.js » ('j') | 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-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 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.includes(request.type) :
89 record.request.type == request.type) &&
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 have to compare the selector in order to avoid duplicates.
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 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); 145 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
(...skipping 11 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 */
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 { 327 {
333 updateFilters([filter], false); 328 updateFilters([filter], false);
334 } 329 }
335 330
336 function onSubscriptionAdded(subscription) 331 function onSubscriptionAdded(subscription)
337 { 332 {
338 if (subscription instanceof SpecialSubscription) 333 if (subscription instanceof SpecialSubscription)
339 updateFilters(subscription.filters, true); 334 updateFilters(subscription.filters, true);
340 } 335 }
341 336
342 chrome.runtime.onConnect.addListener(port => 337 chrome.runtime.onConnect.addListener(newPort =>
343 { 338 {
344 let match = port.name.match(/^devtools-(\d+)$/); 339 let match = newPort.name.match(/^devtools-(\d+)$/);
345 if (!match) 340 if (!match)
346 return; 341 return;
347 342
348 let inspectedTabId = parseInt(match[1], 10); 343 let inspectedTabId = parseInt(match[1], 10);
349 let localOnBeforeRequest = onBeforeRequest.bind(); 344 let localOnBeforeRequest = onBeforeRequest.bind();
350 345
351 chrome.webRequest.onBeforeRequest.addListener( 346 chrome.webRequest.onBeforeRequest.addListener(
352 localOnBeforeRequest, 347 localOnBeforeRequest,
353 { 348 {
354 urls: ["<all_urls>"], 349 urls: ["<all_urls>"],
355 types: ["main_frame"], 350 types: ["main_frame"],
356 tabId: inspectedTabId 351 tabId: inspectedTabId
357 } 352 }
358 ); 353 );
359 354
360 if (!hasPanels()) 355 if (!hasPanels())
361 { 356 {
362 ext.pages.onLoading.addListener(onLoading); 357 ext.pages.onLoading.addListener(onLoading);
363 FilterNotifier.on("filter.added", onFilterAdded); 358 FilterNotifier.on("filter.added", onFilterAdded);
364 FilterNotifier.on("filter.removed", onFilterRemoved); 359 FilterNotifier.on("filter.removed", onFilterRemoved);
365 FilterNotifier.on("subscription.added", onSubscriptionAdded); 360 FilterNotifier.on("subscription.added", onSubscriptionAdded);
366 } 361 }
367 362
368 port.onDisconnect.addListener(() => 363 newPort.onDisconnect.addListener(() =>
369 { 364 {
370 delete panels[inspectedTabId]; 365 delete panels[inspectedTabId];
371 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); 366 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
372 367
373 if (!hasPanels()) 368 if (!hasPanels())
374 { 369 {
375 ext.pages.onLoading.removeListener(onLoading); 370 ext.pages.onLoading.removeListener(onLoading);
376 FilterNotifier.off("filter.added", onFilterAdded); 371 FilterNotifier.off("filter.added", onFilterAdded);
377 FilterNotifier.off("filter.removed", onFilterRemoved); 372 FilterNotifier.off("filter.removed", onFilterRemoved);
378 FilterNotifier.off("subscription.added", onSubscriptionAdded); 373 FilterNotifier.off("subscription.added", onSubscriptionAdded);
379 } 374 }
380 }); 375 });
381 376
382 panels[inspectedTabId] = {port: port, records: []}; 377 panels[inspectedTabId] = {port: newPort, records: []};
383 }); 378 });
384 379
385 port.on("devtools.traceElemHide", (message, sender) => 380 port.on("devtools.traceElemHide", (message, sender) =>
386 { 381 {
387 logHiddenElements( 382 logHiddenElements(
388 sender.page, message.selectors, 383 sender.page, message.selectors,
389 extractHostFromFrame(sender.frame) 384 extractHostFromFrame(sender.frame)
390 ); 385 );
391 }); 386 });
OLDNEW
« no previous file with comments | « lib/csp.js ('k') | lib/filterComposer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld