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; |
| 35 |
| 36 // Map of content types reported by the browser to the respecitve content types |
| 37 // used by Adblock Plus. Other content types are simply mapped to OTHER. |
| 38 let resourceTypes = new Map(function*() |
| 39 { |
| 40 for (let type in RegExpFilter.typeMap) |
| 41 yield [type.toLowerCase(), type]; |
| 42 |
| 43 yield ["sub_frame", "SUBDOCUMENT"]; |
| 44 |
| 45 // Treat navigator.sendBeacon() the same as <a ping>, it's essentially the |
| 46 // same concept - merely generalized. |
| 47 yield ["beacon", "PING"]; |
| 48 |
| 49 // Treat <img srcset> and <picture> the same as other images. |
| 50 yield ["imageset", "IMAGE"]; |
| 51 }()); |
34 | 52 |
35 function onBeforeRequestAsync(page, url, type, docDomain, | 53 function onBeforeRequestAsync(page, url, type, docDomain, |
36 thirdParty, sitekey, | 54 thirdParty, sitekey, |
37 specificOnly, filter) | 55 specificOnly, filter) |
38 { | 56 { |
39 if (filter) | 57 if (filter) |
40 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); | 58 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); |
41 | 59 |
42 if (devtools) | 60 if (devtools) |
43 { | 61 { |
44 devtools.logRequest( | 62 devtools.logRequest( |
45 page, url, type, docDomain, | 63 page, url, type, docDomain, |
46 thirdParty, sitekey, | 64 thirdParty, sitekey, |
47 specificOnly, filter | 65 specificOnly, filter |
48 ); | 66 ); |
49 } | 67 } |
50 } | 68 } |
51 | 69 |
52 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => | 70 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => |
53 { | 71 { |
54 let docDomain = null; | 72 let docDomain = null; |
55 let sitekey = null; | 73 let sitekey = null; |
56 | |
57 let specificOnly = false; | 74 let specificOnly = false; |
58 let thirdParty = false; | 75 let thirdParty = false; |
59 let urlString = stringifyURL(url); | 76 let urlString = stringifyURL(url); |
| 77 |
60 | 78 |
61 if (frame && page) | 79 if (frame && page) |
62 { | 80 { |
63 if (checkWhitelisted(page, frame)) | 81 if (checkWhitelisted(page, frame)) |
64 return true; | 82 return true; |
65 | 83 |
66 docDomain = extractHostFromFrame(frame); | 84 docDomain = extractHostFromFrame(frame); |
67 sitekey = getKey(page, frame); | 85 sitekey = getKey(page, frame); |
68 thirdParty = isThirdParty(url, docDomain); | 86 thirdParty = isThirdParty(url, docDomain); |
69 specificOnly = !!checkWhitelisted( | 87 specificOnly = !!checkWhitelisted(page, frame, |
70 page, frame, RegExpFilter.typeMap.GENERICBLOCK | 88 RegExpFilter.typeMap.GENERICBLOCK); |
71 ); | |
72 } | 89 } |
73 | 90 |
74 | 91 let mappedType = resourceTypes.get(type) || "OTHER"; |
75 | 92 |
76 let filter = defaultMatcher.matchesAny( | 93 let filter = defaultMatcher.matchesAny( |
77 urlString, RegExpFilter.typeMap[type], | 94 urlString, RegExpFilter.typeMap[mappedType], |
78 docDomain, thirdParty, sitekey, specificOnly | 95 docDomain, thirdParty, sitekey, specificOnly |
79 ); | 96 ); |
80 | 97 |
81 setTimeout(onBeforeRequestAsync, 0, page, urlString, | 98 setTimeout(onBeforeRequestAsync, 0, page, urlString, |
82 type, docDomain, | 99 mappedType, docDomain, |
83 thirdParty, sitekey, | 100 thirdParty, sitekey, |
84 specificOnly, filter); | 101 specificOnly, filter); |
85 | 102 |
86 return !(filter instanceof BlockingFilter); | 103 return !(filter instanceof BlockingFilter); |
87 }); | 104 }); |
88 | 105 |
89 port.on("filters.collapse", (message, sender) => | 106 port.on("filters.collapse", (message, sender) => |
90 { | 107 { |
91 if (checkWhitelisted(sender.page, sender.frame)) | 108 if (checkWhitelisted(sender.page, sender.frame)) |
92 return false; | 109 return false; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 FilterNotifier.on("filter.added", onFilterChange); | 179 FilterNotifier.on("filter.added", onFilterChange); |
163 FilterNotifier.on("filter.removed", onFilterChange); | 180 FilterNotifier.on("filter.removed", onFilterChange); |
164 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); | 181 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); |
165 FilterNotifier.on("load", onFilterChange); | 182 FilterNotifier.on("load", onFilterChange); |
166 | 183 |
167 port.on("request.blockedByWrapper", (msg, sender) => | 184 port.on("request.blockedByWrapper", (msg, sender) => |
168 { | 185 { |
169 // Chrome 58 onwards directly supports WebSocket blocking, so we can ignore | 186 // Chrome 58 onwards directly supports WebSocket blocking, so we can ignore |
170 // messages from the wrapper here (see https://crbug.com/129353). Hopefully | 187 // messages from the wrapper here (see https://crbug.com/129353). Hopefully |
171 // WebRTC will be supported soon too (see https://crbug.com/707683). | 188 // WebRTC will be supported soon too (see https://crbug.com/707683). |
172 if (msg.requestType in chrome.webRequest.ResourceType) | 189 if (msg.requestType.toUpperCase() in chrome.webRequest.ResourceType) |
173 return false; | 190 return false; |
174 | 191 |
175 return ext.webRequest.onBeforeRequest._dispatch( | 192 return ext.webRequest.onBeforeRequest._dispatch( |
176 new URL(msg.url), | 193 new URL(msg.url), |
177 msg.requestType, | 194 msg.requestType, |
178 sender.page, | 195 sender.page, |
179 sender.frame | 196 sender.frame |
180 ).includes(false); | 197 ).includes(false); |
181 }); | 198 }); |
LEFT | RIGHT |