Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 |
35 let resourceTypeMappings = new Map([ | 36 // Map of content types reported by the browser to the respecitve content types |
Sebastian Noack
2017/05/18 12:44:24
Nit: This is a mapping of resource types, so IMHO
Manish Jethani
2017/05/18 20:52:04
resourceTypes implies all resource types, but I've
| |
36 ["beacon", "PING"], | 37 // used by Adblock Plus. Other content types are simply mapped to OTHER. |
37 ["imageset", "IMAGE"], | 38 let resourceTypes = new Map(function*() |
38 ["sub_frame", "SUBDOCUMENT"], | 39 { |
39 ["web_manifest", "OTHER"], | 40 for (let type in RegExpFilter.typeMap) |
Sebastian Noack
2017/05/18 12:44:24
I think we shouldn't hard-code the types we treat
Manish Jethani
2017/05/18 20:52:04
My reasoning was that we shouldn't block any resou
Sebastian Noack
2017/05/19 11:30:46
Sorry, I forgot to reply to this, since you addres
| |
40 ["xml_dtd", "OTHER"], | 41 yield [type.toLowerCase(), type]; |
41 ["xslt", "OTHER"] | |
42 ]); | |
43 | 42 |
44 let typeMasks = new Map( | 43 yield ["sub_frame", "SUBDOCUMENT"]; |
45 Object.keys(chrome.webRequest.ResourceType) | 44 |
46 .map(typeKey => chrome.webRequest.ResourceType[typeKey]) | 45 // Treat navigator.sendBeacon() the same as <a ping>, it's essentially the |
47 .map(type => [type, RegExpFilter.typeMap[resourceTypeMappings.get(type)] || | 46 // same concept - merely generalized. |
48 RegExpFilter.typeMap[type.toUpperCase()] || 0]) | 47 yield ["beacon", "PING"]; |
Sebastian Noack
2017/05/18 12:44:24
Please use RegExpFilter.typeMap.OTHER, rather than
Manish Jethani
2017/05/18 20:52:04
Done.
| |
49 ); | 48 |
49 // Treat <img srcset> and <picture> the same as other images. | |
50 yield ["imageset", "IMAGE"]; | |
51 }()); | |
50 | 52 |
51 function onBeforeRequestAsync(page, url, type, docDomain, | 53 function onBeforeRequestAsync(page, url, type, docDomain, |
52 thirdParty, sitekey, | 54 thirdParty, sitekey, |
53 specificOnly, filter) | 55 specificOnly, filter) |
54 { | 56 { |
55 if (filter) | 57 if (filter) |
56 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); | 58 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); |
57 | 59 |
58 if (devtools) | 60 if (devtools) |
59 { | 61 { |
60 devtools.logRequest( | 62 devtools.logRequest( |
61 page, url, | 63 page, url, type, docDomain, |
62 resourceTypeMappings.get(type) || type.toUpperCase(), docDomain, | |
63 thirdParty, sitekey, | 64 thirdParty, sitekey, |
64 specificOnly, filter | 65 specificOnly, filter |
65 ); | 66 ); |
66 } | 67 } |
67 } | 68 } |
68 | 69 |
69 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => | 70 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => |
70 { | 71 { |
71 if (checkWhitelisted(page, frame)) | 72 if (checkWhitelisted(page, frame)) |
72 return true; | 73 return true; |
73 | 74 |
74 let urlString = stringifyURL(url); | 75 let urlString = stringifyURL(url); |
75 let docDomain = extractHostFromFrame(frame); | 76 let docDomain = extractHostFromFrame(frame); |
76 let thirdParty = isThirdParty(url, docDomain); | 77 let thirdParty = isThirdParty(url, docDomain); |
77 let sitekey = getKey(page, frame); | 78 let sitekey = getKey(page, frame); |
78 | 79 |
79 let specificOnly = !!checkWhitelisted( | 80 let specificOnly = !!checkWhitelisted( |
80 page, frame, RegExpFilter.typeMap.GENERICBLOCK | 81 page, frame, RegExpFilter.typeMap.GENERICBLOCK |
81 ); | 82 ); |
82 | 83 |
84 let mappedType = resourceTypes.get(type) || "OTHER"; | |
85 | |
83 let filter = defaultMatcher.matchesAny( | 86 let filter = defaultMatcher.matchesAny( |
84 urlString, typeMasks.get(type), | 87 urlString, RegExpFilter.typeMap[mappedType], |
85 docDomain, thirdParty, sitekey, specificOnly | 88 docDomain, thirdParty, sitekey, specificOnly |
86 ); | 89 ); |
87 | 90 |
88 setTimeout(onBeforeRequestAsync, 0, page, urlString, | 91 setTimeout(onBeforeRequestAsync, 0, page, urlString, |
89 type, docDomain, | 92 mappedType, docDomain, |
90 thirdParty, sitekey, | 93 thirdParty, sitekey, |
91 specificOnly, filter); | 94 specificOnly, filter); |
92 | 95 |
93 return !(filter instanceof BlockingFilter); | 96 return !(filter instanceof BlockingFilter); |
94 }); | 97 }); |
95 | 98 |
96 port.on("filters.collapse", (message, sender) => | 99 port.on("filters.collapse", (message, sender) => |
97 { | 100 { |
98 if (checkWhitelisted(sender.page, sender.frame)) | 101 if (checkWhitelisted(sender.page, sender.frame)) |
99 return false; | 102 return false; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 if (msg.requestType in chrome.webRequest.ResourceType) | 182 if (msg.requestType in chrome.webRequest.ResourceType) |
180 return false; | 183 return false; |
181 | 184 |
182 return ext.webRequest.onBeforeRequest._dispatch( | 185 return ext.webRequest.onBeforeRequest._dispatch( |
183 new URL(msg.url), | 186 new URL(msg.url), |
184 msg.requestType, | 187 msg.requestType, |
185 sender.page, | 188 sender.page, |
186 sender.frame | 189 sender.frame |
187 ).includes(false); | 190 ).includes(false); |
188 }); | 191 }); |
LEFT | RIGHT |