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

Delta Between Two Patch Sets: lib/devtools.js

Issue 6393086494113792: Issue 154 - Added devtools panel showing blocked and blockable items (Closed)
Left Patch Set: Addressed comments, fixed popup blocking detection, log whitelisted documents Created Feb. 2, 2016, 10:38 a.m.
Right Patch Set: Adapt for UI changes generating domain specific filters when necessary Created Feb. 3, 2016, 10:40 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « include.preload.js ('k') | lib/filterComposer.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 14 matching lines...) Expand all
25 25
26 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; 26 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"];
27 27
28 // Mapping of inspected tabs to their devpanel page 28 // Mapping of inspected tabs to their devpanel page
29 // and recorded items. We can't use a PageMap here, 29 // and recorded items. We can't use a PageMap here,
30 // because data must persist after navigation/reload. 30 // because data must persist after navigation/reload.
31 let panels = Object.create(null); 31 let panels = Object.create(null);
32 32
33 function hasPanels() 33 function hasPanels()
34 { 34 {
35 return Object.getOwnPropertyNames(panels).length > 0; 35 return Object.keys(panels).length > 0;
36 } 36 }
37 37
38 function getActivePanel(page) 38 function getActivePanel(page)
39 { 39 {
40 let panel = panels[page._id]; 40 let panel = panels[page._id];
41 if(panel && !panel.reload && !panel.reloading) 41 if(panel && !panel.reload && !panel.reloading)
42 return panel; 42 return panel;
43 return null; 43 return null;
44 }
45
46 function getRequestInfo(request)
47 {
48 return {
49 url: request.url,
50 type: request.type,
51 docDomain: request.docDomain
52 };
53 } 44 }
54 45
55 function getFilterInfo(filter) 46 function getFilterInfo(filter)
56 { 47 {
57 if (!filter) 48 if (!filter)
58 return null; 49 return null;
59 50
60 let userDefined = false; 51 let userDefined = false;
61 let subscriptionTitle = null; 52 let subscriptionTitle = null;
62 53
(...skipping 12 matching lines...) Expand all
75 text: filter.text, 66 text: filter.text,
76 whitelisted: filter instanceof WhitelistFilter, 67 whitelisted: filter instanceof WhitelistFilter,
77 userDefined: userDefined, 68 userDefined: userDefined,
78 subscription: subscriptionTitle 69 subscription: subscriptionTitle
79 }; 70 };
80 } 71 }
81 72
82 function hasRecord(panel, request, filter) 73 function hasRecord(panel, request, filter)
83 { 74 {
84 return panel.records.some(record => 75 return panel.records.some(record =>
85 record.request.type == request.type &&
86 record.request.url == request.url && 76 record.request.url == request.url &&
87 record.request.docDomain == request.docDomain && 77 record.request.docDomain == request.docDomain &&
78
79 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already
80 // a DOCUMENT exception which disables all means of blocking.
81 (record.request.type == "DOCUMENT" ? nonRequestTypes.indexOf(request.type) ! = -1
82 : record.request.type == request.type) &&
88 83
89 // Matched element hiding filters don't relate to a particular request, 84 // Matched element hiding filters don't relate to a particular request,
90 // so we also have to match the CSS selector in order to distinguish them. 85 // so we also have to match the CSS selector in order to distinguish them.
91 (record.filter && record.filter.selector) == (filter && filter.selector) 86 (record.filter && record.filter.selector) == (filter && filter.selector)
92 ); 87 );
93 } 88 }
94 89
95 function addRecord(panel, request, filter) 90 function addRecord(panel, request, filter)
96 { 91 {
97 if (!hasRecord(panel, request, filter)) 92 if (!hasRecord(panel, request, filter))
98 { 93 {
99 panel.panel.sendMessage({ 94 panel.port.postMessage({
100 type: "add-record", 95 type: "add-record",
101 request: getRequestInfo(request), 96 request: request,
102 filter: getFilterInfo(filter) 97 filter: getFilterInfo(filter)
103 }); 98 });
104 99
105 panel.records.push({ 100 panel.records.push({
106 request: request, 101 request: request,
107 filter: filter 102 filter: filter
108 }); 103 });
109 } 104 }
110 } 105 }
111 106
112 function matchRequest(request) 107 function matchRequest(request)
113 { 108 {
114 return defaultMatcher.matchesAny( 109 return defaultMatcher.matchesAny(
115 request.url, 110 request.url,
116 RegExpFilter.typeMap[request.type], 111 RegExpFilter.typeMap[request.type],
117 request.docDomain, 112 request.docDomain,
118 request.thirdParty, 113 request.thirdParty,
119 request.sitekey, 114 request.sitekey,
120 request.specificOnly 115 request.specificOnly
121 ); 116 );
122 } 117 }
123 118
124 /** 119 /**
125 * Logs a request to the devtools panel. 120 * Logs a request to the devtools panel.
126 * 121 *
127 * @param {Page} page The page the request occured on 122 * @param {Page} page The page the request occured on
128 * @param {string] url The URL of the request 123 * @param {string} url The URL of the request
129 * @param {string} type The request type 124 * @param {string} type The request type
130 * @param {string} docDomain The IDN-decoded hostname of the document 125 * @param {string} docDomain The IDN-decoded hostname of the document
131 * @param {boolean} thirdParty Whether the origin of the request and docume nt differs 126 * @param {boolean} thirdParty Whether the origin of the request and documen t differs
132 * @param {string} [sitekey] The active sitekey if there is any 127 * @param {?string} sitekey The active sitekey if there is any
133 * @param {boolean} [specificOnly] Whether generic filters should be ignored 128 * @param {?boolean} specificOnly Whether generic filters should be ignored
134 * @param {BlockingFilter} [filter] The matched filter or null if there is no ma tch 129 * @param {?BlockingFilter} filter The matched filter or null if there is no mat ch
135 */ 130 */
136 exports.logRequest = function(page, url, type, docDomain, 131 exports.logRequest = function(page, url, type, docDomain,
137 thirdParty, sitekey, 132 thirdParty, sitekey,
138 » » » » » » » specificOnly, filter) 133 specificOnly, filter)
139 { 134 {
140 let panel = getActivePanel(page); 135 let panel = getActivePanel(page);
141 if (panel) 136 if (panel)
142 { 137 {
143 let request = { 138 let request = {
144 url: url, 139 url: url,
145 type: type, 140 type: type,
146 docDomain: docDomain, 141 docDomain: docDomain,
147 thirdParty: thirdParty, 142 thirdParty: thirdParty,
148 sitekey: sitekey, 143 sitekey: sitekey,
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 // Clear the devtools panel and reload the inspected tab without caching 220 // Clear the devtools panel and reload the inspected tab without caching
226 // when a new request is issued. However, make sure that we don't end up 221 // when a new request is issued. However, make sure that we don't end up
227 // in an infinite recursion if we already triggered a reload. 222 // in an infinite recursion if we already triggered a reload.
228 if (panel.reloading) 223 if (panel.reloading)
229 { 224 {
230 panel.reloading = false; 225 panel.reloading = false;
231 } 226 }
232 else 227 else
233 { 228 {
234 panel.records = []; 229 panel.records = [];
235 panel.panel.sendMessage({type: "reset"}); 230 panel.port.postMessage({type: "reset"});
236 231
237 // We can't repeat the request if it isn't a GET request. Chrome would 232 // We can't repeat the request if it isn't a GET request. Chrome would
238 // prompt the user to confirm reloading the page, and POST requests are 233 // prompt the user to confirm reloading the page, and POST requests are
239 // known to cause issues on many websites if repeated. 234 // known to cause issues on many websites if repeated.
240 if (details.method == "GET") 235 if (details.method == "GET")
241 panel.reload = true; 236 panel.reload = true;
242 } 237 }
243 } 238 }
244 239
245 function onLoading(page) 240 function onLoading(page)
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 // For filters that aren't associated with any sub-resource request, 314 // For filters that aren't associated with any sub-resource request,
320 // just remove the record. We wouldn't know whether another filter 315 // just remove the record. We wouldn't know whether another filter
321 // matches instead until the page is reloaded. 316 // matches instead until the page is reloaded.
322 else 317 else
323 { 318 {
324 if (filters.indexOf(record.filter) == -1) 319 if (filters.indexOf(record.filter) == -1)
325 continue; 320 continue;
326 321
327 if (nonRequestTypes.indexOf(record.request.type) != -1) 322 if (nonRequestTypes.indexOf(record.request.type) != -1)
328 { 323 {
329 panel.panel.sendMessage({ 324 panel.port.postMessage({
330 type: "remove-record", 325 type: "remove-record",
331 index: i 326 index: i
332 }); 327 });
333 panel.records.splice(i--, 1); 328 panel.records.splice(i--, 1);
334 continue; 329 continue;
335 } 330 }
336 331
337 record.filter = matchRequest(record.request); 332 record.filter = matchRequest(record.request);
338 } 333 }
339 334
340 panel.panel.sendMessage({ 335 panel.port.postMessage({
341 type: "update-record", 336 type: "update-record",
342 index: i, 337 index: i,
343 request: getRequestInfo(record.request), 338 request: record.request,
344 filter: getFilterInfo(record.filter) 339 filter: getFilterInfo(record.filter)
345 }); 340 });
346 } 341 }
347 } 342 }
348 } 343 }
349 344
350 ext.devtools.onCreated.addListener(function(panel) 345 chrome.runtime.onConnect.addListener(port =>
351 { 346 {
352 let inspectedTabId = panel.inspectedTabId; 347 let match = port.name.match(/^devtools-(\d+)$/);
348 if (!match)
349 return;
350
351 let inspectedTabId = parseInt(match[1], 10);
353 let localOnBeforeRequest = onBeforeRequest.bind(); 352 let localOnBeforeRequest = onBeforeRequest.bind();
354 353
355 chrome.webRequest.onBeforeRequest.addListener( 354 chrome.webRequest.onBeforeRequest.addListener(
356 localOnBeforeRequest, 355 localOnBeforeRequest,
357 { 356 {
358 urls: ["<all_urls>"], 357 urls: ["<all_urls>"],
359 types: ["main_frame"], 358 types: ["main_frame"],
360 tabId: inspectedTabId 359 tabId: inspectedTabId
361 } 360 }
362 ); 361 );
363 362
364 if (!hasPanels()) 363 if (!hasPanels())
365 { 364 {
366 ext.pages.onLoading.addListener(onLoading); 365 ext.pages.onLoading.addListener(onLoading);
367 FilterNotifier.addListener(onFilterChange); 366 FilterNotifier.addListener(onFilterChange);
368 } 367 }
369 368
370 panel.onRemoved.addListener(function() 369 port.onDisconnect.addListener(() =>
371 { 370 {
372 delete panels[inspectedTabId]; 371 delete panels[inspectedTabId];
373 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); 372 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
374 373
375 if (!hasPanels()) 374 if (!hasPanels())
376 { 375 {
377 FilterNotifier.removeListener(onFilterChange); 376 FilterNotifier.removeListener(onFilterChange);
378 ext.pages.onLoading.removeListener(onLoading); 377 ext.pages.onLoading.removeListener(onLoading);
379 } 378 }
380 }); 379 });
381 380
382 panels[inspectedTabId] = {panel: panel, records: []}; 381 panels[inspectedTabId] = {port: port, records: []};
383 }); 382 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld