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

Side by Side Diff: background.js

Issue 4864767881641984: Issue 1528 - Implemented backend for general tab of new options page (Closed)
Patch Set: Added addSubscription querystring parameter for testing Created Jan. 30, 2015, 6:06 p.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 | « README.md ('k') | ext/background.js » ('j') | ext/background.js » ('J')
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 16 matching lines...) Expand all
27 var parts = params[i].split("=", 2); 27 var parts = params[i].split("=", 2);
28 if (parts.length == 2 && parts[0] in data) 28 if (parts.length == 2 && parts[0] in data)
29 data[parts[0]] = decodeURIComponent(parts[1]); 29 data[parts[0]] = decodeURIComponent(parts[1]);
30 } 30 }
31 } 31 }
32 } 32 }
33 33
34 var subscriptions =[ 34 var subscriptions =[
35 "https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt", 35 "https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt",
36 "https://easylist-downloads.adblockplus.org/exceptionrules.txt", 36 "https://easylist-downloads.adblockplus.org/exceptionrules.txt",
37 "https://easylist-downloads.adblockplus.org/fanboy-social.txt" 37 "https://easylist-downloads.adblockplus.org/fanboy-social.txt",
38 "~user~786254"
39 ];
40 var filters = [
41 "@@||alternate.de^$document",
42 "@@||der.postillion.com^$document",
43 "@@||taz.de^$document",
44 "@@||amazon.de^$document"
38 ]; 45 ];
39 46
40 var modules = {}; 47 var modules = {};
41 global.require = function(module) 48 global.require = function(module)
42 { 49 {
43 return modules[module]; 50 return modules[module];
44 }; 51 };
45 52
46 modules.utils = { 53 modules.utils = {
47 Utils: { 54 Utils: {
48 getDocLink: function(link) 55 getDocLink: function(link)
49 { 56 {
50 return "https://adblockplus.org/redirect?link=" + encodeURIComponent(lin k); 57 return "https://adblockplus.org/redirect?link=" + encodeURIComponent(lin k);
51 } 58 }
52 } 59 }
53 }; 60 };
54 61
62 modules.prefs = {
63 Prefs: {
64 "subscriptions_exceptionsurl": "https://easylist-downloads.adblockplus.org /exceptionrules.txt"
65 }
66 }
67
55 modules.subscriptionClasses = { 68 modules.subscriptionClasses = {
56 Subscription: function(url) 69 Subscription: function(url)
57 { 70 {
58 this.url = url; 71 this.url = url;
59 this.title = "Subscription " + url; 72 this.title = "Subscription " + url;
60 this.disabled = false; 73 this.disabled = false;
61 this.lastDownload = 1234; 74 this.lastDownload = 1234;
62 }, 75 },
63 76
64 SpecialSubscription: function() {} 77 SpecialSubscription: function(url) {
Felix Dahlke 2015/05/28 20:35:41 Nit: Opening brace on the next line?
Thomas Greiner 2015/06/08 16:12:43 Done.
78 this.url = url;
79 this.disabled = false;
80 this.filters = filters.map(modules.filterClasses.Filter.fromText);
81 }
65 }; 82 };
66 modules.subscriptionClasses.Subscription.fromURL = function(url) 83 modules.subscriptionClasses.Subscription.fromURL = function(url)
67 { 84 {
68 return new modules.subscriptionClasses.Subscription(url); 85 if (/^https?:\/\//.test(url))
86 return new modules.subscriptionClasses.Subscription(url);
87 else
88 return new modules.subscriptionClasses.SpecialSubscription(url);
69 }; 89 };
70 modules.subscriptionClasses.DownloadableSubscription = modules.subscriptionCla sses.Subscription; 90 modules.subscriptionClasses.DownloadableSubscription = modules.subscriptionCla sses.Subscription;
71 91
72 modules.filterStorage = { 92 modules.filterStorage = {
73 FilterStorage: { 93 FilterStorage: {
74 get subscriptions() 94 get subscriptions()
75 { 95 {
76 return subscriptions.map(modules.subscriptionClasses.Subscription.fromUR L); 96 var subscriptions = [];
97 for (var url in modules.filterStorage.FilterStorage.knownSubscriptions)
98 subscriptions.push(modules.filterStorage.FilterStorage.knownSubscripti ons[url]);
99 return subscriptions;
77 }, 100 },
78 101
79 get knownSubscriptions() 102 get knownSubscriptions()
80 { 103 {
81 var result = {}; 104 subscriptions = subscriptions.reduce(function(obj, subscription)
Felix Dahlke 2015/05/28 20:35:41 Can we do something less complex here? Took a whil
Thomas Greiner 2015/06/08 16:12:43 Done.
82 for (var i = 0; i < subscriptions.length; i++) 105 {
83 result[subscriptions[i]] = modules.subscriptionClasses.Subscription.fr omURL(subscriptions[i]); 106 obj[subscription] = modules.subscriptionClasses.Subscription.fromURL(s ubscription);
84 return result; 107 return obj;
108 }, Object.create(null));
109
110 Object.defineProperty(modules.filterStorage.FilterStorage, "knownSubscri ptions", {
Felix Dahlke 2015/05/28 20:35:41 Nit: Opening brace on the next line?
Thomas Greiner 2015/06/08 16:12:43 Done.
111 get: function()
112 {
113 return subscriptions;
114 }
115 });
116
117 return modules.filterStorage.FilterStorage.knownSubscriptions;
85 }, 118 },
86 119
87 addSubscription: function(subscription) 120 addSubscription: function(subscription)
88 { 121 {
89 var index = subscriptions.indexOf(subscription.url); 122 if (!(subscription.url in modules.filterStorage.FilterStorage.knownSubsc riptions))
90 if (index < 0)
91 { 123 {
92 subscriptions.push(subscription.url); 124 subscriptions[subscription.url] = modules.subscriptionClasses.Subscrip tion.fromURL(subscription.url);
93 modules.filterNotifier.FilterNotifier.triggerListeners("subscription.a dded", subscription); 125 modules.filterNotifier.FilterNotifier.triggerListeners("subscription.a dded", subscription);
94 } 126 }
95 }, 127 },
96 128
97 removeSubscription: function(subscription) 129 removeSubscription: function(subscription)
98 { 130 {
99 var index = subscriptions.indexOf(subscription.url); 131 if (subscription.url in modules.filterStorage.FilterStorage.knownSubscri ptions)
100 if (index >= 0)
101 { 132 {
102 subscriptions.splice(index, 1); 133 delete subscriptions[subscription.url];
103 modules.filterNotifier.FilterNotifier.triggerListeners("subscription.r emoved", subscription); 134 modules.filterNotifier.FilterNotifier.triggerListeners("subscription.r emoved", subscription);
104 } 135 }
136 },
137
138 addFilter: function(filter)
139 {
140 var subscription = modules.filterStorage.FilterStorage.knownSubscription s["~user~786254"];
Felix Dahlke 2015/05/28 20:35:41 Seeing this used a few times - how about a "consta
Thomas Greiner 2015/06/08 16:12:43 Done.
141 for (var i = 0; i < subscription.filters.length; i++)
142 {
143 if (subscription.filters[i].text == filter.text)
144 return;
145 }
146 subscription.filters.push(filter);
147 modules.filterNotifier.FilterNotifier.triggerListeners("filter.added", f ilter);
148 },
149
150 removeFilter: function(filter)
151 {
152 var subscription = modules.filterStorage.FilterStorage.knownSubscription s["~user~786254"];
153 for (var i = 0; i < subscription.filters.length; i++)
154 {
155 if (subscription.filters[i].text == filter.text)
156 {
157 subscription.filters.splice(i, 1);
158 modules.filterNotifier.FilterNotifier.triggerListeners("filter.remov ed", filter);
159 return;
160 }
161 }
105 } 162 }
106 } 163 }
107 }; 164 };
108 165
109 modules.filterClasses = { 166 modules.filterClasses = {
110 BlockingFilter: function() {} 167 BlockingFilter: function() {},
168 Filter: function(text)
169 {
170 this.text = text;
171 this.disabled = false;
172 }
173 };
174 modules.filterClasses.Filter.fromText = function(text)
175 {
176 return new modules.filterClasses.Filter(text);
111 }; 177 };
112 178
113 modules.synchronizer = { 179 modules.synchronizer = {
114 Synchronizer: {} 180 Synchronizer: {}
115 }; 181 };
116 182
117 modules.matcher = { 183 modules.matcher = {
118 defaultMatcher: { 184 defaultMatcher: {
119 matchesAny: function(url, requestType, docDomain, thirdParty) 185 matchesAny: function(url, requestType, docDomain, thirdParty)
120 { 186 {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 for (var i = 0; i < listeners.length; i++) 218 for (var i = 0; i < listeners.length; i++)
153 listeners[i].apply(null, args); 219 listeners[i].apply(null, args);
154 } 220 }
155 } 221 }
156 }; 222 };
157 223
158 modules.info = { 224 modules.info = {
159 platform: "gecko", 225 platform: "gecko",
160 platformVersion: "34.0", 226 platformVersion: "34.0",
161 application: "firefox", 227 application: "firefox",
162 applicationVersion: "34.0" 228 applicationVersion: "34.0",
229 addon: "adblockplus",
Felix Dahlke 2015/05/28 20:35:41 Shouldn't this be `addonName`?
Thomas Greiner 2015/06/08 16:12:43 Done. Thanks for pointing out this inconsistency.
230 addonVersion: "2.6.7"
163 }; 231 };
164 updateFromURL(modules.info); 232 updateFromURL(modules.info);
165 233
166 global.Services = { 234 global.Services = {
167 vc: { 235 vc: {
168 compare: function(v1, v2) 236 compare: function(v1, v2)
169 { 237 {
170 return parseFloat(v1) - parseFloat(v2); 238 return parseFloat(v1) - parseFloat(v2);
171 } 239 }
172 } 240 }
173 }; 241 };
174 242
175 var issues = {seenDataCorruption: false, filterlistsReinitialized: false}; 243 var issues = {seenDataCorruption: false, filterlistsReinitialized: false};
176 updateFromURL(issues); 244 updateFromURL(issues);
177 global.seenDataCorruption = issues.seenDataCorruption; 245 global.seenDataCorruption = issues.seenDataCorruption;
178 global.filterlistsReinitialized = issues.filterlistsReinitialized; 246 global.filterlistsReinitialized = issues.filterlistsReinitialized;
247
248 var events = {addSubscription: false};
249 updateFromURL(events);
250 if (events.addSubscription)
251 {
252 // We don't know how long it will take for the page to fully load
253 // so we'll post the message after one second
254 setTimeout(function()
255 {
256 window.postMessage({
257 type: "message",
258 payload: {
259 title: "Custom subscription",
260 url: "http://example.com/custom.txt",
261 type: "add-subscription"
262 }
263 }, "*");
264 }, 1000);
265 }
179 })(this); 266 })(this);
OLDNEW
« no previous file with comments | « README.md ('k') | ext/background.js » ('j') | ext/background.js » ('J')

Powered by Google App Engine
This is Rietveld