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