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