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

Delta Between Two Patch Sets: background.js

Issue 29338734: Issue 3839 - Adapt for Prefs event changes (Closed)
Left Patch Set: app.listen doesn't need a FilterNotifier listener Created March 21, 2016, 5:18 p.m.
Right Patch Set: Addresed comments Created March 22, 2016, 2:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | messageResponder.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 (function(global) 18 (function(global)
19 { 19 {
20 function EventEmitter()
21 {
22 this._listeners = Object.create(null);
23 }
24 EventEmitter.prototype = {
25 on: function(name, listener)
26 {
27 if (name in this._listeners)
28 this._listeners[name].push(listener);
29 else
30 this._listeners[name] = [listener];
31 },
32 off: function(name, listener)
33 {
34 var listeners = this._listeners[name];
35 if (listeners)
36 {
37 var idx = listeners.indexOf(listener);
38 if (idx != -1)
39 listeners.splice(idx, 1);
40 }
41 },
42 emit: function(name)
43 {
44 var listeners = this._listeners[name];
45 if (listeners)
46 {
47 for (var i = 0; i < listeners.length; i++)
48 listeners[i].apply(null, Array.prototype.slice.call(arguments, 1));
49 }
50 }
51 };
52
20 function Notifier() 53 function Notifier()
21 { 54 {
22 this._listeners = []; 55 this._eventEmitter = new EventEmitter();
23 } 56 }
24 Notifier.prototype = { 57 Notifier.prototype = {
25 _listeners: null,
26
27 addListener: function(listener) 58 addListener: function(listener)
28 { 59 {
29 if (this._listeners.indexOf(listener) < 0) 60 var listeners = this._eventEmitter._listeners[""];
30 this._listeners.push(listener); 61 if (!listeners || listener.indexOf(listener) == -1)
31 }, 62 this._eventEmitter.on("", listener);
32 63 },
33 removeListener: function(listener) 64 removeListener: function(listener)
34 { 65 {
35 var index = this._listeners.indexOf(listener); 66 this._eventEmitter.off("", listener);
36 if (index >= 0) 67 },
37 this._listeners.splice(index, 1);
38 },
39
40 triggerListeners: function() 68 triggerListeners: function()
41 { 69 {
42 var args = Array.prototype.slice.apply(arguments); 70 var args = Array.prototype.slice.apply(arguments);
43 var listeners = this._listeners.slice(); 71 args.unshift("");
44 for (var i = 0; i < listeners.length; i++) 72 this._eventEmitter.emit.apply(this._eventEmitter, args);
45 listeners[i].apply(null, args);
46 } 73 }
47 }; 74 };
48 75
49 function updateFromURL(data) 76 function updateFromURL(data)
50 { 77 {
51 if (window.location.search) 78 if (window.location.search)
52 { 79 {
53 var params = window.location.search.substr(1).split("&"); 80 var params = window.location.search.substr(1).split("&");
54 for (var i = 0; i < params.length; i++) 81 for (var i = 0; i < params.length; i++)
55 { 82 {
(...skipping 28 matching lines...) Expand all
84 { 111 {
85 return "https://adblockplus.org/redirect?link=" + encodeURIComponent(lin k); 112 return "https://adblockplus.org/redirect?link=" + encodeURIComponent(lin k);
86 }, 113 },
87 get appLocale() 114 get appLocale()
88 { 115 {
89 return parent.ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-"); 116 return parent.ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-");
90 } 117 }
91 } 118 }
92 }; 119 };
93 120
94 modules.prefs = { 121 modules.prefs = {Prefs: new EventEmitter()};
95 Prefs: {
96 _listeners: Object.create(null),
97
98 on: function(preference, callback)
99 {
100 if (preference in this._listeners)
101 this._listeners[preference].push(callback);
102 else
103 this._listeners[preference] = [callback];
104 }
105 }
106 };
107 var prefs = { 122 var prefs = {
108 notifications_ignoredcategories: (params.showNotificationUI) ? ["*"] : [], 123 notifications_ignoredcategories: (params.showNotificationUI) ? ["*"] : [],
109 notifications_showui: params.showNotificationUI, 124 notifications_showui: params.showNotificationUI,
110 safari_contentblocker: false, 125 safari_contentblocker: false,
111 shouldShowBlockElementMenu: true, 126 shouldShowBlockElementMenu: true,
112 show_devtools_panel: true, 127 show_devtools_panel: true,
113 subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/exc eptionrules.txt" 128 subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/exc eptionrules.txt"
114 }; 129 };
115 Object.keys(prefs).forEach(function(key) 130 Object.keys(prefs).forEach(function(key)
116 { 131 {
117 Object.defineProperty(modules.prefs.Prefs, key, { 132 Object.defineProperty(modules.prefs.Prefs, key, {
118 get: function() 133 get: function()
119 { 134 {
120 return prefs[key]; 135 return prefs[key];
121 }, 136 },
122 set: function(value) 137 set: function(value)
123 { 138 {
124 prefs[key] = value; 139 prefs[key] = value;
125 140 modules.prefs.Prefs.emit(key);
126 var listeners = modules.prefs.Prefs._listeners[key];
127 if (listeners)
128 for (var i = 0; i < listeners.length; i++)
129 listeners[i]();
130 } 141 }
131 }); 142 });
132 }); 143 });
133 144
134 modules.notification = { 145 modules.notification = {
135 Notification: { 146 Notification: {
136 toggleIgnoreCategory: function(category) 147 toggleIgnoreCategory: function(category)
137 { 148 {
138 var categories = prefs.notifications_ignoredcategories; 149 var categories = prefs.notifications_ignoredcategories;
139 var index = categories.indexOf(category); 150 var index = categories.indexOf(category);
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 492
482 if (params.safariContentBlocker) 493 if (params.safariContentBlocker)
483 { 494 {
484 global.safari = { 495 global.safari = {
485 extension: { 496 extension: {
486 setContentBlocker: function() {} 497 setContentBlocker: function() {}
487 } 498 }
488 }; 499 };
489 } 500 }
490 })(this); 501 })(this);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld