OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 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 let info = require("info"); |
| 19 if (info.platform == "chromium") |
| 20 { |
| 21 let {WhitelistFilter} = require("filterClasses"); |
| 22 let {SpecialSubscription} = require("subscriptionClasses"); |
| 23 let {Filter, ElemHideFilter} = require("filterClasses"); |
| 24 let {FilterStorage} = require("filterStorage"); |
| 25 let {defaultMatcher} = require("matcher"); |
| 26 let {FilterNotifier} = require("filterNotifier"); |
| 27 |
| 28 let panels = Object.create(null); |
| 29 |
| 30 function getFilterInfo(filter) |
| 31 { |
| 32 if (!filter) |
| 33 return null; |
| 34 |
| 35 let userDefined = false; |
| 36 let subscriptionTitle = null; |
| 37 |
| 38 for (let subscription of filter.subscriptions) |
| 39 { |
| 40 if (!subscription.disabled) |
| 41 { |
| 42 if (subscription instanceof SpecialSubscription) |
| 43 userDefined = true; |
| 44 else |
| 45 subscriptionTitle = subscription.title; |
| 46 } |
| 47 } |
| 48 |
| 49 return { |
| 50 text: filter.text, |
| 51 whitelisted: filter instanceof WhitelistFilter, |
| 52 userDefined: userDefined, |
| 53 subscription: subscriptionTitle |
| 54 }; |
| 55 } |
| 56 |
| 57 function addRecord(panel, request, filter) |
| 58 { |
| 59 panel.port.postMessage({ |
| 60 type: "add-record", |
| 61 request: request, |
| 62 filter: getFilterInfo(filter) |
| 63 }); |
| 64 |
| 65 panel.records.push({ |
| 66 request: request, |
| 67 filter: filter |
| 68 }); |
| 69 } |
| 70 |
| 71 function matchRequest(request) |
| 72 { |
| 73 return defaultMatcher.matchesAny( |
| 74 request.url, |
| 75 request.type, |
| 76 request.docDomain, |
| 77 isThirdParty( |
| 78 extractHostFromURL(request.url), |
| 79 request.docDomain |
| 80 ), |
| 81 request.sitekey |
| 82 ); |
| 83 } |
| 84 |
| 85 function logRequest(tabId, url, type, docDomain, sitekey, filter) |
| 86 { |
| 87 let panel = panels[tabId]; |
| 88 if (panel) |
| 89 { |
| 90 let request = { |
| 91 url: url, |
| 92 type: type, |
| 93 docDomain: docDomain, |
| 94 sitekey: sitekey |
| 95 }; |
| 96 |
| 97 addRecord(panel, request, filter); |
| 98 } |
| 99 } |
| 100 exports.logRequest = logRequest; |
| 101 |
| 102 function logHiddenElements(tabId, selectors, docDomain) |
| 103 { |
| 104 let panel = panels[tabId]; |
| 105 if (panel) |
| 106 { |
| 107 for (let subscription of FilterStorage.subscriptions) |
| 108 { |
| 109 if (subscription.disabled) |
| 110 continue; |
| 111 |
| 112 for (let filter of subscription.filters) |
| 113 { |
| 114 if (!(filter instanceof ElemHideFilter)) |
| 115 continue; |
| 116 if (selectors.indexOf(filter.selector) == -1) |
| 117 continue; |
| 118 if (!filter.isActiveOnDomain(docDomain)) |
| 119 continue; |
| 120 if (panel.records.some(record => record.request.type == "ELEMHIDE" && |
| 121 record.request.docDomain == docDomain
&& |
| 122 record.filter.selector == filter.sele
ctor)) |
| 123 continue; |
| 124 |
| 125 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); |
| 126 } |
| 127 } |
| 128 } |
| 129 }; |
| 130 exports.logHiddenElements = logHiddenElements; |
| 131 |
| 132 function hasPanel(page) |
| 133 { |
| 134 return page._id in panels; |
| 135 } |
| 136 exports.hasPanel = hasPanel; |
| 137 |
| 138 function onMessage(msg) |
| 139 { |
| 140 let filter = Filter.fromText(msg.filter); |
| 141 |
| 142 switch (msg.action) |
| 143 { |
| 144 case "add": |
| 145 FilterStorage.addFilter(filter); |
| 146 break; |
| 147 |
| 148 case "remove": |
| 149 FilterStorage.removeFilter(filter); |
| 150 break; |
| 151 } |
| 152 } |
| 153 |
| 154 chrome.runtime.onConnect.addListener(function(port) |
| 155 { |
| 156 let match = port.name.match(/^devtools-(\d+)$/); |
| 157 if (match) |
| 158 { |
| 159 let tabId = match[1]; |
| 160 panels[tabId] = {port: port, records: []}; |
| 161 |
| 162 port.onMessage.addListener(onMessage); |
| 163 port.onDisconnect.addListener(() => delete panels[tabId]); |
| 164 } |
| 165 }); |
| 166 |
| 167 chrome.webNavigation.onBeforeNavigate.addListener(function(details) |
| 168 { |
| 169 let panel = panels[details.tabId]; |
| 170 if (panel && details.frameId == 0) |
| 171 { |
| 172 // We have to flush the in-memory cache on page load. |
| 173 // Otherwise requests answered from the in-memory cache |
| 174 // will not be shown in the devtools panel. |
| 175 chrome.webRequest.handlerBehaviorChanged(); |
| 176 |
| 177 panel.records = []; |
| 178 panel.port.postMessage({type: "reset"}); |
| 179 } |
| 180 }); |
| 181 |
| 182 FilterNotifier.addListener(function(action, filter) |
| 183 { |
| 184 if (action != "filter.added" && action != "filter.removed") |
| 185 return; |
| 186 |
| 187 for (let tabId in panels) |
| 188 { |
| 189 let panel = panels[tabId]; |
| 190 |
| 191 for (let i = 0; i < panel.records.length; i++) |
| 192 { |
| 193 let record = panel.records[i]; |
| 194 |
| 195 if (action == "filter.added") |
| 196 { |
| 197 if (record.request.type == "ELEMHIDE") |
| 198 continue; |
| 199 |
| 200 if (matchRequest(record.request) != filter) |
| 201 continue; |
| 202 |
| 203 record.filter = filter; |
| 204 } |
| 205 |
| 206 if (action == "filter.removed") |
| 207 { |
| 208 if (record.filter != filter) |
| 209 continue; |
| 210 |
| 211 if (record.request.type == "ELEMHIDE") |
| 212 { |
| 213 panel.port.postMessage({ |
| 214 type: "remove-record", |
| 215 index: i |
| 216 }); |
| 217 panel.records.splice(i--, 1); |
| 218 continue; |
| 219 } |
| 220 |
| 221 record.filter = matchRequest(record.request); |
| 222 } |
| 223 |
| 224 panel.port.postMessage({ |
| 225 type: "update-record", |
| 226 index: i, |
| 227 request: record.request, |
| 228 filter: getFilterInfo(record.filter) |
| 229 }); |
| 230 } |
| 231 } |
| 232 }); |
| 233 } |
| 234 else |
| 235 { |
| 236 exports.logRequest = () => {}; |
| 237 exports.logHiddenElements = () => {}; |
| 238 exports.hasPanel = () => false; |
| 239 } |
OLD | NEW |