| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 11 matching lines...) Expand all Loading... | |
| 22 const {Filter, RegExpFilter, BlockingFilter} = require("filterClasses"); | 22 const {Filter, RegExpFilter, BlockingFilter} = require("filterClasses"); |
| 23 const {Subscription} = require("subscriptionClasses"); | 23 const {Subscription} = require("subscriptionClasses"); |
| 24 const {defaultMatcher} = require("matcher"); | 24 const {defaultMatcher} = require("matcher"); |
| 25 const {FilterNotifier} = require("filterNotifier"); | 25 const {FilterNotifier} = require("filterNotifier"); |
| 26 const {Prefs} = require("prefs"); | 26 const {Prefs} = require("prefs"); |
| 27 const {checkWhitelisted, getKey} = require("whitelisting"); | 27 const {checkWhitelisted, getKey} = require("whitelisting"); |
| 28 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("url"); | 28 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("url"); |
| 29 const {port} = require("messaging"); | 29 const {port} = require("messaging"); |
| 30 const devtools = require("devtools"); | 30 const devtools = require("devtools"); |
| 31 | 31 |
| 32 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests. | 32 // Chrome and Firefox (WebExtensions) can't distinguish between |
| 33 // OBJECT_SUBREQUEST and OBJECT requests. | |
| 33 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; | 34 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; |
| 34 | 35 |
| 36 let resourceTypeMapping = new Map([ | |
| 37 ["beacon", "PING"], | |
| 38 ["imageset", "IMAGE"], | |
| 39 ["sub_frame", "SUBDOCUMENT"] | |
| 40 ]); | |
|
Sebastian Noack
2017/05/19 11:30:46
How about using a mapping like below?
let resou
Manish Jethani
2017/05/19 16:54:42
Yes, that's two lookups now instead of one for the
| |
| 41 | |
| 42 let typeMasks = new Map( | |
| 43 Object.keys(chrome.webRequest.ResourceType) | |
| 44 .map(typeKey => chrome.webRequest.ResourceType[typeKey]) | |
| 45 .map(type => [type, RegExpFilter.typeMap[resourceTypeMapping.get(type)] || | |
| 46 RegExpFilter.typeMap[type.toUpperCase()] || | |
| 47 RegExpFilter.typeMap.OTHER]) | |
| 48 ); | |
| 49 | |
| 35 function onBeforeRequestAsync(page, url, type, docDomain, | 50 function onBeforeRequestAsync(page, url, type, docDomain, |
| 36 thirdParty, sitekey, | 51 thirdParty, sitekey, |
| 37 specificOnly, filter) | 52 specificOnly, filter) |
| 38 { | 53 { |
| 39 if (filter) | 54 if (filter) |
| 40 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); | 55 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); |
| 41 | 56 |
| 42 if (devtools) | 57 if (devtools) |
| 43 { | 58 { |
| 59 let mappedType = resourceTypeMapping.get(type) || type.toUpperCase(); | |
| 60 if (!RegExpFilter.typeMap[mappedType]) | |
| 61 mappedType = "OTHER"; | |
| 62 | |
| 44 devtools.logRequest( | 63 devtools.logRequest( |
| 45 page, url, type, docDomain, | 64 page, url, |
| 65 mappedType, docDomain, | |
| 46 thirdParty, sitekey, | 66 thirdParty, sitekey, |
| 47 specificOnly, filter | 67 specificOnly, filter |
| 48 ); | 68 ); |
| 49 } | 69 } |
| 50 } | 70 } |
| 51 | 71 |
| 52 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => | 72 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => |
| 53 { | 73 { |
| 54 if (checkWhitelisted(page, frame)) | 74 if (checkWhitelisted(page, frame)) |
| 55 return true; | 75 return true; |
| 56 | 76 |
| 57 let urlString = stringifyURL(url); | 77 let urlString = stringifyURL(url); |
| 58 let docDomain = extractHostFromFrame(frame); | 78 let docDomain = extractHostFromFrame(frame); |
| 59 let thirdParty = isThirdParty(url, docDomain); | 79 let thirdParty = isThirdParty(url, docDomain); |
| 60 let sitekey = getKey(page, frame); | 80 let sitekey = getKey(page, frame); |
| 61 | 81 |
| 62 let specificOnly = !!checkWhitelisted( | 82 let specificOnly = !!checkWhitelisted( |
| 63 page, frame, RegExpFilter.typeMap.GENERICBLOCK | 83 page, frame, RegExpFilter.typeMap.GENERICBLOCK |
| 64 ); | 84 ); |
| 65 | 85 |
| 66 let filter = defaultMatcher.matchesAny( | 86 let filter = defaultMatcher.matchesAny( |
| 67 urlString, RegExpFilter.typeMap[type], | 87 urlString, typeMasks.get(type), |
| 68 docDomain, thirdParty, sitekey, specificOnly | 88 docDomain, thirdParty, sitekey, specificOnly |
| 69 ); | 89 ); |
| 70 | 90 |
| 71 setTimeout(onBeforeRequestAsync, 0, page, urlString, | 91 setTimeout(onBeforeRequestAsync, 0, page, urlString, |
| 72 type, docDomain, | 92 type, docDomain, |
| 73 thirdParty, sitekey, | 93 thirdParty, sitekey, |
| 74 specificOnly, filter); | 94 specificOnly, filter); |
| 75 | 95 |
| 76 return !(filter instanceof BlockingFilter); | 96 return !(filter instanceof BlockingFilter); |
| 77 }); | 97 }); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 if (msg.requestType in chrome.webRequest.ResourceType) | 182 if (msg.requestType in chrome.webRequest.ResourceType) |
| 163 return false; | 183 return false; |
| 164 | 184 |
| 165 return ext.webRequest.onBeforeRequest._dispatch( | 185 return ext.webRequest.onBeforeRequest._dispatch( |
| 166 new URL(msg.url), | 186 new URL(msg.url), |
| 167 msg.requestType, | 187 msg.requestType, |
| 168 sender.page, | 188 sender.page, |
| 169 sender.frame | 189 sender.frame |
| 170 ).includes(false); | 190 ).includes(false); |
| 171 }); | 191 }); |
| OLD | NEW |