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 /* globals require */ |
| 19 |
18 "use strict"; | 20 "use strict"; |
19 | 21 |
20 { | 22 { |
21 var ext = ext || require("ext_background"); | 23 if (typeof ext == "undefined") |
| 24 window.ext = require("ext_background"); |
22 | 25 |
23 const {port} = require("messaging"); | 26 const {port} = require("messaging"); |
24 const {Prefs} = require("prefs"); | 27 const {Prefs} = require("prefs"); |
25 const {Utils} = require("utils"); | 28 const {Utils} = require("utils"); |
26 const {FilterStorage} = require("filterStorage"); | 29 const {FilterStorage} = require("filterStorage"); |
27 const {FilterNotifier} = require("filterNotifier"); | 30 const {FilterNotifier} = require("filterNotifier"); |
28 const {defaultMatcher} = require("matcher"); | 31 const {defaultMatcher} = require("matcher"); |
29 const {ElemHideEmulation} = require("elemHideEmulation"); | 32 const {ElemHideEmulation} = require("elemHideEmulation"); |
30 const {Notification: NotificationStorage} = require("notification"); | 33 const {Notification: NotificationStorage} = require("notification"); |
31 | 34 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 "lastDownload", "title", "url"], subscription); | 73 "lastDownload", "title", "url"], subscription); |
71 obj.isDownloading = Synchronizer.isExecuting(subscription.url); | 74 obj.isDownloading = Synchronizer.isExecuting(subscription.url); |
72 return obj; | 75 return obj; |
73 } | 76 } |
74 | 77 |
75 let convertFilter = convertObject.bind(null, ["text"]); | 78 let convertFilter = convertObject.bind(null, ["text"]); |
76 | 79 |
77 let changeListeners = new ext.PageMap(); | 80 let changeListeners = new ext.PageMap(); |
78 let listenedPreferences = Object.create(null); | 81 let listenedPreferences = Object.create(null); |
79 let listenedFilterChanges = Object.create(null); | 82 let listenedFilterChanges = Object.create(null); |
80 let messageTypes = { | 83 let messageTypes = new Map([ |
81 "app": "app.respond", | 84 ["app", "app.respond"], |
82 "filter": "filters.respond", | 85 ["filter", "filters.respond"], |
83 "pref": "prefs.respond", | 86 ["pref", "prefs.respond"], |
84 "subscription": "subscriptions.respond" | 87 ["subscription", "subscriptions.respond"] |
85 }; | 88 ]); |
86 | 89 |
87 function sendMessage(type, action) | 90 function sendMessage(type, action, ...args) |
88 { | 91 { |
89 let pages = changeListeners.keys(); | 92 let pages = changeListeners.keys(); |
90 if (pages.length == 0) | 93 if (pages.length == 0) |
91 return; | 94 return; |
92 | 95 |
93 let args = []; | 96 let convertedArgs = []; |
94 for (let i = 2; i < arguments.length; i++) | 97 for (let arg of args) |
95 { | 98 { |
96 let arg = arguments[i]; | |
97 if (arg instanceof Subscription) | 99 if (arg instanceof Subscription) |
98 args.push(convertSubscription(arg)); | 100 convertedArgs.push(convertSubscription(arg)); |
99 else if (arg instanceof Filter) | 101 else if (arg instanceof Filter) |
100 args.push(convertFilter(arg)); | 102 convertedArgs.push(convertFilter(arg)); |
101 else | 103 else |
102 args.push(arg); | 104 convertedArgs.push(arg); |
103 } | 105 } |
104 | 106 |
105 for (let page of pages) | 107 for (let page of pages) |
106 { | 108 { |
107 let filters = changeListeners.get(page); | 109 let filters = changeListeners.get(page); |
108 let actions = filters[type]; | 110 let actions = filters[type]; |
109 if (actions && actions.indexOf(action) != -1) | 111 if (actions && actions.indexOf(action) != -1) |
110 { | 112 { |
111 page.sendMessage({ | 113 page.sendMessage({ |
112 type: messageTypes[type], | 114 type: messageTypes.get(type), |
113 action: action, | 115 action, |
114 args: args | 116 args: convertedArgs |
115 }); | 117 }); |
116 } | 118 } |
117 } | 119 } |
118 } | 120 } |
119 | 121 |
120 function addFilterListeners(type, actions) | 122 function addFilterListeners(type, actions) |
121 { | 123 { |
122 for (let action of actions) | 124 for (let action of actions) |
123 { | 125 { |
124 let name; | 126 let name; |
125 if (type == "filter" && action == "loaded") | 127 if (type == "filter" && action == "loaded") |
126 name = "load"; | 128 name = "load"; |
127 else | 129 else |
128 name = type + "." + action; | 130 name = type + "." + action; |
129 | 131 |
130 if (!(name in listenedFilterChanges)) | 132 if (!(name in listenedFilterChanges)) |
131 { | 133 { |
132 listenedFilterChanges[name] = null; | 134 listenedFilterChanges[name] = null; |
133 FilterNotifier.on(name, function() | 135 FilterNotifier.on(name, (...args) => |
134 { | 136 { |
135 let args = [type, action]; | 137 sendMessage(type, action, ...args); |
136 for (let arg of arguments) | |
137 args.push(arg); | |
138 sendMessage.apply(null, args); | |
139 }); | 138 }); |
140 } | 139 } |
141 } | 140 } |
142 } | 141 } |
143 | 142 |
144 function getListenerFilters(page) | 143 function getListenerFilters(page) |
145 { | 144 { |
146 let listenerFilters = changeListeners.get(page); | 145 let listenerFilters = changeListeners.get(page); |
147 if (!listenerFilters) | 146 if (!listenerFilters) |
148 { | 147 { |
149 listenerFilters = Object.create(null); | 148 listenerFilters = Object.create(null); |
150 changeListeners.set(page, listenerFilters); | 149 changeListeners.set(page, listenerFilters); |
151 } | 150 } |
152 return listenerFilters; | 151 return listenerFilters; |
153 } | 152 } |
154 | 153 |
155 port.on("app.get", (message, sender) => | 154 port.on("app.get", (message, sender) => |
156 { | 155 { |
157 if (message.what == "issues") | 156 if (message.what == "issues") |
158 { | 157 { |
159 let subscriptionInit = tryRequire("subscriptionInit"); | 158 let subscriptionInit = tryRequire("subscriptionInit"); |
160 return { | 159 return { |
161 filterlistsReinitialized: subscriptionInit ? subscriptionInit.reinitiali
zed : false | 160 filterlistsReinitialized: subscriptionInit |
| 161 ? subscriptionInit.reinitialized : false |
162 }; | 162 }; |
163 } | 163 } |
164 | 164 |
165 if (message.what == "doclink") | 165 if (message.what == "doclink") |
166 return Utils.getDocLink(message.link); | 166 return Utils.getDocLink(message.link); |
167 | 167 |
168 if (message.what == "localeInfo") | 168 if (message.what == "localeInfo") |
169 { | 169 { |
170 let bidiDir; | 170 let bidiDir; |
171 if ("chromeRegistry" in Utils) | 171 if ("chromeRegistry" in Utils) |
172 bidiDir = Utils.chromeRegistry.isLocaleRTL("adblockplus") ? "rtl" : "ltr
"; | 172 { |
| 173 let isRtl = Utils.chromeRegistry.isLocaleRTL("adblockplus"); |
| 174 bidiDir = isRtl ? "rtl" : "ltr"; |
| 175 } |
173 else | 176 else |
174 bidiDir = ext.i18n.getMessage("@@bidi_dir"); | 177 bidiDir = ext.i18n.getMessage("@@bidi_dir"); |
175 | 178 |
176 return {locale: Utils.appLocale, bidiDir: bidiDir}; | 179 return {locale: Utils.appLocale, bidiDir}; |
177 } | 180 } |
178 | 181 |
179 if (message.what == "features") | 182 if (message.what == "features") |
180 { | 183 { |
181 return { | 184 return { |
182 devToolsPanel: info.platform == "chromium" | 185 devToolsPanel: info.platform == "chromium" |
183 }; | 186 }; |
184 } | 187 } |
185 | 188 |
186 return info[message.what]; | 189 return info[message.what]; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 { | 225 { |
223 if (message.what == "elemhideemulation") | 226 if (message.what == "elemhideemulation") |
224 { | 227 { |
225 let filters = []; | 228 let filters = []; |
226 const {checkWhitelisted} = require("whitelisting"); | 229 const {checkWhitelisted} = require("whitelisting"); |
227 | 230 |
228 if (Prefs.enabled && !checkWhitelisted(sender.page, sender.frame, | 231 if (Prefs.enabled && !checkWhitelisted(sender.page, sender.frame, |
229 RegExpFilter.typeMap.DOCUMENT | | 232 RegExpFilter.typeMap.DOCUMENT | |
230 RegExpFilter.typeMap.ELEMHIDE)) | 233 RegExpFilter.typeMap.ELEMHIDE)) |
231 { | 234 { |
232 let hostname = sender.frame.url.hostname; | 235 let {hostname} = sender.frame.url; |
233 filters = ElemHideEmulation.getRulesForDomain(hostname); | 236 filters = ElemHideEmulation.getRulesForDomain(hostname); |
234 filters = filters.map(filter => | 237 filters = filters.map((filter) => |
235 { | 238 { |
236 return { | 239 return { |
237 selector: filter.selector, | 240 selector: filter.selector, |
238 text: filter.text | 241 text: filter.text |
239 }; | 242 }; |
240 }); | 243 }); |
241 } | 244 } |
242 return filters; | 245 return filters; |
243 } | 246 } |
244 | 247 |
(...skipping 28 matching lines...) Expand all Loading... |
273 return errors; | 276 return errors; |
274 | 277 |
275 for (let subscription of FilterStorage.subscriptions) | 278 for (let subscription of FilterStorage.subscriptions) |
276 { | 279 { |
277 if (!(subscription instanceof SpecialSubscription)) | 280 if (!(subscription instanceof SpecialSubscription)) |
278 continue; | 281 continue; |
279 | 282 |
280 for (let j = subscription.filters.length - 1; j >= 0; j--) | 283 for (let j = subscription.filters.length - 1; j >= 0; j--) |
281 { | 284 { |
282 let filter = subscription.filters[j]; | 285 let filter = subscription.filters[j]; |
283 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) | 286 if (/^@@\|\|([^/:]+)\^\$document$/.test(filter.text)) |
284 continue; | 287 continue; |
285 | 288 |
286 if (!(filter.text in seenFilter)) | 289 if (!(filter.text in seenFilter)) |
287 FilterStorage.removeFilter(filter); | 290 FilterStorage.removeFilter(filter); |
288 } | 291 } |
289 } | 292 } |
290 | 293 |
291 return errors; | 294 return errors; |
292 }); | 295 }); |
293 | 296 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 ext.showOptions(() => | 355 ext.showOptions(() => |
353 { | 356 { |
354 sendMessage("app", "addSubscription", subscription); | 357 sendMessage("app", "addSubscription", subscription); |
355 }); | 358 }); |
356 } | 359 } |
357 else | 360 else |
358 { | 361 { |
359 subscription.disabled = false; | 362 subscription.disabled = false; |
360 FilterStorage.addSubscription(subscription); | 363 FilterStorage.addSubscription(subscription); |
361 | 364 |
362 if (subscription instanceof DownloadableSubscription && !subscription.last
Download) | 365 if (subscription instanceof DownloadableSubscription && |
| 366 !subscription.lastDownload) |
363 Synchronizer.execute(subscription); | 367 Synchronizer.execute(subscription); |
364 } | 368 } |
365 }); | 369 }); |
366 | 370 |
367 port.on("subscriptions.get", (message, sender) => | 371 port.on("subscriptions.get", (message, sender) => |
368 { | 372 { |
369 let subscriptions = FilterStorage.subscriptions.filter(s => | 373 let subscriptions = FilterStorage.subscriptions.filter((s) => |
370 { | 374 { |
371 if (message.ignoreDisabled && s.disabled) | 375 if (message.ignoreDisabled && s.disabled) |
372 return false; | 376 return false; |
373 if (s instanceof DownloadableSubscription && message.downloadable) | 377 if (s instanceof DownloadableSubscription && message.downloadable) |
374 return true; | 378 return true; |
375 if (s instanceof SpecialSubscription && message.special) | 379 if (s instanceof SpecialSubscription && message.special) |
376 return true; | 380 return true; |
377 return false; | 381 return false; |
378 }); | 382 }); |
379 | 383 |
(...skipping 29 matching lines...) Expand all Loading... |
409 subscription.title = message.title; | 413 subscription.title = message.title; |
410 subscription.homepage = message.homepage; | 414 subscription.homepage = message.homepage; |
411 FilterStorage.addSubscription(subscription); | 415 FilterStorage.addSubscription(subscription); |
412 if (!subscription.lastDownload) | 416 if (!subscription.lastDownload) |
413 Synchronizer.execute(subscription); | 417 Synchronizer.execute(subscription); |
414 } | 418 } |
415 }); | 419 }); |
416 | 420 |
417 port.on("subscriptions.update", (message, sender) => | 421 port.on("subscriptions.update", (message, sender) => |
418 { | 422 { |
419 let subscriptions = message.url ? [Subscription.fromURL(message.url)] : | 423 let {subscriptions} = FilterStorage; |
420 FilterStorage.subscriptions; | 424 if (message.url) |
| 425 subscriptions = [Subscription.fromURL(message.url)]; |
| 426 |
421 for (let subscription of subscriptions) | 427 for (let subscription of subscriptions) |
422 { | 428 { |
423 if (subscription instanceof DownloadableSubscription) | 429 if (subscription instanceof DownloadableSubscription) |
424 Synchronizer.execute(subscription, true); | 430 Synchronizer.execute(subscription, true); |
425 } | 431 } |
426 }); | 432 }); |
427 } | 433 } |
OLD | NEW |