OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 "use strict"; |
| 19 |
| 20 let {RegExpFilter, WhitelistFilter, ElemHideFilter} = require("filterClasses"); |
| 21 let {SpecialSubscription} = require("subscriptionClasses"); |
| 22 let {FilterStorage} = require("filterStorage"); |
| 23 let {defaultMatcher} = require("matcher"); |
| 24 let {FilterNotifier} = require("filterNotifier"); |
| 25 |
| 26 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; |
| 27 |
| 28 // Mapping of inspected tabs to their devpanel page |
| 29 // and recorded items. We can't use a PageMap here, |
| 30 // because data must persist after navigation/reload. |
| 31 let panels = Object.create(null); |
| 32 |
| 33 function hasPanels() |
| 34 { |
| 35 return Object.keys(panels).length > 0; |
| 36 } |
| 37 |
| 38 function getActivePanel(page) |
| 39 { |
| 40 let panel = panels[page._id]; |
| 41 if(panel && !panel.reload && !panel.reloading) |
| 42 return panel; |
| 43 return null; |
| 44 } |
| 45 |
| 46 function getFilterInfo(filter) |
| 47 { |
| 48 if (!filter) |
| 49 return null; |
| 50 |
| 51 let userDefined = false; |
| 52 let subscriptionTitle = null; |
| 53 |
| 54 for (let subscription of filter.subscriptions) |
| 55 { |
| 56 if (!subscription.disabled) |
| 57 { |
| 58 if (subscription instanceof SpecialSubscription) |
| 59 userDefined = true; |
| 60 else |
| 61 subscriptionTitle = subscription.title; |
| 62 } |
| 63 } |
| 64 |
| 65 return { |
| 66 text: filter.text, |
| 67 whitelisted: filter instanceof WhitelistFilter, |
| 68 userDefined: userDefined, |
| 69 subscription: subscriptionTitle |
| 70 }; |
| 71 } |
| 72 |
| 73 function hasRecord(panel, request, filter) |
| 74 { |
| 75 return panel.records.some(record => |
| 76 record.request.url == request.url && |
| 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) && |
| 83 |
| 84 // Matched element hiding filters don't relate to a particular request, |
| 85 // so we also have to match the CSS selector in order to distinguish them. |
| 86 (record.filter && record.filter.selector) == (filter && filter.selector) |
| 87 ); |
| 88 } |
| 89 |
| 90 function addRecord(panel, request, filter) |
| 91 { |
| 92 if (!hasRecord(panel, request, filter)) |
| 93 { |
| 94 panel.port.postMessage({ |
| 95 type: "add-record", |
| 96 request: request, |
| 97 filter: getFilterInfo(filter) |
| 98 }); |
| 99 |
| 100 panel.records.push({ |
| 101 request: request, |
| 102 filter: filter |
| 103 }); |
| 104 } |
| 105 } |
| 106 |
| 107 function matchRequest(request) |
| 108 { |
| 109 return defaultMatcher.matchesAny( |
| 110 request.url, |
| 111 RegExpFilter.typeMap[request.type], |
| 112 request.docDomain, |
| 113 request.thirdParty, |
| 114 request.sitekey, |
| 115 request.specificOnly |
| 116 ); |
| 117 } |
| 118 |
| 119 /** |
| 120 * Logs a request to the devtools panel. |
| 121 * |
| 122 * @param {Page} page The page the request occured on |
| 123 * @param {string} url The URL of the request |
| 124 * @param {string} type The request type |
| 125 * @param {string} docDomain The IDN-decoded hostname of the document |
| 126 * @param {boolean} thirdParty Whether the origin of the request and documen
t differs |
| 127 * @param {?string} sitekey The active sitekey if there is any |
| 128 * @param {?boolean} specificOnly Whether generic filters should be ignored |
| 129 * @param {?BlockingFilter} filter The matched filter or null if there is no mat
ch |
| 130 */ |
| 131 exports.logRequest = function(page, url, type, docDomain, |
| 132 thirdParty, sitekey, |
| 133 specificOnly, filter) |
| 134 { |
| 135 let panel = getActivePanel(page); |
| 136 if (panel) |
| 137 { |
| 138 let request = { |
| 139 url: url, |
| 140 type: type, |
| 141 docDomain: docDomain, |
| 142 thirdParty: thirdParty, |
| 143 sitekey: sitekey, |
| 144 specificOnly: specificOnly |
| 145 }; |
| 146 |
| 147 addRecord(panel, request, filter); |
| 148 } |
| 149 }; |
| 150 |
| 151 /** |
| 152 * Logs active element hiding filters to the devtools panel. |
| 153 * |
| 154 * @param {Page} page The page the elements were hidden on |
| 155 * @param {string[]} selectors The CSS selectors of active elemhide filters |
| 156 * @param {string} docDomain The IDN-decoded hostname of the document |
| 157 */ |
| 158 exports.logHiddenElements = function(page, selectors, docDomain) |
| 159 { |
| 160 let panel = getActivePanel(page); |
| 161 { |
| 162 for (let subscription of FilterStorage.subscriptions) |
| 163 { |
| 164 if (subscription.disabled) |
| 165 continue; |
| 166 |
| 167 for (let filter of subscription.filters) |
| 168 { |
| 169 if (!(filter instanceof ElemHideFilter)) |
| 170 continue; |
| 171 if (selectors.indexOf(filter.selector) == -1) |
| 172 continue; |
| 173 if (!filter.isActiveOnDomain(docDomain)) |
| 174 continue; |
| 175 |
| 176 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); |
| 177 } |
| 178 } |
| 179 } |
| 180 }; |
| 181 |
| 182 /** |
| 183 * Logs a whitelisting filter, that disables (some kind of) |
| 184 * blocking for a particular document, to the devtools panel. |
| 185 * |
| 186 * @param {Page} page The page the whitelisting is active on |
| 187 * @param {string} url The url of the whitelisted document |
| 188 * @param {number} typeMask The bit mask of whitelisting types checked fo
r |
| 189 * @param {string} docDomain The IDN-decoded hostname of the parent docume
nt |
| 190 * @param {WhitelistFilter} filter The matched whitelisting filter |
| 191 */ |
| 192 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter
) |
| 193 { |
| 194 let panel = getActivePanel(page); |
| 195 if (panel) |
| 196 { |
| 197 for (let type of nonRequestTypes) |
| 198 { |
| 199 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) |
| 200 addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter); |
| 201 } |
| 202 } |
| 203 }; |
| 204 |
| 205 /** |
| 206 * Checks whether a page is inspected by the devtools panel. |
| 207 * |
| 208 * @param {Page} page |
| 209 * @return {boolean} |
| 210 */ |
| 211 exports.hasPanel = function(page) |
| 212 { |
| 213 return page._id in panels; |
| 214 }; |
| 215 |
| 216 function onBeforeRequest(details) |
| 217 { |
| 218 let panel = panels[details.tabId]; |
| 219 |
| 220 // Clear the devtools panel and reload the inspected tab without caching |
| 221 // when a new request is issued. However, make sure that we don't end up |
| 222 // in an infinite recursion if we already triggered a reload. |
| 223 if (panel.reloading) |
| 224 { |
| 225 panel.reloading = false; |
| 226 } |
| 227 else |
| 228 { |
| 229 panel.records = []; |
| 230 panel.port.postMessage({type: "reset"}); |
| 231 |
| 232 // We can't repeat the request if it isn't a GET request. Chrome would |
| 233 // prompt the user to confirm reloading the page, and POST requests are |
| 234 // known to cause issues on many websites if repeated. |
| 235 if (details.method == "GET") |
| 236 panel.reload = true; |
| 237 } |
| 238 } |
| 239 |
| 240 function onLoading(page) |
| 241 { |
| 242 let tabId = page._id; |
| 243 let panel = panels[tabId]; |
| 244 |
| 245 // Reloading the tab is the only way that allows bypassing all caches, in |
| 246 // order to see all requests in the devtools panel. Reloading must not be |
| 247 // performed before the tab changes to "loading", otherwise it will load the |
| 248 // previous URL. |
| 249 if (panel && panel.reload) |
| 250 { |
| 251 chrome.tabs.reload(tabId, {bypassCache: true}); |
| 252 |
| 253 panel.reload = false; |
| 254 panel.reloading = true; |
| 255 } |
| 256 } |
| 257 |
| 258 function onFilterChange(action, arg) |
| 259 { |
| 260 let added, filters; |
| 261 switch (action) |
| 262 { |
| 263 case "filter.added": |
| 264 added = true; |
| 265 filters = [arg]; |
| 266 break; |
| 267 |
| 268 case "filter.removed": |
| 269 added = false; |
| 270 filters = [arg]; |
| 271 break; |
| 272 |
| 273 // When there haven't ever been any user filters before, the subscription is |
| 274 // added, triggering a "subscription.added" instead of a "filter.added" even
t. |
| 275 case "subscription.added": |
| 276 if (arg instanceof SpecialSubscription) |
| 277 { |
| 278 added = true; |
| 279 filters = arg.filters; |
| 280 break; |
| 281 } |
| 282 |
| 283 default: |
| 284 return; |
| 285 } |
| 286 |
| 287 for (let tabId in panels) |
| 288 { |
| 289 let panel = panels[tabId]; |
| 290 |
| 291 for (let i = 0; i < panel.records.length; i++) |
| 292 { |
| 293 let record = panel.records[i]; |
| 294 |
| 295 // If an added filter matches a request shown in the devtools panel, |
| 296 // update that record to show the new filter. Ignore filters that aren't |
| 297 // associated with any sub-resource request. There is no record for these |
| 298 // if they don't already match. In particular, in case of element hiding |
| 299 // filters, we also wouldn't know if any new element matches. |
| 300 if (added) |
| 301 { |
| 302 if (nonRequestTypes.indexOf(record.request.type) != -1) |
| 303 continue; |
| 304 |
| 305 let filter = matchRequest(record.request); |
| 306 if (filters.indexOf(filter) == -1) |
| 307 continue; |
| 308 |
| 309 record.filter = filter; |
| 310 } |
| 311 |
| 312 // If a filter shown in the devtools panel got removed, update that |
| 313 // record to show the filter that matches now, or none, instead. |
| 314 // For filters that aren't associated with any sub-resource request, |
| 315 // just remove the record. We wouldn't know whether another filter |
| 316 // matches instead until the page is reloaded. |
| 317 else |
| 318 { |
| 319 if (filters.indexOf(record.filter) == -1) |
| 320 continue; |
| 321 |
| 322 if (nonRequestTypes.indexOf(record.request.type) != -1) |
| 323 { |
| 324 panel.port.postMessage({ |
| 325 type: "remove-record", |
| 326 index: i |
| 327 }); |
| 328 panel.records.splice(i--, 1); |
| 329 continue; |
| 330 } |
| 331 |
| 332 record.filter = matchRequest(record.request); |
| 333 } |
| 334 |
| 335 panel.port.postMessage({ |
| 336 type: "update-record", |
| 337 index: i, |
| 338 request: record.request, |
| 339 filter: getFilterInfo(record.filter) |
| 340 }); |
| 341 } |
| 342 } |
| 343 } |
| 344 |
| 345 chrome.runtime.onConnect.addListener(port => |
| 346 { |
| 347 let match = port.name.match(/^devtools-(\d+)$/); |
| 348 if (!match) |
| 349 return; |
| 350 |
| 351 let inspectedTabId = parseInt(match[1], 10); |
| 352 let localOnBeforeRequest = onBeforeRequest.bind(); |
| 353 |
| 354 chrome.webRequest.onBeforeRequest.addListener( |
| 355 localOnBeforeRequest, |
| 356 { |
| 357 urls: ["<all_urls>"], |
| 358 types: ["main_frame"], |
| 359 tabId: inspectedTabId |
| 360 } |
| 361 ); |
| 362 |
| 363 if (!hasPanels()) |
| 364 { |
| 365 ext.pages.onLoading.addListener(onLoading); |
| 366 FilterNotifier.addListener(onFilterChange); |
| 367 } |
| 368 |
| 369 port.onDisconnect.addListener(() => |
| 370 { |
| 371 delete panels[inspectedTabId]; |
| 372 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); |
| 373 |
| 374 if (!hasPanels()) |
| 375 { |
| 376 FilterNotifier.removeListener(onFilterChange); |
| 377 ext.pages.onLoading.removeListener(onLoading); |
| 378 } |
| 379 }); |
| 380 |
| 381 panels[inspectedTabId] = {port: port, records: []}; |
| 382 }); |
OLD | NEW |