Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: messageResponder.js

Issue 29321198: Issue 2376 - Implement custom filters in new options page (Closed)
Patch Set: Small fixes Created July 14, 2015, 11:16 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 12 matching lines...) Expand all
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 BlockingFilter = filterClasses.BlockingFilter; 31 var BlockingFilter = filterClasses.BlockingFilter;
32 var Synchronizer = require("synchronizer").Synchronizer; 32 var Synchronizer = require("synchronizer").Synchronizer;
33 var parseFilters = require("filterValidation").parseFilters;
34 var parseFilter = require("filterValidation").parseFilter;
33 35
34 var subscriptionClasses = require("subscriptionClasses"); 36 var subscriptionClasses = require("subscriptionClasses");
35 var Subscription = subscriptionClasses.Subscription; 37 var Subscription = subscriptionClasses.Subscription;
36 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; 38 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription;
37 var SpecialSubscription = subscriptionClasses.SpecialSubscription; 39 var SpecialSubscription = subscriptionClasses.SpecialSubscription;
38 40
39 function convertObject(keys, obj) 41 function convertObject(keys, obj)
40 { 42 {
41 var result = {}; 43 var result = {};
42 for (var i = 0; i < keys.length; i++) 44 for (var i = 0; i < keys.length; i++)
43 result[keys[i]] = obj[keys[i]]; 45 result[keys[i]] = obj[keys[i]];
44 return result; 46 return result;
45 } 47 }
46 48
47 var convertSubscription = convertObject.bind(null, ["disabled", 49 var convertSubscription = convertObject.bind(null, ["disabled",
48 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); 50 "downloadStatus", "homepage", "lastSuccess", "title", "url"]);
49 var convertFilter = convertObject.bind(null, ["text"]); 51 var convertFilter = convertObject.bind(null, ["text"]);
50 52
51 var changeListeners = null; 53 var changeListeners = null;
52 var messageTypes = { 54 var messageTypes = {
53 "app": "app.listen", 55 "app": "app.listen",
54 "filter": "filters.listen", 56 "filter": "filters.listen",
55 "subscription": "subscriptions.listen" 57 "subscription": "subscriptions.listen"
56 }; 58 };
57 59
60 function sendMessage(type, action, args, page)
61 {
62 var pages = page ? [page] : changeListeners.keys();
63 for (var i = 0; i < pages.length; i++)
64 {
65 var filters = changeListeners.get(pages[i]);
66 if (filters[type] && filters[type].indexOf(action) >= 0)
67 {
68 pages[i].sendMessage({
69 type: messageTypes[type],
70 action: action,
71 args: args
72 });
73 }
74 }
75 }
76
58 function onFilterChange(action) 77 function onFilterChange(action)
59 { 78 {
60 if (action == "load") 79 if (action == "load")
61 action = "filter.loaded"; 80 action = "filter.loaded";
62 81
63 var parts = action.split(".", 2); 82 var parts = action.split(".", 2);
64 var type; 83 var type;
65 if (parts.length == 1) 84 if (parts.length == 1)
66 { 85 {
67 type = "app"; 86 type = "app";
(...skipping 10 matching lines...) Expand all
78 97
79 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) 98 var args = Array.prototype.slice.call(arguments, 1).map(function(arg)
80 { 99 {
81 if (arg instanceof Subscription) 100 if (arg instanceof Subscription)
82 return convertSubscription(arg); 101 return convertSubscription(arg);
83 else if (arg instanceof Filter) 102 else if (arg instanceof Filter)
84 return convertFilter(arg); 103 return convertFilter(arg);
85 else 104 else
86 return arg; 105 return arg;
87 }); 106 });
88 107 sendMessage(type, action, args);
89 var pages = changeListeners.keys(); 108 }
90 for (var i = 0; i < pages.length; i++)
91 {
92 var filters = changeListeners.get(pages[i]);
93 if (filters[type] && filters[type].indexOf(action) >= 0)
94 {
95 pages[i].sendMessage({
96 type: messageTypes[type],
97 action: action,
98 args: args
99 });
100 }
101 }
102 };
103 109
104 global.ext.onMessage.addListener(function(message, sender, callback) 110 global.ext.onMessage.addListener(function(message, sender, callback)
105 { 111 {
106 var listenerFilters = null; 112 var listenerFilters = null;
107 switch (message.type) 113 switch (message.type)
108 { 114 {
109 case "app.listen": 115 case "app.listen":
110 case "filters.listen": 116 case "filters.listen":
111 case "subscriptions.listen": 117 case "subscriptions.listen":
112 if (!changeListeners) 118 if (!changeListeners)
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 case "filters.get": 194 case "filters.get":
189 var subscription = Subscription.fromURL(message.subscriptionUrl); 195 var subscription = Subscription.fromURL(message.subscriptionUrl);
190 if (!subscription) 196 if (!subscription)
191 { 197 {
192 callback([]); 198 callback([]);
193 break; 199 break;
194 } 200 }
195 201
196 callback(subscription.filters.map(convertFilter)); 202 callback(subscription.filters.map(convertFilter));
197 break; 203 break;
204 case "filters.importRaw":
205 var result = parseFilters(message.text);
206 var errors = result.errors.filter(function(error)
207 {
208 return error.type != "unexpected-filter-list-header";
209 });
210
211 if (errors.length > 0)
212 {
213 sendMessage("app", "error", errors, sender.page);
214 return;
215 }
216
217 var seenFilter = Object.create(null);
218 for (var i = 0; i < result.filters.length; i++)
219 {
220 var filter = result.filters[i];
221 FilterStorage.addFilter(filter);
222 seenFilter[filter.text] = null;
223 }
224
225 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
226 {
227 var subscription = FilterStorage.subscriptions[i];
228 if (!(subscription instanceof SpecialSubscription))
229 continue;
230
231 var filters = subscription.filters.slice();
232 for (var j = 0; j < filters.length; j++)
233 {
234 var filter = filters[j];
235 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
236 continue;
237
238 if (!(filter.text in seenFilter))
239 FilterStorage.removeFilter(filter);
240 }
241 }
242 break;
198 case "filters.listen": 243 case "filters.listen":
199 if (message.filter) 244 if (message.filter)
200 listenerFilters.filter = message.filter; 245 listenerFilters.filter = message.filter;
201 else 246 else
202 delete listenerFilters.filter; 247 delete listenerFilters.filter;
203 break; 248 break;
249 case "filters.parse":
Sebastian Noack 2015/07/14 11:57:24 IMO this should be renamed to "filters.add", repla
saroyanm 2015/07/14 13:22:59 Done.
250 var result = parseFilter(message.text);
251 if (result.error)
252 sendMessage("app", "error", [result.error.toString()], sender.page);
253 else
254 FilterStorage.addFilter(result.filter);
255 break;
204 case "filters.remove": 256 case "filters.remove":
205 var filter = Filter.fromText(message.text); 257 var filter = Filter.fromText(message.text);
206 var subscription = null; 258 var subscription = null;
207 if (message.subscriptionUrl) 259 if (message.subscriptionUrl)
208 subscription = Subscription.fromURL(message.subscriptionUrl); 260 subscription = Subscription.fromURL(message.subscriptionUrl);
209 261
210 if (!subscription) 262 if (!subscription)
211 FilterStorage.removeFilter(filter); 263 FilterStorage.removeFilter(filter);
212 else 264 else
213 FilterStorage.removeFilter(filter, subscription, message.index); 265 FilterStorage.removeFilter(filter, subscription, message.index);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 subscription.title = message.title; 319 subscription.title = message.title;
268 subscription.homepage = message.homepage; 320 subscription.homepage = message.homepage;
269 FilterStorage.addSubscription(subscription); 321 FilterStorage.addSubscription(subscription);
270 if (!subscription.lastDownload) 322 if (!subscription.lastDownload)
271 Synchronizer.execute(subscription); 323 Synchronizer.execute(subscription);
272 } 324 }
273 break; 325 break;
274 } 326 }
275 }); 327 });
276 })(this); 328 })(this);
OLDNEW
« no previous file with comments | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld