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: Reduce inline eslint-disable comments Created Feb. 8, 2017, 7:27 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/compat.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-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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const {RegExpFilter, WhitelistFilter, ElemHideFilter} = require("filterClasses") ; 20 const {RegExpFilter,
21 WhitelistFilter, ElemHideFilter} = require("filterClasses");
21 const {SpecialSubscription} = require("subscriptionClasses"); 22 const {SpecialSubscription} = require("subscriptionClasses");
22 const {FilterStorage} = require("filterStorage"); 23 const {FilterStorage} = require("filterStorage");
23 const {defaultMatcher} = require("matcher"); 24 const {defaultMatcher} = require("matcher");
24 const {FilterNotifier} = require("filterNotifier"); 25 const {FilterNotifier} = require("filterNotifier");
25 const {extractHostFromFrame} = require("url"); 26 const {extractHostFromFrame} = require("url");
26 const {port} = require("messaging"); 27 const {port} = require("messaging");
27 28
28 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; 29 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"];
29 30
30 // Mapping of inspected tabs to their devpanel page 31 // Mapping of inspected tabs to their devpanel page
31 // and recorded items. We can't use a PageMap here, 32 // and recorded items. We can't use a PageMap here,
32 // because data must persist after navigation/reload. 33 // because data must persist after navigation/reload.
33 let panels = Object.create(null); 34 let panels = Object.create(null);
34 35
35 function hasPanels() 36 function hasPanels()
36 { 37 {
37 return Object.keys(panels).length > 0; 38 return Object.keys(panels).length > 0;
38 } 39 }
39 40
40 function getActivePanel(page) 41 function getActivePanel(page)
41 { 42 {
42 let panel = panels[page.id]; 43 let panel = panels[page.id];
43 if(panel && !panel.reload && !panel.reloading) 44 if (panel && !panel.reload && !panel.reloading)
44 return panel; 45 return panel;
45 return null; 46 return null;
46 } 47 }
47 48
48 function getFilterInfo(filter) 49 function getFilterInfo(filter)
49 { 50 {
50 if (!filter) 51 if (!filter)
51 return null; 52 return null;
52 53
53 let userDefined = false; 54 let userDefined = false;
54 let subscriptionTitle = null; 55 let subscriptionTitle = null;
55 56
56 for (let subscription of filter.subscriptions) 57 for (let subscription of filter.subscriptions)
57 { 58 {
58 if (!subscription.disabled) 59 if (!subscription.disabled)
59 { 60 {
60 if (subscription instanceof SpecialSubscription) 61 if (subscription instanceof SpecialSubscription)
61 userDefined = true; 62 userDefined = true;
62 else 63 else
63 subscriptionTitle = subscription.title; 64 subscriptionTitle = subscription.title;
64 } 65 }
65 } 66 }
66 67
67 return { 68 return {
68 text: filter.text, 69 text: filter.text,
69 whitelisted: filter instanceof WhitelistFilter, 70 whitelisted: filter instanceof WhitelistFilter,
70 userDefined: userDefined, 71 userDefined,
71 subscription: subscriptionTitle 72 subscription: subscriptionTitle
72 }; 73 };
73 } 74 }
74 75
75 function hasRecord(panel, request, filter) 76 function hasRecord(panel, request, filter)
76 { 77 {
77 return panel.records.some(record => 78 return panel.records.some(record =>
78 record.request.url == request.url && 79 record.request.url == request.url &&
79 record.request.docDomain == request.docDomain && 80 record.request.docDomain == request.docDomain &&
80 81
81 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already 82 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already
82 // a DOCUMENT exception which disables all means of blocking. 83 // a DOCUMENT exception which disables all means of blocking.
83 (record.request.type == "DOCUMENT" ? nonRequestTypes.indexOf(request.type) ! = -1 84 (record.request.type == "DOCUMENT"
84 : record.request.type == request.type) && 85 ? nonRequestTypes.indexOf(request.type) != -1
86 : record.request.type == request.type) &&
85 87
86 // Matched element hiding filters don't relate to a particular request, 88 // Matched element hiding filters don't relate to a particular request,
87 // so we also have to match the CSS selector in order to distinguish them. 89 // so we also have to match the CSS selector in order to distinguish them.
88 (record.filter && record.filter.selector) == (filter && filter.selector) 90 (record.filter && record.filter.selector) == (filter && filter.selector)
89 ); 91 );
90 } 92 }
91 93
92 function addRecord(panel, request, filter) 94 function addRecord(panel, request, filter)
93 { 95 {
94 if (!hasRecord(panel, request, filter)) 96 if (!hasRecord(panel, request, filter))
95 { 97 {
96 panel.port.postMessage({ 98 panel.port.postMessage({
97 type: "add-record", 99 type: "add-record",
98 request: request, 100 request,
99 filter: getFilterInfo(filter) 101 filter: getFilterInfo(filter)
100 }); 102 });
101 103
102 panel.records.push({ 104 panel.records.push({request, filter});
103 request: request,
104 filter: filter
105 });
106 } 105 }
107 } 106 }
108 107
109 function matchRequest(request) 108 function matchRequest(request)
110 { 109 {
111 return defaultMatcher.matchesAny( 110 return defaultMatcher.matchesAny(
112 request.url, 111 request.url,
113 RegExpFilter.typeMap[request.type], 112 RegExpFilter.typeMap[request.type],
114 request.docDomain, 113 request.docDomain,
115 request.thirdParty, 114 request.thirdParty,
116 request.sitekey, 115 request.sitekey,
117 request.specificOnly 116 request.specificOnly
118 ); 117 );
119 } 118 }
120 119
121 /** 120 /**
122 * Logs a request to the devtools panel. 121 * Logs a request to the devtools panel.
123 * 122 *
124 * @param {Page} page The page the request occured on 123 * @param {Page} page The page the request occured on
125 * @param {string} url The URL of the request 124 * @param {string} url The URL of the request
126 * @param {string} type The request type 125 * @param {string} type The request type
127 * @param {string} docDomain The IDN-decoded hostname of the document 126 * @param {string} docDomain The IDN-decoded hostname of the document
128 * @param {boolean} thirdParty Whether the origin of the request and documen t differs 127 * @param {boolean} thirdParty Whether the origin of the request and
128 * document differs
129 * @param {?string} sitekey The active sitekey if there is any 129 * @param {?string} sitekey The active sitekey if there is any
130 * @param {?boolean} specificOnly Whether generic filters should be ignored 130 * @param {?boolean} specificOnly Whether generic filters should be ignored
131 * @param {?BlockingFilter} filter The matched filter or null if there is no mat ch 131 * @param {?BlockingFilter} filter The matched filter or null if there is no
132 * match
132 */ 133 */
133 exports.logRequest = function(page, url, type, docDomain, 134 exports.logRequest = function(page, url, type, docDomain,
134 thirdParty, sitekey, 135 thirdParty, sitekey,
135 specificOnly, filter) 136 specificOnly, filter)
136 { 137 {
137 let panel = getActivePanel(page); 138 let panel = getActivePanel(page);
138 if (panel) 139 if (panel)
139 { 140 {
140 let request = { 141 addRecord(panel, {url, type, docDomain, thirdParty, sitekey, specificOnly},
141 url: url, 142 filter);
142 type: type,
143 docDomain: docDomain,
144 thirdParty: thirdParty,
145 sitekey: sitekey,
146 specificOnly: specificOnly
147 };
148
149 addRecord(panel, request, filter);
150 } 143 }
151 }; 144 };
152 145
153 /** 146 /**
154 * Logs active element hiding filters to the devtools panel. 147 * Logs active element hiding filters to the devtools panel.
155 * 148 *
156 * @param {Page} page The page the elements were hidden on 149 * @param {Page} page The page the elements were hidden on
157 * @param {string[]} selectors The CSS selectors of active elemhide filters 150 * @param {string[]} selectors The CSS selectors of active elemhide filters
158 * @param {string} docDomain The IDN-decoded hostname of the document 151 * @param {string} docDomain The IDN-decoded hostname of the document
159 */ 152 */
160 function logHiddenElements(page, selectors, docDomain) 153 function logHiddenElements(page, selectors, docDomain)
161 { 154 {
162 let panel = getActivePanel(page); 155 let panel = getActivePanel(page);
156 if (panel)
163 { 157 {
164 for (let subscription of FilterStorage.subscriptions) 158 for (let subscription of FilterStorage.subscriptions)
165 { 159 {
166 if (subscription.disabled) 160 if (subscription.disabled)
167 continue; 161 continue;
168 162
169 for (let filter of subscription.filters) 163 for (let filter of subscription.filters)
170 { 164 {
171 if (!(filter instanceof ElemHideFilter)) 165 if (!(filter instanceof ElemHideFilter))
172 continue; 166 continue;
173 if (selectors.indexOf(filter.selector) == -1) 167 if (selectors.indexOf(filter.selector) == -1)
174 continue; 168 continue;
175 if (!filter.isActiveOnDomain(docDomain)) 169 if (!filter.isActiveOnDomain(docDomain))
176 continue; 170 continue;
177 171
178 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); 172 addRecord(panel, {type: "ELEMHIDE", docDomain}, filter);
179 } 173 }
180 } 174 }
181 } 175 }
182 }; 176 }
183 177
184 /** 178 /**
185 * Logs a whitelisting filter, that disables (some kind of) 179 * Logs a whitelisting filter, that disables (some kind of)
186 * blocking for a particular document, to the devtools panel. 180 * blocking for a particular document, to the devtools panel.
187 * 181 *
188 * @param {Page} page The page the whitelisting is active on 182 * @param {Page} page The page the whitelisting is active on
189 * @param {string} url The url of the whitelisted document 183 * @param {string} url The url of the whitelisted document
190 * @param {number} typeMask The bit mask of whitelisting types checked fo r 184 * @param {number} typeMask The bit mask of whitelisting types checked
191 * @param {string} docDomain The IDN-decoded hostname of the parent docume nt 185 * for
186 * @param {string} docDomain The IDN-decoded hostname of the parent
187 * document
192 * @param {WhitelistFilter} filter The matched whitelisting filter 188 * @param {WhitelistFilter} filter The matched whitelisting filter
193 */ 189 */
194 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter ) 190 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain,
191 filter)
195 { 192 {
196 let panel = getActivePanel(page); 193 let panel = getActivePanel(page);
197 if (panel) 194 if (panel)
198 { 195 {
199 for (let type of nonRequestTypes) 196 for (let type of nonRequestTypes)
200 { 197 {
201 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) 198 if (typeMask & filter.contentType & RegExpFilter.typeMap[type])
202 addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter); 199 addRecord(panel, {url, type, docDomain}, filter);
203 } 200 }
204 } 201 }
205 }; 202 };
206 203
207 /** 204 /**
208 * Checks whether a page is inspected by the devtools panel. 205 * Checks whether a page is inspected by the devtools panel.
209 * 206 *
210 * @param {Page} page 207 * @param {Page} page
211 * @return {boolean} 208 * @return {boolean}
212 */ 209 */
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 { 323 {
327 updateFilters([filter], false); 324 updateFilters([filter], false);
328 } 325 }
329 326
330 function onSubscriptionAdded(subscription) 327 function onSubscriptionAdded(subscription)
331 { 328 {
332 if (subscription instanceof SpecialSubscription) 329 if (subscription instanceof SpecialSubscription)
333 updateFilters(subscription.filters, true); 330 updateFilters(subscription.filters, true);
334 } 331 }
335 332
336 chrome.runtime.onConnect.addListener(port => 333 chrome.runtime.onConnect.addListener(newPort =>
337 { 334 {
338 let match = port.name.match(/^devtools-(\d+)$/); 335 let match = newPort.name.match(/^devtools-(\d+)$/);
339 if (!match) 336 if (!match)
340 return; 337 return;
341 338
342 let inspectedTabId = parseInt(match[1], 10); 339 let inspectedTabId = parseInt(match[1], 10);
343 let localOnBeforeRequest = onBeforeRequest.bind(); 340 let localOnBeforeRequest = onBeforeRequest.bind();
344 341
345 chrome.webRequest.onBeforeRequest.addListener( 342 chrome.webRequest.onBeforeRequest.addListener(
346 localOnBeforeRequest, 343 localOnBeforeRequest,
347 { 344 {
348 urls: ["<all_urls>"], 345 urls: ["<all_urls>"],
349 types: ["main_frame"], 346 types: ["main_frame"],
350 tabId: inspectedTabId 347 tabId: inspectedTabId
351 } 348 }
352 ); 349 );
353 350
354 if (!hasPanels()) 351 if (!hasPanels())
355 { 352 {
356 ext.pages.onLoading.addListener(onLoading); 353 ext.pages.onLoading.addListener(onLoading);
357 FilterNotifier.on("filter.added", onFilterAdded); 354 FilterNotifier.on("filter.added", onFilterAdded);
358 FilterNotifier.on("filter.removed", onFilterRemoved); 355 FilterNotifier.on("filter.removed", onFilterRemoved);
359 FilterNotifier.on("subscription.added", onSubscriptionAdded); 356 FilterNotifier.on("subscription.added", onSubscriptionAdded);
360 } 357 }
361 358
362 port.onDisconnect.addListener(() => 359 newPort.onDisconnect.addListener(() =>
363 { 360 {
364 delete panels[inspectedTabId]; 361 delete panels[inspectedTabId];
365 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); 362 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
366 363
367 if (!hasPanels()) 364 if (!hasPanels())
368 { 365 {
369 ext.pages.onLoading.removeListener(onLoading); 366 ext.pages.onLoading.removeListener(onLoading);
370 FilterNotifier.off("filter.added", onFilterAdded); 367 FilterNotifier.off("filter.added", onFilterAdded);
371 FilterNotifier.off("filter.removed", onFilterRemoved); 368 FilterNotifier.off("filter.removed", onFilterRemoved);
372 FilterNotifier.off("subscription.added", onSubscriptionAdded); 369 FilterNotifier.off("subscription.added", onSubscriptionAdded);
373 } 370 }
374 }); 371 });
375 372
376 panels[inspectedTabId] = {port: port, records: []}; 373 panels[inspectedTabId] = {port: newPort, records: []};
377 }); 374 });
378 375
379 port.on("devtools.traceElemHide", (message, sender) => 376 port.on("devtools.traceElemHide", (message, sender) =>
380 { 377 {
381 logHiddenElements( 378 logHiddenElements(
382 sender.page, message.selectors, 379 sender.page, message.selectors,
383 extractHostFromFrame(sender.frame) 380 extractHostFromFrame(sender.frame)
384 ); 381 );
385 }); 382 });
OLDNEW
« no previous file with comments | « lib/compat.js ('k') | lib/filterComposer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld