LEFT | RIGHT |
(no file at all) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present 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 /** @module hitLogger */ |
| 19 |
| 20 "use strict"; |
| 21 |
| 22 const {extractHostFromFrame} = require("./url"); |
| 23 const {EventEmitter} = require("../adblockpluscore/lib/events"); |
| 24 const {FilterStorage} = require("../adblockpluscore/lib/filterStorage"); |
| 25 const {port} = require("./messaging"); |
| 26 const {RegExpFilter, |
| 27 ElemHideFilter} = require("../adblockpluscore/lib/filterClasses"); |
| 28 |
| 29 const nonRequestTypes = exports.nonRequestTypes = [ |
| 30 "DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE", "CSP" |
| 31 ]; |
| 32 |
| 33 let eventEmitter = new EventEmitter(); |
| 34 |
| 35 /** |
| 36 * @namespace |
| 37 * @static |
| 38 */ |
| 39 let HitLogger = exports.HitLogger = { |
| 40 /** |
| 41 * Adds a listener for requests, filter hits etc related to the tab. |
| 42 * |
| 43 * Note: Calling code is responsible for removing the listener again, |
| 44 * it will not be automatically removed when the tab is closed. |
| 45 * |
| 46 * @param {number} tabId |
| 47 * @param {function} listener |
| 48 */ |
| 49 addListener: eventEmitter.on.bind(eventEmitter), |
| 50 |
| 51 /** |
| 52 * Removes a listener for the tab. |
| 53 * |
| 54 * @param {number} tabId |
| 55 * @param {function} listener |
| 56 */ |
| 57 removeListener: eventEmitter.off.bind(eventEmitter), |
| 58 |
| 59 /** |
| 60 * Checks whether a tab is being inspected by anything. |
| 61 * |
| 62 * @param {number} tabId |
| 63 * @return {boolean} |
| 64 */ |
| 65 hasListener(tabId) |
| 66 { |
| 67 let listeners = eventEmitter._listeners.get(tabId); |
| 68 return listeners && listeners.length > 0; |
| 69 } |
| 70 }; |
| 71 |
| 72 /** |
| 73 * Logs a request associated with a tab or multiple tabs. |
| 74 * |
| 75 * @param {number[]} tabIds |
| 76 * The tabIds associated with the request |
| 77 * @param {Object} request |
| 78 * The request to log |
| 79 * @param {string} request.url |
| 80 * The URL of the request |
| 81 * @param {string} request.type |
| 82 * The request type |
| 83 * @param {string} request.docDomain |
| 84 * The hostname of the document |
| 85 * @param {boolean} request.thirdParty |
| 86 * Whether the origin of the request and document differs |
| 87 * @param {?string} request.sitekey |
| 88 * The active sitekey if there is any |
| 89 * @param {?boolean} request.specificOnly |
| 90 * Whether generic filters should be ignored |
| 91 * @param {?BlockingFilter} filter |
| 92 * The matched filter or null if there is no match |
| 93 */ |
| 94 exports.logRequest = (tabIds, request, filter) => |
| 95 { |
| 96 for (let tabId of tabIds) |
| 97 eventEmitter.emit(tabId, request, filter); |
| 98 }; |
| 99 |
| 100 /** |
| 101 * Logs active element hiding filters for a tab. |
| 102 * |
| 103 * @param {number} tabId The ID of the tab, the elements were hidden in |
| 104 * @param {string[]} selectors The selectors of applied ElemHideFilters |
| 105 * @param {string[]} filters The text of applied ElemHideEmulationFilters |
| 106 * @param {string} docDomain The hostname of the document |
| 107 */ |
| 108 function logHiddenElements(tabId, selectors, filters, docDomain) |
| 109 { |
| 110 if (HitLogger.hasListener(tabId)) |
| 111 { |
| 112 for (let subscription of FilterStorage.subscriptions) |
| 113 { |
| 114 if (subscription.disabled) |
| 115 continue; |
| 116 |
| 117 for (let filter of subscription.filters) |
| 118 { |
| 119 // We only know the exact filter in case of element hiding emulation. |
| 120 // For regular element hiding filters, the content script only knows |
| 121 // the selector, so we have to find a filter that has an identical |
| 122 // selector and is active on the domain the match was reported from. |
| 123 let isActiveElemHideFilter = filter instanceof ElemHideFilter && |
| 124 selectors.includes(filter.selector) && |
| 125 filter.isActiveOnDomain(docDomain); |
| 126 |
| 127 if (isActiveElemHideFilter || filters.includes(filter.text)) |
| 128 eventEmitter.emit(tabId, {type: "ELEMHIDE", docDomain}, filter); |
| 129 } |
| 130 } |
| 131 } |
| 132 } |
| 133 |
| 134 /** |
| 135 * Logs a whitelisting filter that disables (some kind of) |
| 136 * blocking for a particular document. |
| 137 * |
| 138 * @param {number} tabId The tabId the whitelisting is active for |
| 139 * @param {string} url The url of the whitelisted document |
| 140 * @param {number} typeMask The bit mask of whitelisting types checked |
| 141 * for |
| 142 * @param {string} docDomain The hostname of the parent document |
| 143 * @param {WhitelistFilter} filter The matched whitelisting filter |
| 144 */ |
| 145 exports.logWhitelistedDocument = (tabId, url, typeMask, docDomain, filter) => |
| 146 { |
| 147 if (HitLogger.hasListener(tabId)) |
| 148 { |
| 149 for (let type of nonRequestTypes) |
| 150 { |
| 151 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) |
| 152 eventEmitter.emit(tabId, {url, type, docDomain}, filter); |
| 153 } |
| 154 } |
| 155 }; |
| 156 |
| 157 port.on("hitLogger.traceElemHide", (message, sender) => |
| 158 { |
| 159 logHiddenElements( |
| 160 sender.page.id, message.selectors, message.filters, |
| 161 extractHostFromFrame(sender.frame) |
| 162 ); |
| 163 }); |
LEFT | RIGHT |