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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /** @module requestBlocker */ | 18 /** @module requestBlocker */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
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, getDecodedHostname, | 28 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("url"); |
29 extractHostFromFrame, isThirdParty} = require("url"); | |
30 const {port} = require("messaging"); | 29 const {port} = require("messaging"); |
31 const devtools = require("devtools"); | 30 const devtools = require("devtools"); |
32 | 31 |
33 // 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. | |
34 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 }()); | |
35 | 52 |
36 function onBeforeRequestAsync(page, url, type, docDomain, | 53 function onBeforeRequestAsync(page, url, type, docDomain, |
37 thirdParty, sitekey, | 54 thirdParty, sitekey, |
38 specificOnly, filter) | 55 specificOnly, filter) |
39 { | 56 { |
40 if (filter) | 57 if (filter) |
41 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); | 58 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); |
42 | 59 |
43 if (devtools) | 60 if (devtools) |
44 { | 61 { |
45 devtools.logRequest( | 62 devtools.logRequest( |
46 page, url, type, docDomain, | 63 page, url, type, docDomain, |
47 thirdParty, sitekey, | 64 thirdParty, sitekey, |
48 specificOnly, filter | 65 specificOnly, filter |
49 ); | 66 ); |
50 } | 67 } |
51 } | 68 } |
52 | 69 |
53 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => | 70 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => |
54 { | 71 { |
55 let docDomain = getDecodedHostname(url); | 72 let docDomain = null; |
Sebastian Noack
2017/04/21 08:39:53
Is there a reason you derive the parent document's
Jon Sonesen
2017/04/24 08:40:56
I see, I misunderstood what I was doing here. I ju
| |
56 let sitekey = null; | 73 let sitekey = null; |
74 let specificOnly = false; | |
75 let thirdParty = false; | |
76 let urlString = stringifyURL(url); | |
77 | |
78 | |
57 if (frame && page) | 79 if (frame && page) |
58 { | 80 { |
59 if (checkWhitelisted(page, frame)) | 81 if (checkWhitelisted(page, frame)) |
60 return true; | 82 return true; |
83 | |
61 docDomain = extractHostFromFrame(frame); | 84 docDomain = extractHostFromFrame(frame); |
62 sitekey = getKey(page, frame); | 85 sitekey = getKey(page, frame); |
86 thirdParty = isThirdParty(url, docDomain); | |
87 specificOnly = !!checkWhitelisted(page, frame, | |
88 RegExpFilter.typeMap.GENERICBLOCK); | |
63 } | 89 } |
64 | 90 |
65 let urlString = stringifyURL(url); | 91 let mappedType = resourceTypes.get(type) || "OTHER"; |
66 let thirdParty = isThirdParty(url, docDomain); | |
67 | |
68 let specificOnly = !!checkWhitelisted( | |
69 page, frame, RegExpFilter.typeMap.GENERICBLOCK | |
70 ); | |
71 | 92 |
72 let filter = defaultMatcher.matchesAny( | 93 let filter = defaultMatcher.matchesAny( |
73 urlString, RegExpFilter.typeMap[type], | 94 urlString, RegExpFilter.typeMap[mappedType], |
74 docDomain, thirdParty, sitekey, specificOnly | 95 docDomain, thirdParty, sitekey, specificOnly |
75 ); | 96 ); |
76 | 97 |
77 setTimeout(onBeforeRequestAsync, 0, page, urlString, | 98 setTimeout(onBeforeRequestAsync, 0, page, urlString, |
78 type, docDomain, | 99 mappedType, docDomain, |
79 thirdParty, sitekey, | 100 thirdParty, sitekey, |
80 specificOnly, filter); | 101 specificOnly, filter); |
81 | 102 |
82 return !(filter instanceof BlockingFilter); | 103 return !(filter instanceof BlockingFilter); |
83 }); | 104 }); |
84 | 105 |
85 port.on("filters.collapse", (message, sender) => | 106 port.on("filters.collapse", (message, sender) => |
86 { | 107 { |
87 if (checkWhitelisted(sender.page, sender.frame)) | 108 if (checkWhitelisted(sender.page, sender.frame)) |
88 return false; | 109 return false; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 FilterNotifier.on("filter.added", onFilterChange); | 179 FilterNotifier.on("filter.added", onFilterChange); |
159 FilterNotifier.on("filter.removed", onFilterChange); | 180 FilterNotifier.on("filter.removed", onFilterChange); |
160 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); | 181 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); |
161 FilterNotifier.on("load", onFilterChange); | 182 FilterNotifier.on("load", onFilterChange); |
162 | 183 |
163 port.on("request.blockedByWrapper", (msg, sender) => | 184 port.on("request.blockedByWrapper", (msg, sender) => |
164 { | 185 { |
165 // Chrome 58 onwards directly supports WebSocket blocking, so we can ignore | 186 // Chrome 58 onwards directly supports WebSocket blocking, so we can ignore |
166 // messages from the wrapper here (see https://crbug.com/129353). Hopefully | 187 // messages from the wrapper here (see https://crbug.com/129353). Hopefully |
167 // WebRTC will be supported soon too (see https://crbug.com/707683). | 188 // WebRTC will be supported soon too (see https://crbug.com/707683). |
168 if (msg.requestType in chrome.webRequest.ResourceType) | 189 if (msg.requestType.toUpperCase() in chrome.webRequest.ResourceType) |
169 return false; | 190 return false; |
170 | 191 |
171 return ext.webRequest.onBeforeRequest._dispatch( | 192 return ext.webRequest.onBeforeRequest._dispatch( |
172 new URL(msg.url), | 193 new URL(msg.url), |
173 msg.requestType, | 194 msg.requestType, |
174 sender.page, | 195 sender.page, |
175 sender.frame | 196 sender.frame |
176 ).includes(false); | 197 ).includes(false); |
177 }); | 198 }); |
LEFT | RIGHT |