| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 20 matching lines...) Expand all Loading... | |
| 31 const TITLE_PROP = "title"; | 31 const TITLE_PROP = "title"; |
| 32 const URL_PROP = "url"; | 32 const URL_PROP = "url"; |
| 33 | 33 |
| 34 function init() | 34 function init() |
| 35 { | 35 { |
| 36 Promise.all([FilterNotifier.once("load"), Prefs.untilLoaded]).then(onLoaded); | 36 Promise.all([FilterNotifier.once("load"), Prefs.untilLoaded]).then(onLoaded); |
| 37 } | 37 } |
| 38 | 38 |
| 39 function onLoaded() | 39 function onLoaded() |
| 40 { | 40 { |
| 41 browser.runtime.abbRegisterRequestListener(handleRequest); | 41 browser.runtime.registerAbbMessageListener(handleMessage); |
| 42 browser.runtime.abbSendRequest("Abb:OnLoaded"); | 42 browser.runtime.sendAbbMessage("OnLoaded"); |
|
Thomas Greiner
2018/08/27 15:24:41
Detail: The "Abb:" prefix seems redundant here sin
diegocarloslima
2018/08/27 21:19:16
Acknowledged.
| |
| 43 FilterNotifier.on("save", onFiltersSaved); | 43 FilterNotifier.on("save", onFiltersSaved); |
| 44 } | 44 } |
| 45 | 45 |
| 46 function onFiltersSaved() | 46 function onFiltersSaved() |
| 47 { | 47 { |
| 48 browser.runtime.abbSendRequest("Abb:OnFiltersSaved"); | 48 browser.runtime.sendAbbMessage("OnFiltersSaved"); |
| 49 } | 49 } |
| 50 | 50 |
| 51 function handleRequest(data) | 51 function handleMessage(data) |
| 52 { | 52 { |
| 53 | |
|
anton
2018/08/27 05:57:10
do we need this empty line?
diegocarloslima
2018/08/27 21:19:16
Acknowledged.
| |
| 54 switch ((data["action"])) | 53 switch ((data["action"])) |
| 55 { | 54 { |
| 56 case "getAdblockPlusEnabled": | 55 case "getAdblockPlusEnabled": |
| 57 return successData(Prefs.enabled); | 56 return successData(Prefs.enabled); |
| 58 | 57 |
| 59 case "setAdblockPlusEnabled": | 58 case "setAdblockPlusEnabled": |
| 60 if (!checkData(data, ENABLE_PROP)) break; | 59 if (!checkData(data, ENABLE_PROP)) break; |
| 61 Prefs.enabled = !!data[ENABLE_PROP]; | 60 Prefs.enabled = !!data[ENABLE_PROP]; |
| 62 return successData(); | 61 return successData(); |
| 63 | 62 |
| 64 case "getAcceptableAdsEnabled": | 63 case "getAcceptableAdsEnabled": |
| 65 return successData(isSubscriptionEnabled( | 64 return successData(isSubscriptionEnabled( |
| 66 Prefs.subscriptions_exceptionsurl)); | 65 Prefs.subscriptions_exceptionsurl)); |
| 67 | 66 |
| 68 case "setAcceptableAdsEnabled": | 67 case "setAcceptableAdsEnabled": |
| 69 if (!checkData(data, ENABLE_PROP)) break; | 68 if (!checkData(data, ENABLE_PROP)) break; |
| 70 const acceptableAdsTitle = "Allow non-intrusive advertising"; | 69 const acceptableAdsTitle = "Allow non-intrusive advertising"; |
| 71 setSubscriptionEnabled(!!data[ENABLE_PROP], | 70 setSubscriptionEnabled(!!data[ENABLE_PROP], |
| 72 Prefs.subscriptions_exceptionsurl, acceptableAdsTitle); | 71 Prefs.subscriptions_exceptionsurl, acceptableAdsTitle); |
| 73 return successData(getAllSubscriptions()); | 72 return successData(); |
|
Thomas Greiner
2018/08/27 15:24:41
The previous version didn't return anything but I
diegocarloslima
2018/08/27 21:19:16
Yeah, this was some test code that I forgot to rem
| |
| 74 | 73 |
| 75 case "getEnabledSubscriptions": | 74 case "getEnabledSubscriptions": |
| 76 return successData(getEnabledSubscriptions()); | 75 return successData(getEnabledSubscriptions()); |
| 77 | 76 |
| 78 case "isSubscriptionEnabled": | 77 case "isSubscriptionEnabled": |
| 79 if (!checkData(data, URL_PROP)) break; | 78 if (!checkData(data, URL_PROP)) break; |
| 80 return successData(isSubscriptionEnabled(data[URL_PROP])); | 79 return successData(isSubscriptionEnabled(data[URL_PROP])); |
| 81 | 80 |
| 82 case "addSubscription": | 81 case "addSubscription": |
| 83 if (!checkData(data, URL_PROP)) break; | 82 if (!checkData(data, URL_PROP)) break; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 96 if (!checkData(data, [URL_PROP, HOST_PROP])) break; | 95 if (!checkData(data, [URL_PROP, HOST_PROP])) break; |
| 97 return successData(isDomainWhitelisted(data[URL_PROP], data[HOST_PROP])); | 96 return successData(isDomainWhitelisted(data[URL_PROP], data[HOST_PROP])); |
| 98 | 97 |
| 99 case "whitelistDomain": | 98 case "whitelistDomain": |
| 100 if (!checkData(data, [ENABLE_PROP, URL_PROP, HOST_PROP])) break; | 99 if (!checkData(data, [ENABLE_PROP, URL_PROP, HOST_PROP])) break; |
| 101 setDomainWhitelisted(!!data[ENABLE_PROP], data[URL_PROP], data[HOST_PROP]) ; | 100 setDomainWhitelisted(!!data[ENABLE_PROP], data[URL_PROP], data[HOST_PROP]) ; |
| 102 return successData(); | 101 return successData(); |
| 103 | 102 |
| 104 } | 103 } |
| 105 return errorData("Malformed request"); | 104 return errorData("Malformed request"); |
| 106 } | |
| 107 | |
| 108 // remove | |
| 109 function getAllSubscriptions() | |
| 110 { | |
| 111 return FilterStorage.subscriptions.map(sub => ({"title": sub.title, "url": sub .url, "downloadable": (sub instanceof DownloadableSubscription), "special": (sub instanceof SpecialSubscription), "disabled": sub.disabled})); | |
| 112 } | 105 } |
| 113 | 106 |
| 114 function getEnabledSubscriptions() | 107 function getEnabledSubscriptions() |
| 115 { | 108 { |
| 116 return FilterStorage.subscriptions.filter(sub => !sub.disabled).map(sub => | 109 return FilterStorage.subscriptions.filter(sub => !sub.disabled).map(sub => |
| 117 ({[TITLE_PROP]: sub.title, [URL_PROP]: sub.url})); | 110 ({[TITLE_PROP]: sub.title, [URL_PROP]: sub.url})); |
| 118 } | 111 } |
| 119 | 112 |
| 120 function isSubscriptionEnabled(url) | 113 function isSubscriptionEnabled(url) |
| 121 { | 114 { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 { | 152 { |
| 160 FilterStorage.removeFilter(filter); | 153 FilterStorage.removeFilter(filter); |
| 161 if (filter.subscriptions.length) filter.disabled = true; | 154 if (filter.subscriptions.length) filter.disabled = true; |
| 162 } | 155 } |
| 163 | 156 |
| 164 function getWhitelistedDomains() | 157 function getWhitelistedDomains() |
| 165 { | 158 { |
| 166 const whitelistRegex = /^@@\|\|([^/:]+)\^\$document$/; | 159 const whitelistRegex = /^@@\|\|([^/:]+)\^\$document$/; |
| 167 const results = []; | 160 const results = []; |
| 168 | 161 |
| 169 FilterStorage.subscriptions.forEach(function(sub) { | 162 for (const sub of FilterStorage.subscriptions) |
| 170 if (!(sub instanceof SpecialSubscription) || sub.disabled) return; | 163 { |
| 171 sub.filters.forEach(function(filter) { | 164 if (!(sub instanceof SpecialSubscription) || sub.disabled) continue; |
|
Thomas Greiner
2018/08/27 15:24:41
Suggestion: If you want you can use a for-of loop
diegocarloslima
2018/08/27 21:19:16
Acknowledged.
| |
| 165 for (const filter of sub.filters) | |
| 166 { | |
| 172 const match = filter.text.match(whitelistRegex); | 167 const match = filter.text.match(whitelistRegex); |
| 173 if (match) results.push({[URL_PROP]: match[1]}); | 168 if (match) results.push({[URL_PROP]: match[1]}); |
| 174 }); | 169 } |
| 175 }); | 170 } |
| 176 | 171 |
| 177 return results; | 172 return results; |
| 178 } | |
| 179 | |
| 180 function flatIt(array) | |
|
Thomas Greiner
2018/08/27 15:24:41
Detail: You can more efficiently implement that us
diegocarloslima
2018/08/27 21:19:16
This was also some test code ^^
| |
| 181 { | |
| 182 let flatArray = []; | |
| 183 array.forEach(function(item) { | |
| 184 flatArray = flatArray.concat(item); | |
| 185 }); | |
| 186 return flatArray; | |
| 187 } | 173 } |
| 188 | 174 |
| 189 function isDomainWhitelisted(url, host) | 175 function isDomainWhitelisted(url, host) |
| 190 { | 176 { |
| 191 return !!getWhitelistingFilter(url, host); | 177 return !!getWhitelistingFilter(url, host); |
| 192 } | 178 } |
| 193 | 179 |
| 194 function setDomainWhitelisted(isWhitelisted, url, host) | 180 function setDomainWhitelisted(isWhitelisted, url, host) |
| 195 { | 181 { |
| 196 if (isDomainWhitelisted(url, host) == isWhitelisted) return; | 182 if (isDomainWhitelisted(url, host) == isWhitelisted) return; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 221 url, RegExpFilter.typeMap.DOCUMENT, host, false, null, false); | 207 url, RegExpFilter.typeMap.DOCUMENT, host, false, null, false); |
| 222 } | 208 } |
| 223 catch (e) {} | 209 catch (e) {} |
| 224 return null; | 210 return null; |
| 225 } | 211 } |
| 226 | 212 |
| 227 function checkData(data, check) | 213 function checkData(data, check) |
| 228 { | 214 { |
| 229 if (!data) return false; | 215 if (!data) return false; |
| 230 const properties = [].concat(check || []); | 216 const properties = [].concat(check || []); |
| 231 return properties.every(function(item) { | 217 return properties.every(function(item) |
| 218 { | |
| 232 return item in data; | 219 return item in data; |
| 233 }); | 220 }); |
| 234 } | 221 } |
| 235 | 222 |
| 236 function successData(value) | 223 function successData(value) |
| 237 { | 224 { |
| 238 const data = {}; | 225 const data = {}; |
| 239 data.success = true; | 226 data.success = true; |
| 240 if (value != null) data.value = value; | 227 if (value != null) data.value = value; |
| 241 return data; | 228 return data; |
| 242 } | 229 } |
| 243 | 230 |
| 244 function errorData(errorMsg) | 231 function errorData(errorMsg) |
| 245 { | 232 { |
| 246 const data = {}; | 233 const data = {}; |
| 247 if (errorMsg) data.errorMsg = errorMsg; | 234 if (errorMsg) data.error = errorMsg; |
|
Thomas Greiner
2018/08/27 15:24:42
The endpoint in ext-c-runtime.js expects this prop
diegocarloslima
2018/08/27 21:19:16
Acknowledged.
| |
| 248 return data; | 235 return data; |
| 249 } | 236 } |
| 250 | 237 |
| 251 init(); | 238 init(); |
| LEFT | RIGHT |