| 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"); |
|
Sebastian Noack
2017/04/29 23:07:59
It seems the newly imported function (i.e. getDeco
Jon Sonesen
2017/05/04 10:26:05
Done.
| |
| 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 = null; | 72 let docDomain = null; |
| 56 let sitekey = null; | 73 let sitekey = null; |
| 57 | |
| 58 let specificOnly = false; | 74 let specificOnly = false; |
| 59 let thirdParty = false; | 75 let thirdParty = false; |
| 60 let urlString = stringifyURL(url); | 76 let urlString = stringifyURL(url); |
| 77 | |
| 61 | 78 |
| 62 if (frame && page) | 79 if (frame && page) |
| 63 { | 80 { |
| 64 if (checkWhitelisted(page, frame)) | 81 if (checkWhitelisted(page, frame)) |
| 65 return true; | 82 return true; |
|
Sebastian Noack
2017/04/29 23:08:00
Nit: I'd always add a blank line when returning be
Jon Sonesen
2017/05/04 10:26:06
Done.
| |
| 83 | |
| 66 docDomain = extractHostFromFrame(frame); | 84 docDomain = extractHostFromFrame(frame); |
| 67 sitekey = getKey(page, frame); | 85 sitekey = getKey(page, frame); |
| 68 isThirdParty(url, docDomain); | 86 thirdParty = isThirdParty(url, docDomain); |
|
Sebastian Noack
2017/04/29 23:07:59
As this code stands, this call is redundant. Did y
Jon Sonesen
2017/05/04 10:26:05
Yeah that's what happened there.
Thanks.
Done.
| |
| 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 |