OLD | NEW |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 (function(global) | 18 (function(global) |
19 { | 19 { |
20 if (!global.ext) | 20 if (!global.ext) |
21 global.ext = require("ext_background"); | 21 global.ext = require("ext_background"); |
22 | 22 |
| 23 var port = require("messaging").port; |
23 var Prefs = require("prefs").Prefs; | 24 var Prefs = require("prefs").Prefs; |
24 var Utils = require("utils").Utils; | 25 var Utils = require("utils").Utils; |
25 var FilterStorage = require("filterStorage").FilterStorage; | 26 var FilterStorage = require("filterStorage").FilterStorage; |
26 var FilterNotifier = require("filterNotifier").FilterNotifier; | 27 var FilterNotifier = require("filterNotifier").FilterNotifier; |
27 var defaultMatcher = require("matcher").defaultMatcher; | 28 var defaultMatcher = require("matcher").defaultMatcher; |
28 var ElemHideEmulation = require("elemHideEmulation").ElemHideEmulation; | 29 var ElemHideEmulation = require("elemHideEmulation").ElemHideEmulation; |
29 var NotificationStorage = require("notification").Notification; | 30 var NotificationStorage = require("notification").Notification; |
30 | 31 |
31 var filterClasses = require("filterClasses"); | 32 var filterClasses = require("filterClasses"); |
32 var Filter = filterClasses.Filter; | 33 var Filter = filterClasses.Filter; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 { | 151 { |
151 var listenerFilters = changeListeners.get(page); | 152 var listenerFilters = changeListeners.get(page); |
152 if (!listenerFilters) | 153 if (!listenerFilters) |
153 { | 154 { |
154 listenerFilters = Object.create(null); | 155 listenerFilters = Object.create(null); |
155 changeListeners.set(page, listenerFilters); | 156 changeListeners.set(page, listenerFilters); |
156 } | 157 } |
157 return listenerFilters; | 158 return listenerFilters; |
158 } | 159 } |
159 | 160 |
160 global.ext.onMessage.addListener(function(message, sender, callback) | 161 port.on("app.get", (message, sender) => |
161 { | 162 { |
162 switch (message.type) | 163 if (message.what == "issues") |
163 { | 164 { |
164 case "app.get": | 165 var subscriptionInit = tryRequire("subscriptionInit"); |
165 if (message.what == "issues") | 166 return { |
| 167 filterlistsReinitialized: subscriptionInit ? subscriptionInit.reinitiali
zed : false, |
| 168 legacySafariVersion: (info.platform == "safari" && ( |
| 169 Services.vc.compare(info.platformVersion, "6.0") < 0 || // beforel
oad breaks websites in Safari 5 |
| 170 Services.vc.compare(info.platformVersion, "6.1") == 0 || // extensi
ons are broken in 6.1 and 7.0 |
| 171 Services.vc.compare(info.platformVersion, "7.0") == 0)) |
| 172 }; |
| 173 } |
| 174 |
| 175 if (message.what == "doclink") |
| 176 return Utils.getDocLink(message.link); |
| 177 |
| 178 if (message.what == "localeInfo") |
| 179 { |
| 180 var bidiDir; |
| 181 if ("chromeRegistry" in Utils) |
| 182 bidiDir = Utils.chromeRegistry.isLocaleRTL("adblockplus") ? "rtl" : "ltr
"; |
| 183 else |
| 184 bidiDir = ext.i18n.getMessage("@@bidi_dir"); |
| 185 |
| 186 return {locale: Utils.appLocale, bidiDir: bidiDir}; |
| 187 } |
| 188 |
| 189 if (message.what == "features") |
| 190 { |
| 191 return { |
| 192 devToolsPanel: info.platform == "chromium", |
| 193 safariContentBlocker: "safari" in global |
| 194 && "extension" in global.safari |
| 195 && "setContentBlocker" in global.safari.extension |
| 196 }; |
| 197 } |
| 198 |
| 199 return info[message.what]; |
| 200 }); |
| 201 |
| 202 port.on("app.listen", (message, sender) => |
| 203 { |
| 204 getListenerFilters(sender.page).app = message.filter; |
| 205 }); |
| 206 |
| 207 port.on("app.open", (message, sender) => |
| 208 { |
| 209 if (message.what == "options") |
| 210 ext.showOptions(); |
| 211 }); |
| 212 |
| 213 port.on("filters.add", (message, sender) => |
| 214 { |
| 215 var result = require("filterValidation").parseFilter(message.text); |
| 216 var errors = []; |
| 217 if (result.error) |
| 218 errors.push(result.error.toString()); |
| 219 else if (result.filter) |
| 220 FilterStorage.addFilter(result.filter); |
| 221 |
| 222 return errors; |
| 223 }); |
| 224 |
| 225 port.on("filters.blocked", (message, sender) => |
| 226 { |
| 227 var filter = defaultMatcher.matchesAny(message.url, |
| 228 RegExpFilter.typeMap[message.requestType], message.docDomain, |
| 229 message.thirdParty); |
| 230 |
| 231 return filter instanceof BlockingFilter; |
| 232 }); |
| 233 |
| 234 port.on("filters.get", (message, sender) => |
| 235 { |
| 236 if (message.what == "elemhideemulation") |
| 237 { |
| 238 var filters = []; |
| 239 var checkWhitelisted = require("whitelisting").checkWhitelisted; |
| 240 |
| 241 if (Prefs.enabled && !checkWhitelisted(sender.page, sender.frame, |
| 242 RegExpFilter.typeMap.DOCUMENT | |
| 243 RegExpFilter.typeMap.ELEMHIDE)) |
| 244 { |
| 245 var hostname = sender.frame.url.hostname; |
| 246 filters = ElemHideEmulation.getRulesForDomain(hostname); |
| 247 filters = filters.map(function(filter) |
166 { | 248 { |
167 var subscriptionInit = tryRequire("subscriptionInit"); | 249 return { |
168 callback({ | 250 selector: filter.selector, |
169 filterlistsReinitialized: subscriptionInit ? subscriptionInit.reinit
ialized : false, | 251 text: filter.text |
170 legacySafariVersion: (info.platform == "safari" && ( | 252 }; |
171 Services.vc.compare(info.platformVersion, "6.0") < 0 || // bef
oreload breaks websites in Safari 5 | 253 }); |
172 Services.vc.compare(info.platformVersion, "6.1") == 0 || // ext
ensions are broken in 6.1 and 7.0 | 254 } |
173 Services.vc.compare(info.platformVersion, "7.0") == 0)) | 255 return filters; |
174 }); | 256 } |
175 } | 257 |
176 else if (message.what == "doclink") | 258 var subscription = Subscription.fromURL(message.subscriptionUrl); |
177 callback(Utils.getDocLink(message.link)); | 259 if (!subscription) |
178 else if (message.what == "localeInfo") | 260 return []; |
| 261 |
| 262 return subscription.filters.map(convertFilter); |
| 263 }); |
| 264 |
| 265 port.on("filters.importRaw", (message, sender) => |
| 266 { |
| 267 var result = require("filterValidation").parseFilters(message.text); |
| 268 var errors = []; |
| 269 for (var i = 0; i < result.errors.length; i++) |
| 270 { |
| 271 var error = result.errors[i]; |
| 272 if (error.type != "unexpected-filter-list-header") |
| 273 errors.push(error.toString()); |
| 274 } |
| 275 |
| 276 if (errors.length > 0) |
| 277 return errors; |
| 278 |
| 279 var seenFilter = Object.create(null); |
| 280 for (var i = 0; i < result.filters.length; i++) |
| 281 { |
| 282 var filter = result.filters[i]; |
| 283 FilterStorage.addFilter(filter); |
| 284 seenFilter[filter.text] = null; |
| 285 } |
| 286 |
| 287 if (!message.removeExisting) |
| 288 return errors; |
| 289 |
| 290 for (var i = 0; i < FilterStorage.subscriptions.length; i++) |
| 291 { |
| 292 var subscription = FilterStorage.subscriptions[i]; |
| 293 if (!(subscription instanceof SpecialSubscription)) |
| 294 continue; |
| 295 |
| 296 for (var j = subscription.filters.length - 1; j >= 0; j--) |
| 297 { |
| 298 var filter = subscription.filters[j]; |
| 299 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) |
| 300 continue; |
| 301 |
| 302 if (!(filter.text in seenFilter)) |
| 303 FilterStorage.removeFilter(filter); |
| 304 } |
| 305 } |
| 306 |
| 307 return errors; |
| 308 }); |
| 309 |
| 310 port.on("filters.listen", (message, sender) => |
| 311 { |
| 312 getListenerFilters(sender.page).filter = message.filter; |
| 313 addFilterListeners("filter", message.filter); |
| 314 }); |
| 315 |
| 316 port.on("filters.remove", (message, sender) => |
| 317 { |
| 318 var filter = Filter.fromText(message.text); |
| 319 var subscription = null; |
| 320 if (message.subscriptionUrl) |
| 321 subscription = Subscription.fromURL(message.subscriptionUrl); |
| 322 |
| 323 if (!subscription) |
| 324 FilterStorage.removeFilter(filter); |
| 325 else |
| 326 FilterStorage.removeFilter(filter, subscription, message.index); |
| 327 }); |
| 328 |
| 329 port.on("prefs.get", (message, sender) => |
| 330 { |
| 331 return Prefs[message.key]; |
| 332 }); |
| 333 |
| 334 port.on("prefs.listen", (message, sender) => |
| 335 { |
| 336 getListenerFilters(sender.page).pref = message.filter; |
| 337 message.filter.forEach(function(preference) |
| 338 { |
| 339 if (!(preference in listenedPreferences)) |
| 340 { |
| 341 listenedPreferences[preference] = null; |
| 342 Prefs.on(preference, function() |
179 { | 343 { |
180 var bidiDir; | 344 sendMessage("pref", preference, Prefs[preference]); |
181 if ("chromeRegistry" in Utils) | |
182 bidiDir = Utils.chromeRegistry.isLocaleRTL("adblockplus") ? "rtl" :
"ltr"; | |
183 else | |
184 bidiDir = ext.i18n.getMessage("@@bidi_dir"); | |
185 | |
186 callback({locale: Utils.appLocale, bidiDir: bidiDir}); | |
187 } | |
188 else if (message.what == "features") | |
189 { | |
190 callback({ | |
191 devToolsPanel: info.platform == "chromium", | |
192 safariContentBlocker: "safari" in global | |
193 && "extension" in global.safari | |
194 && "setContentBlocker" in global.safari.extension | |
195 }); | |
196 } | |
197 else | |
198 callback(info[message.what]); | |
199 break; | |
200 case "app.listen": | |
201 getListenerFilters(sender.page).app = message.filter; | |
202 break; | |
203 case "app.open": | |
204 if (message.what == "options") | |
205 ext.showOptions(); | |
206 break; | |
207 case "filters.add": | |
208 var result = require("filterValidation").parseFilter(message.text); | |
209 var errors = []; | |
210 if (result.error) | |
211 errors.push(result.error.toString()); | |
212 else if (result.filter) | |
213 FilterStorage.addFilter(result.filter); | |
214 callback(errors); | |
215 break; | |
216 case "filters.blocked": | |
217 var filter = defaultMatcher.matchesAny(message.url, | |
218 RegExpFilter.typeMap[message.requestType], message.docDomain, | |
219 message.thirdParty); | |
220 callback(filter instanceof BlockingFilter); | |
221 break; | |
222 case "filters.get": | |
223 if (message.what == "elemhideemulation") | |
224 { | |
225 var filters = []; | |
226 var checkWhitelisted = require("whitelisting").checkWhitelisted; | |
227 | |
228 if (Prefs.enabled && !checkWhitelisted(sender.page, sender.frame, | |
229 RegExpFilter.typeMap.DOCUMENT | | |
230 RegExpFilter.typeMap.ELEMHIDE)) | |
231 { | |
232 var hostname = sender.frame.url.hostname; | |
233 filters = ElemHideEmulation.getRulesForDomain(hostname); | |
234 filters = filters.map(function(filter) | |
235 { | |
236 return { | |
237 selector: filter.selector, | |
238 text: filter.text | |
239 }; | |
240 }); | |
241 } | |
242 callback(filters); | |
243 break; | |
244 } | |
245 | |
246 var subscription = Subscription.fromURL(message.subscriptionUrl); | |
247 if (!subscription) | |
248 { | |
249 callback([]); | |
250 break; | |
251 } | |
252 | |
253 callback(subscription.filters.map(convertFilter)); | |
254 break; | |
255 case "filters.importRaw": | |
256 var result = require("filterValidation").parseFilters(message.text); | |
257 var errors = []; | |
258 for (var i = 0; i < result.errors.length; i++) | |
259 { | |
260 var error = result.errors[i]; | |
261 if (error.type != "unexpected-filter-list-header") | |
262 errors.push(error.toString()); | |
263 } | |
264 | |
265 callback(errors); | |
266 if (errors.length > 0) | |
267 return; | |
268 | |
269 var seenFilter = Object.create(null); | |
270 for (var i = 0; i < result.filters.length; i++) | |
271 { | |
272 var filter = result.filters[i]; | |
273 FilterStorage.addFilter(filter); | |
274 seenFilter[filter.text] = null; | |
275 } | |
276 | |
277 if (!message.removeExisting) | |
278 return; | |
279 | |
280 for (var i = 0; i < FilterStorage.subscriptions.length; i++) | |
281 { | |
282 var subscription = FilterStorage.subscriptions[i]; | |
283 if (!(subscription instanceof SpecialSubscription)) | |
284 continue; | |
285 | |
286 for (var j = subscription.filters.length - 1; j >= 0; j--) | |
287 { | |
288 var filter = subscription.filters[j]; | |
289 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) | |
290 continue; | |
291 | |
292 if (!(filter.text in seenFilter)) | |
293 FilterStorage.removeFilter(filter); | |
294 } | |
295 } | |
296 break; | |
297 case "filters.listen": | |
298 getListenerFilters(sender.page).filter = message.filter; | |
299 addFilterListeners("filter", message.filter); | |
300 break; | |
301 case "filters.remove": | |
302 var filter = Filter.fromText(message.text); | |
303 var subscription = null; | |
304 if (message.subscriptionUrl) | |
305 subscription = Subscription.fromURL(message.subscriptionUrl); | |
306 | |
307 if (!subscription) | |
308 FilterStorage.removeFilter(filter); | |
309 else | |
310 FilterStorage.removeFilter(filter, subscription, message.index); | |
311 break; | |
312 case "prefs.get": | |
313 callback(Prefs[message.key]); | |
314 break; | |
315 case "prefs.listen": | |
316 getListenerFilters(sender.page).pref = message.filter; | |
317 message.filter.forEach(function(preference) | |
318 { | |
319 if (!(preference in listenedPreferences)) | |
320 { | |
321 listenedPreferences[preference] = null; | |
322 Prefs.on(preference, function() | |
323 { | |
324 sendMessage("pref", preference, Prefs[preference]); | |
325 }); | |
326 } | |
327 }); | 345 }); |
328 break; | 346 } |
329 case "prefs.toggle": | 347 }); |
330 if (message.key == "notifications_ignoredcategories") | 348 }); |
331 NotificationStorage.toggleIgnoreCategory("*"); | 349 |
332 else | 350 port.on("prefs.toggle", (message, sender) => |
333 Prefs[message.key] = !Prefs[message.key]; | 351 { |
334 break; | 352 if (message.key == "notifications_ignoredcategories") |
335 case "subscriptions.add": | 353 NotificationStorage.toggleIgnoreCategory("*"); |
336 var subscription = Subscription.fromURL(message.url); | 354 else |
337 if ("title" in message) | 355 Prefs[message.key] = !Prefs[message.key]; |
338 subscription.title = message.title; | 356 }); |
339 if ("homepage" in message) | 357 |
340 subscription.homepage = message.homepage; | 358 port.on("subscriptions.add", (message, sender) => |
341 | 359 { |
342 if (message.confirm) | 360 var subscription = Subscription.fromURL(message.url); |
343 { | 361 if ("title" in message) |
344 ext.showOptions(function() | 362 subscription.title = message.title; |
345 { | 363 if ("homepage" in message) |
346 sendMessage("app", "addSubscription", subscription); | 364 subscription.homepage = message.homepage; |
347 }); | 365 |
348 } | 366 if (message.confirm) |
349 else | 367 { |
350 { | 368 ext.showOptions(function() |
351 subscription.disabled = false; | 369 { |
352 FilterStorage.addSubscription(subscription); | 370 sendMessage("app", "addSubscription", subscription); |
353 | 371 }); |
354 if (subscription instanceof DownloadableSubscription && !subscription.
lastDownload) | 372 } |
355 Synchronizer.execute(subscription); | 373 else |
356 } | 374 { |
357 break; | 375 subscription.disabled = false; |
358 case "subscriptions.get": | 376 FilterStorage.addSubscription(subscription); |
359 var subscriptions = FilterStorage.subscriptions.filter(function(s) | 377 |
360 { | 378 if (subscription instanceof DownloadableSubscription && !subscription.last
Download) |
361 if (message.ignoreDisabled && s.disabled) | 379 Synchronizer.execute(subscription); |
362 return false; | 380 } |
363 if (s instanceof DownloadableSubscription && message.downloadable) | 381 }); |
364 return true; | 382 |
365 if (s instanceof SpecialSubscription && message.special) | 383 port.on("subscriptions.get", (message, sender) => |
366 return true; | 384 { |
367 return false; | 385 var subscriptions = FilterStorage.subscriptions.filter(function(s) |
368 }); | 386 { |
369 callback(subscriptions.map(convertSubscription)); | 387 if (message.ignoreDisabled && s.disabled) |
370 break; | 388 return false; |
371 case "subscriptions.listen": | 389 if (s instanceof DownloadableSubscription && message.downloadable) |
372 getListenerFilters(sender.page).subscription = message.filter; | 390 return true; |
373 addFilterListeners("subscription", message.filter); | 391 if (s instanceof SpecialSubscription && message.special) |
374 break; | 392 return true; |
375 case "subscriptions.remove": | 393 return false; |
376 var subscription = Subscription.fromURL(message.url); | 394 }); |
377 if (subscription.url in FilterStorage.knownSubscriptions) | 395 |
378 FilterStorage.removeSubscription(subscription); | 396 return subscriptions.map(convertSubscription); |
379 break; | 397 }); |
380 case "subscriptions.toggle": | 398 |
381 var subscription = Subscription.fromURL(message.url); | 399 port.on("subscriptions.listen", (message, sender) => |
382 if (subscription.url in FilterStorage.knownSubscriptions) | 400 { |
383 { | 401 getListenerFilters(sender.page).subscription = message.filter; |
384 if (subscription.disabled || message.keepInstalled) | 402 addFilterListeners("subscription", message.filter); |
385 subscription.disabled = !subscription.disabled; | 403 }); |
386 else | 404 |
387 FilterStorage.removeSubscription(subscription); | 405 port.on("subscriptions.remove", (message, sender) => |
388 } | 406 { |
389 else | 407 var subscription = Subscription.fromURL(message.url); |
390 { | 408 if (subscription.url in FilterStorage.knownSubscriptions) |
391 subscription.disabled = false; | 409 FilterStorage.removeSubscription(subscription); |
392 subscription.title = message.title; | 410 }); |
393 subscription.homepage = message.homepage; | 411 |
394 FilterStorage.addSubscription(subscription); | 412 port.on("subscriptions.toggle", (message, sender) => |
395 if (!subscription.lastDownload) | 413 { |
396 Synchronizer.execute(subscription); | 414 var subscription = Subscription.fromURL(message.url); |
397 } | 415 if (subscription.url in FilterStorage.knownSubscriptions) |
398 break; | 416 { |
399 case "subscriptions.update": | 417 if (subscription.disabled || message.keepInstalled) |
400 var subscriptions = message.url ? [Subscription.fromURL(message.url)] : | 418 subscription.disabled = !subscription.disabled; |
401 FilterStorage.subscriptions; | 419 else |
402 for (var i = 0; i < subscriptions.length; i++) | 420 FilterStorage.removeSubscription(subscription); |
403 { | 421 } |
404 var subscription = subscriptions[i]; | 422 else |
405 if (subscription instanceof DownloadableSubscription) | 423 { |
406 Synchronizer.execute(subscription, true); | 424 subscription.disabled = false; |
407 } | 425 subscription.title = message.title; |
408 break; | 426 subscription.homepage = message.homepage; |
| 427 FilterStorage.addSubscription(subscription); |
| 428 if (!subscription.lastDownload) |
| 429 Synchronizer.execute(subscription); |
| 430 } |
| 431 }); |
| 432 |
| 433 port.on("subscriptions.update", (message, sender) => |
| 434 { |
| 435 var subscriptions = message.url ? [Subscription.fromURL(message.url)] : |
| 436 FilterStorage.subscriptions; |
| 437 for (var i = 0; i < subscriptions.length; i++) |
| 438 { |
| 439 var subscription = subscriptions[i]; |
| 440 if (subscription instanceof DownloadableSubscription) |
| 441 Synchronizer.execute(subscription, true); |
409 } | 442 } |
410 }); | 443 }); |
411 })(this); | 444 })(this); |
OLD | NEW |