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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 10 matching lines...) Expand all Loading... | |
21 global.ext = require("ext_background"); | 21 global.ext = require("ext_background"); |
22 | 22 |
23 var Prefs = require("prefs").Prefs; | 23 var Prefs = require("prefs").Prefs; |
24 var Utils = require("utils").Utils; | 24 var Utils = require("utils").Utils; |
25 var FilterStorage = require("filterStorage").FilterStorage; | 25 var FilterStorage = require("filterStorage").FilterStorage; |
26 var FilterNotifier = require("filterNotifier").FilterNotifier; | 26 var FilterNotifier = require("filterNotifier").FilterNotifier; |
27 var defaultMatcher = require("matcher").defaultMatcher; | 27 var defaultMatcher = require("matcher").defaultMatcher; |
28 | 28 |
29 var filterClasses = require("filterClasses"); | 29 var filterClasses = require("filterClasses"); |
30 var Filter = filterClasses.Filter; | 30 var Filter = filterClasses.Filter; |
31 var WhitelistFilter = filterClasses.WhitelistFilter | |
32 var BlockingFilter = filterClasses.BlockingFilter; | 31 var BlockingFilter = filterClasses.BlockingFilter; |
33 var Synchronizer = require("synchronizer").Synchronizer; | 32 var Synchronizer = require("synchronizer").Synchronizer; |
34 var parseFilters = require("filterValidation").parseFilters; | 33 var filterValidation = require("filterValidation"); |
35 var parseFilter = require("filterValidation").parseFilter; | |
36 | 34 |
37 var subscriptionClasses = require("subscriptionClasses"); | 35 var subscriptionClasses = require("subscriptionClasses"); |
38 var Subscription = subscriptionClasses.Subscription; | 36 var Subscription = subscriptionClasses.Subscription; |
39 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; | 37 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; |
40 var SpecialSubscription = subscriptionClasses.SpecialSubscription; | 38 var SpecialSubscription = subscriptionClasses.SpecialSubscription; |
41 | 39 |
42 function convertObject(keys, obj) | 40 function convertObject(keys, obj) |
43 { | 41 { |
44 var result = {}; | 42 var result = {}; |
45 for (var i = 0; i < keys.length; i++) | 43 for (var i = 0; i < keys.length; i++) |
46 result[keys[i]] = obj[keys[i]]; | 44 result[keys[i]] = obj[keys[i]]; |
47 return result; | 45 return result; |
48 } | 46 } |
49 | 47 |
50 var convertSubscription = convertObject.bind(null, ["disabled", | 48 var convertSubscription = convertObject.bind(null, ["disabled", |
51 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); | 49 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); |
52 var convertFilter = convertObject.bind(null, ["text"]); | 50 var convertFilter = convertObject.bind(null, ["text"]); |
53 | 51 |
54 var changeListeners = null; | 52 var changeListeners = null; |
55 var messageTypes = { | 53 var messageTypes = { |
56 "app": "app.listen", | 54 "app": "app.listen", |
57 "filter": "filters.listen", | 55 "filter": "filters.listen", |
58 "subscription": "subscriptions.listen" | 56 "subscription": "subscriptions.listen" |
59 }; | 57 }; |
60 | 58 |
59 function sendMessage(type, action, args, page) | |
60 { | |
61 var pages = page ? [page] : changeListeners.keys(); | |
62 for (var i = 0; i < pages.length; i++) | |
63 { | |
64 var filters = changeListeners.get(pages[i]); | |
65 if (filters[type] && filters[type].indexOf(action) >= 0) | |
66 { | |
67 pages[i].sendMessage({ | |
68 type: messageTypes[type], | |
69 action: action, | |
70 args: args | |
71 }); | |
72 } | |
73 } | |
74 } | |
75 | |
61 function onFilterChange(action) | 76 function onFilterChange(action) |
62 { | 77 { |
63 if (action == "load") | 78 if (action == "load") |
64 action = "filter.loaded"; | 79 action = "filter.loaded"; |
65 | 80 |
66 var parts = action.split(".", 2); | 81 var parts = action.split(".", 2); |
67 var type; | 82 var type; |
68 if (parts.length == 1) | 83 if (parts.length == 1) |
69 { | 84 { |
70 type = "app"; | 85 type = "app"; |
(...skipping 10 matching lines...) Expand all Loading... | |
81 | 96 |
82 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) | 97 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) |
83 { | 98 { |
84 if (arg instanceof Subscription) | 99 if (arg instanceof Subscription) |
85 return convertSubscription(arg); | 100 return convertSubscription(arg); |
86 else if (arg instanceof Filter) | 101 else if (arg instanceof Filter) |
87 return convertFilter(arg); | 102 return convertFilter(arg); |
88 else | 103 else |
89 return arg; | 104 return arg; |
90 }); | 105 }); |
91 | 106 sendMessage(type, action, args); |
92 var pages = changeListeners.keys(); | 107 } |
93 for (var i = 0; i < pages.length; i++) | |
94 { | |
95 var filters = changeListeners.get(pages[i]); | |
96 if (filters[type] && filters[type].indexOf(action) >= 0) | |
97 { | |
98 pages[i].sendMessage({ | |
99 type: messageTypes[type], | |
100 action: action, | |
101 args: args | |
102 }); | |
103 } | |
104 } | |
105 }; | |
106 | 108 |
107 global.ext.onMessage.addListener(function(message, sender, callback) | 109 global.ext.onMessage.addListener(function(message, sender, callback) |
108 { | 110 { |
109 var listenerFilters = null; | 111 var listenerFilters = null; |
110 switch (message.type) | 112 switch (message.type) |
111 { | 113 { |
112 case "app.listen": | 114 case "app.listen": |
113 case "filters.listen": | 115 case "filters.listen": |
114 case "subscriptions.listen": | 116 case "subscriptions.listen": |
115 if (!changeListeners) | 117 if (!changeListeners) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 listenerFilters.app = message.filter; | 176 listenerFilters.app = message.filter; |
175 else | 177 else |
176 delete listenerFilters.app; | 178 delete listenerFilters.app; |
177 break; | 179 break; |
178 case "app.open": | 180 case "app.open": |
179 if (message.what == "options") | 181 if (message.what == "options") |
180 ext.showOptions(); | 182 ext.showOptions(); |
181 break; | 183 break; |
182 case "filters.add": | 184 case "filters.add": |
183 var filter = Filter.fromText(message.text); | 185 var filter = Filter.fromText(message.text); |
184 FilterStorage.addFilter(filter); | 186 var result = filterValidation.parseFilter(message.text); |
187 if (result.error) | |
188 sendMessage("app", "error", [result.error.toString()], sender.page); | |
189 else if (result.filter) | |
190 FilterStorage.addFilter(result.filter); | |
185 break; | 191 break; |
186 case "filters.blocked": | 192 case "filters.blocked": |
187 var filter = defaultMatcher.matchesAny(message.url, message.requestType, | 193 var filter = defaultMatcher.matchesAny(message.url, message.requestType, |
188 message.docDomain, message.thirdParty); | 194 message.docDomain, message.thirdParty); |
189 callback(filter instanceof BlockingFilter); | 195 callback(filter instanceof BlockingFilter); |
190 break; | 196 break; |
191 case "filters.get": | 197 case "filters.get": |
192 var subscription = Subscription.fromURL(message.subscriptionUrl); | 198 var subscription = Subscription.fromURL(message.subscriptionUrl); |
193 if (!subscription) | 199 if (!subscription) |
194 { | 200 { |
195 callback([]); | 201 callback([]); |
196 break; | 202 break; |
197 } | 203 } |
198 | 204 |
199 callback(subscription.filters.map(convertFilter)); | 205 callback(subscription.filters.map(convertFilter)); |
200 break; | 206 break; |
201 case "filters.importRaw": | 207 case "filters.importRaw": |
202 var result = parseFilters(message.text); | 208 var result = filterValidation.parseFilters(message.text); |
203 var errors = result.errors.filter(function(error) | 209 var errors = []; |
204 { | 210 for (var i = 0; i < result.errors.length; i++) |
205 return error.type != "unexpected-filter-list-header"; | 211 { |
206 }); | 212 var error = result.errors[i]; |
213 if (error.type != "unexpected-filter-list-header") | |
214 errors.push(error.toString()); | |
215 } | |
207 | 216 |
208 if (errors.length > 0) | 217 if (errors.length > 0) |
209 { | 218 { |
210 alert(errors.join("\n")); | 219 sendMessage("app", "error", errors, sender.page); |
211 return; | 220 return; |
212 } | 221 } |
213 | 222 |
214 var seenFilter = Object.create(null); | 223 var seenFilter = Object.create(null); |
215 for (var i = 0; i < result.filters.length; i++) | 224 for (var i = 0; i < result.filters.length; i++) |
216 { | 225 { |
217 var filter = result.filters[i]; | 226 var filter = result.filters[i]; |
218 FilterStorage.addFilter(filter); | 227 FilterStorage.addFilter(filter); |
219 seenFilter[filter.text] = null; | 228 seenFilter[filter.text] = null; |
220 } | 229 } |
221 | 230 |
222 for (var i = 0; i < FilterStorage.subscriptions.length; i++) | 231 for (var i = 0; i < FilterStorage.subscriptions.length; i++) |
223 { | 232 { |
224 var subscription = FilterStorage.subscriptions[i]; | 233 var subscription = FilterStorage.subscriptions[i]; |
225 if (!(subscription instanceof SpecialSubscription)) | 234 if (!(subscription instanceof SpecialSubscription)) |
226 continue; | 235 continue; |
227 | 236 |
228 var filters = subscription.filters.slice(); | 237 for (var j = subscription.filters.length - 1; j >= 0; j--) |
229 for (var j = 0; j < filters.length; j++) | |
230 { | 238 { |
231 var filter = filters[j]; | 239 var filter = subscription.filters[j]; |
232 if (filter instanceof WhitelistFilter && | 240 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) |
233 /^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) | |
234 continue; | 241 continue; |
235 | 242 |
236 if (!(filter.text in seenFilter)) | 243 if (!(filter.text in seenFilter)) |
237 FilterStorage.removeFilter(filter); | 244 FilterStorage.removeFilter(filter); |
238 } | 245 } |
239 } | 246 } |
240 break; | 247 break; |
241 case "filters.listen": | 248 case "filters.listen": |
242 if (message.filter) | 249 if (message.filter) |
243 listenerFilters.filter = message.filter; | 250 listenerFilters.filter = message.filter; |
244 else | 251 else |
245 delete listenerFilters.filter; | 252 delete listenerFilters.filter; |
246 break; | |
247 case "filters.parse": | |
248 var result = parseFilter(message.text); | |
249 if (result.error) | |
250 alert(result.error); | |
Sebastian Noack
2015/07/10 07:31:01
Please no alert() or similar calls in the backgrou
Thomas Greiner
2015/07/10 12:38:53
Unfortunately, this didn't make it into the initia
| |
251 else | |
252 FilterStorage.addFilter(result.filter); | |
253 break; | 253 break; |
254 case "filters.remove": | 254 case "filters.remove": |
255 var filter = Filter.fromText(message.text); | 255 var filter = Filter.fromText(message.text); |
256 var subscription = null; | 256 var subscription = null; |
257 if (message.subscriptionUrl) | 257 if (message.subscriptionUrl) |
258 subscription = Subscription.fromURL(message.subscriptionUrl); | 258 subscription = Subscription.fromURL(message.subscriptionUrl); |
259 | 259 |
260 if (!subscription) | 260 if (!subscription) |
261 FilterStorage.removeFilter(filter); | 261 FilterStorage.removeFilter(filter); |
262 else | 262 else |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 subscription.title = message.title; | 317 subscription.title = message.title; |
318 subscription.homepage = message.homepage; | 318 subscription.homepage = message.homepage; |
319 FilterStorage.addSubscription(subscription); | 319 FilterStorage.addSubscription(subscription); |
320 if (!subscription.lastDownload) | 320 if (!subscription.lastDownload) |
321 Synchronizer.execute(subscription); | 321 Synchronizer.execute(subscription); |
322 } | 322 } |
323 break; | 323 break; |
324 } | 324 } |
325 }); | 325 }); |
326 })(this); | 326 })(this); |
LEFT | RIGHT |