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