LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 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() { | 18 (function() |
| 19 { |
19 /* Events */ | 20 /* Events */ |
20 | 21 |
21 var TabEventTarget = function() { | 22 var TabEventTarget = function() |
| 23 { |
22 WrappedEventTarget.apply(this, arguments); | 24 WrappedEventTarget.apply(this, arguments); |
23 | 25 |
24 this._tabs = {}; | 26 this._tabs = {}; |
25 | 27 |
26 this._sharedListener = this._sharedListener.bind(this); | 28 this._sharedListener = this._sharedListener.bind(this); |
27 this._removeTab = this._removeTab.bind(this); | 29 this._removeTab = this._removeTab.bind(this); |
28 }; | 30 }; |
29 TabEventTarget.prototype = { | 31 TabEventTarget.prototype = { |
30 __proto__: WrappedEventTarget.prototype, | 32 __proto__: WrappedEventTarget.prototype, |
31 _bindToTab: function(tab) { | 33 _bindToTab: function(tab) |
| 34 { |
32 return { | 35 return { |
33 addListener: function(listener) { | 36 addListener: function(listener) |
| 37 { |
34 var listeners = this._tabs[tab._id]; | 38 var listeners = this._tabs[tab._id]; |
35 | 39 |
36 if (!listeners) { | 40 if (!listeners) |
| 41 { |
37 this._tabs[tab._id] = listeners = []; | 42 this._tabs[tab._id] = listeners = []; |
38 | 43 |
39 if (Object.keys(this._tabs).length == 1) | 44 if (Object.keys(this._tabs).length == 1) |
40 this.addListener(this._sharedListener); | 45 this.addListener(this._sharedListener); |
41 | 46 |
42 tab.onRemoved.addListener(this._removeTab); | 47 tab.onRemoved.addListener(this._removeTab); |
43 } | 48 } |
44 | 49 |
45 listeners.push(listener); | 50 listeners.push(listener); |
46 }.bind(this), | 51 }.bind(this), |
47 removeListener: function(listener) { | 52 removeListener: function(listener) |
| 53 { |
48 var listeners = this._tabs[tab._id]; | 54 var listeners = this._tabs[tab._id]; |
49 if (!listeners) | 55 if (!listeners) |
50 return; | 56 return; |
51 | 57 |
52 var idx = listeners.indexOf(listener); | 58 var idx = listeners.indexOf(listener); |
53 if (idx == -1) | 59 if (idx == -1) |
54 return; | 60 return; |
55 | 61 |
56 listeners.splice(idx, 1); | 62 listeners.splice(idx, 1); |
57 | 63 |
58 if (listeners.length == 0) | 64 if (listeners.length == 0) |
59 tab.onRemoved.removeListener(this._removeTab); | 65 tab.onRemoved.removeListener(this._removeTab); |
60 else { | 66 else |
| 67 { |
61 if (listeners.length > 1) | 68 if (listeners.length > 1) |
62 return; | 69 return; |
63 if (listeners[0] != this._removeTab) | 70 if (listeners[0] != this._removeTab) |
64 return; | 71 return; |
65 } | 72 } |
66 | 73 |
67 this._removeTab(tab); | 74 this._removeTab(tab); |
68 }.bind(this) | 75 }.bind(this) |
69 }; | 76 }; |
70 }, | 77 }, |
71 _sharedListener: function(tab) { | 78 _sharedListener: function(tab) |
| 79 { |
72 var listeners = this._tabs[tab._id]; | 80 var listeners = this._tabs[tab._id]; |
73 | 81 |
74 if (!listeners) | 82 if (!listeners) |
75 return; | 83 return; |
76 | 84 |
77 // copy listeners before calling them, because of they | 85 // copy listeners before calling them, because they might |
78 // might add or remove other listeners, which must not be | 86 // add or remove other listeners, which must not be taken |
79 // taken into account before the next occurrance of the event | 87 // into account before the next occurrence of the event |
80 listeners = listeners.slice(0); | 88 listeners = listeners.slice(0); |
81 | 89 |
82 for (var i = 0; i < listeners.length; i++) | 90 for (var i = 0; i < listeners.length; i++) |
83 listeners[i](tab); | 91 listeners[i](tab); |
84 }, | 92 }, |
85 _removeTab: function(tab) { | 93 _removeTab: function(tab) |
| 94 { |
86 delete this._tabs[tab._id]; | 95 delete this._tabs[tab._id]; |
87 | 96 |
88 if (Object.keys(this._tabs).length == 0) | 97 if (Object.keys(this._tabs).length == 0) |
89 this.removeListener(this._sharedListener); | 98 this.removeListener(this._sharedListener); |
90 } | 99 } |
91 }; | 100 }; |
92 | 101 |
93 var BeforeNavigateTabEventTarget = function() { | 102 var BeforeNavigateTabEventTarget = function() |
| 103 { |
94 TabEventTarget.call(this, chrome.tabs.onUpdated); | 104 TabEventTarget.call(this, chrome.tabs.onUpdated); |
95 }; | 105 }; |
96 BeforeNavigateTabEventTarget.prototype = { | 106 BeforeNavigateTabEventTarget.prototype = { |
97 __proto__: TabEventTarget.prototype, | 107 __proto__: TabEventTarget.prototype, |
98 _wrapListener: function(listener) { | 108 _wrapListener: function(listener) |
99 return function(id, info, tab) { | 109 { |
| 110 return function(id, info, tab) |
| 111 { |
100 if ("url" in info) | 112 if ("url" in info) |
101 listener(new Tab(tab)); | 113 listener(new Tab(tab)); |
102 }; | 114 }; |
103 } | 115 } |
104 }; | 116 }; |
105 | 117 |
106 var CompletedTabEventTarget = function() { | 118 var CompletedTabEventTarget = function() |
| 119 { |
107 TabEventTarget.call(this, chrome.tabs.onUpdated); | 120 TabEventTarget.call(this, chrome.tabs.onUpdated); |
108 }; | 121 }; |
109 CompletedTabEventTarget.prototype = { | 122 CompletedTabEventTarget.prototype = { |
110 __proto__: TabEventTarget.prototype, | 123 __proto__: TabEventTarget.prototype, |
111 _wrapListener: function(listener) { | 124 _wrapListener: function(listener) |
112 return function(id, info, tab) { | 125 { |
| 126 return function(id, info, tab) |
| 127 { |
113 if (info.status == "complete") | 128 if (info.status == "complete") |
114 listener(new Tab(tab)); | 129 listener(new Tab(tab)); |
115 }; | 130 }; |
116 } | 131 } |
117 }; | 132 }; |
118 | 133 |
119 var ActivatedTabEventTarget = function() { | 134 var ActivatedTabEventTarget = function() |
| 135 { |
120 TabEventTarget.call(this, chrome.tabs.onActivated); | 136 TabEventTarget.call(this, chrome.tabs.onActivated); |
121 }; | 137 }; |
122 ActivatedTabEventTarget.prototype = { | 138 ActivatedTabEventTarget.prototype = { |
123 __proto__: TabEventTarget.prototype, | 139 __proto__: TabEventTarget.prototype, |
124 _wrapListener: function(listener) { | 140 _wrapListener: function(listener) |
125 return function(info) { | 141 { |
126 chrome.tabs.get(info.tabId, function(tab) { | 142 return function(info) |
| 143 { |
| 144 chrome.tabs.get(info.tabId, function(tab) |
| 145 { |
127 listener(new Tab(tab)); | 146 listener(new Tab(tab)); |
128 }); | 147 }); |
129 }; | 148 }; |
130 } | 149 } |
131 } | 150 } |
132 | 151 |
133 var RemovedTabEventTarget = function() { | 152 var RemovedTabEventTarget = function() |
| 153 { |
134 TabEventTarget.call(this, chrome.tabs.onRemoved); | 154 TabEventTarget.call(this, chrome.tabs.onRemoved); |
135 }; | 155 }; |
136 RemovedTabEventTarget.prototype = { | 156 RemovedTabEventTarget.prototype = { |
137 __proto__: TabEventTarget.prototype, | 157 __proto__: TabEventTarget.prototype, |
138 _wrapListener: function(listener) { | 158 _wrapListener: function(listener) |
139 return function(id) { | 159 { |
140 listener(new Tab({id: id})); | 160 return function(id) { listener(new Tab({id: id})); }; |
141 }; | 161 } |
142 } | 162 }; |
143 }; | 163 |
144 | 164 var BeforeRequestEventTarget = function() |
145 var BeforeRequestEventTarget = function() { | 165 { |
146 WrappedEventTarget.call(this, chrome.webRequest.onBeforeRequest); | 166 WrappedEventTarget.call(this, chrome.webRequest.onBeforeRequest); |
147 }; | 167 }; |
148 BeforeRequestEventTarget.prototype = { | 168 BeforeRequestEventTarget.prototype = { |
149 __proto__: WrappedEventTarget.prototype, | 169 __proto__: WrappedEventTarget.prototype, |
150 _wrapListener: function(listener) { | 170 _wrapListener: function(listener) |
151 return function(details) { | 171 { |
| 172 return function(details) |
| 173 { |
152 var tab = null; | 174 var tab = null; |
153 | 175 |
154 if (details.tabId != -1) | 176 if (details.tabId != -1) |
155 tab = new Tab({id: details.tabId}); | 177 tab = new Tab({id: details.tabId}); |
156 | 178 |
157 return {cancel: listener( | 179 return {cancel: listener( |
158 details.url, | 180 details.url, |
159 details.type, | 181 details.type, |
160 tab, | 182 tab, |
161 details.frameId, | 183 details.frameId, |
162 details.parentFrameId | 184 details.parentFrameId |
163 ) === false}; | 185 ) === false}; |
164 }; | 186 }; |
165 }, | 187 }, |
166 _prepareExtraArguments: function(urls) { | 188 _prepareExtraArguments: function(urls) |
| 189 { |
167 return [urls ? {urls: urls} : {}, ["blocking"]]; | 190 return [urls ? {urls: urls} : {}, ["blocking"]]; |
168 } | 191 } |
169 }; | 192 }; |
170 | 193 |
171 | 194 |
172 /* Tabs */ | 195 /* Tabs */ |
173 | 196 |
174 var PageAction = function(tabId) { | 197 var PageAction = function(tabId) |
| 198 { |
175 this._tabId = tabId; | 199 this._tabId = tabId; |
176 }; | 200 }; |
177 PageAction.prototype = { | 201 PageAction.prototype = { |
178 setIcon: function(path) { | 202 setIcon: function(path) |
| 203 { |
179 chrome.pageAction.setIcon({tabId: this._tabId, path: path}); | 204 chrome.pageAction.setIcon({tabId: this._tabId, path: path}); |
180 }, | 205 }, |
181 setTitle: function(title) { | 206 setTitle: function(title) |
| 207 { |
182 chrome.pageAction.setTitle({tabId: this._tabId, title: title}); | 208 chrome.pageAction.setTitle({tabId: this._tabId, title: title}); |
183 }, | 209 }, |
184 hide: function() { | 210 hide: function() |
| 211 { |
185 chrome.pageAction.hide(this._tabId); | 212 chrome.pageAction.hide(this._tabId); |
186 }, | 213 }, |
187 show: function() { | 214 show: function() |
| 215 { |
188 chrome.pageAction.show(this._tabId); | 216 chrome.pageAction.show(this._tabId); |
189 } | 217 } |
190 }; | 218 }; |
191 | 219 |
192 Tab = function(tab) { | 220 Tab = function(tab) |
| 221 { |
193 this._id = tab.id; | 222 this._id = tab.id; |
194 | 223 |
195 this.url = tab.url; | 224 this.url = tab.url; |
196 this.pageAction = new PageAction(tab.id); | 225 this.pageAction = new PageAction(tab.id); |
197 | 226 |
198 this.onBeforeNavigate = ext.tabs.onBeforeNavigate._bindToTab(this); | 227 this.onBeforeNavigate = ext.tabs.onBeforeNavigate._bindToTab(this); |
199 this.onCompleted = ext.tabs.onCompleted._bindToTab(this); | 228 this.onCompleted = ext.tabs.onCompleted._bindToTab(this); |
200 this.onActivated = ext.tabs.onActivated._bindToTab(this); | 229 this.onActivated = ext.tabs.onActivated._bindToTab(this); |
201 this.onRemoved = ext.tabs.onRemoved._bindToTab(this); | 230 this.onRemoved = ext.tabs.onRemoved._bindToTab(this); |
202 }; | 231 }; |
203 Tab.prototype = { | 232 Tab.prototype = { |
204 close: function() { | 233 close: function() |
| 234 { |
205 chrome.tabs.remove(this._id); | 235 chrome.tabs.remove(this._id); |
206 }, | 236 }, |
207 activate: function() { | 237 activate: function() |
| 238 { |
208 chrome.tabs.update(this._id, {selected: true}); | 239 chrome.tabs.update(this._id, {selected: true}); |
209 }, | 240 }, |
210 sendMessage: function(message, responseCallback) { | 241 sendMessage: function(message, responseCallback) |
| 242 { |
211 chrome.tabs.sendMessage(this._id, message, responseCallback); | 243 chrome.tabs.sendMessage(this._id, message, responseCallback); |
212 } | 244 } |
213 }; | 245 }; |
214 | 246 |
215 TabMap = function() { | 247 TabMap = function() |
| 248 { |
216 this._map = {}; | 249 this._map = {}; |
217 this.delete = this.delete.bind(this); | 250 this.delete = this.delete.bind(this); |
218 }; | 251 }; |
219 TabMap.prototype = { | 252 TabMap.prototype = { |
220 get: function(tab) { | 253 get: function(tab) |
| 254 { |
221 return (this._map[tab._id] || {}).value; | 255 return (this._map[tab._id] || {}).value; |
222 }, | 256 }, |
223 set: function(tab, value) { | 257 set: function(tab, value) |
| 258 { |
224 if (!(tab._id in this._map)) | 259 if (!(tab._id in this._map)) |
225 tab.onRemoved.addListener(this.delete); | 260 tab.onRemoved.addListener(this.delete); |
226 | 261 |
227 this._map[tab._id] = {tab: tab, value: value}; | 262 this._map[tab._id] = {tab: tab, value: value}; |
228 }, | 263 }, |
229 has: function(tab) { | 264 has: function(tab) |
| 265 { |
230 return tab._id in this._map; | 266 return tab._id in this._map; |
231 }, | 267 }, |
232 clear: function() { | 268 clear: function() |
| 269 { |
233 for (var id in this._map) | 270 for (var id in this._map) |
234 this.delete(this._map[id].tab); | 271 this.delete(this._map[id].tab); |
235 } | 272 } |
236 }; | 273 }; |
237 TabMap.prototype["delete"] = function(tab) { | 274 TabMap.prototype["delete"] = function(tab) |
| 275 { |
238 delete this._map[tab._id]; | 276 delete this._map[tab._id]; |
239 tab.onRemoved.removeListener(this.delete); | 277 tab.onRemoved.removeListener(this.delete); |
240 }; | 278 }; |
241 | 279 |
242 | 280 |
243 /* Windows */ | 281 /* Windows */ |
244 | 282 |
245 Window = function(win) { | 283 Window = function(win) |
| 284 { |
246 this._id = win.id; | 285 this._id = win.id; |
247 this.visible = win.status != "minimized"; | 286 this.visible = win.status != "minimized"; |
248 }; | 287 }; |
249 Window.prototype = { | 288 Window.prototype = { |
250 getAllTabs: function(callback) { | 289 getAllTabs: function(callback) |
251 chrome.tabs.query({windowId: this._id}, function(tabs) { | 290 { |
252 callback(tabs.map(function(tab) { | 291 chrome.tabs.query({windowId: this._id}, function(tabs) |
253 return new Tab(tab); | 292 { |
254 })); | 293 callback(tabs.map(function(tab) { return new Tab(tab); })); |
255 }); | 294 }); |
256 }, | 295 }, |
257 getActiveTab: function(callback) { | 296 getActiveTab: function(callback) |
258 chrome.tabs.query({windowId: this._id, active: true}, function(tabs) { | 297 { |
| 298 chrome.tabs.query({windowId: this._id, active: true}, function(tabs) |
| 299 { |
259 callback(new Tab(tabs[0])); | 300 callback(new Tab(tabs[0])); |
260 }); | 301 }); |
261 }, | 302 }, |
262 openTab: function(url, callback) { | 303 openTab: function(url, callback) |
| 304 { |
263 var props = {windowId: this._id, url: url}; | 305 var props = {windowId: this._id, url: url}; |
264 | 306 |
265 if (!callback) | 307 if (!callback) |
266 chrome.tabs.create(props); | 308 chrome.tabs.create(props); |
267 else | 309 else |
268 chrome.tabs.create(props, function(tab) { | 310 chrome.tabs.create(props, function(tab) |
| 311 { |
269 callback(new Tab(tab)); | 312 callback(new Tab(tab)); |
270 }); | 313 }); |
271 } | 314 } |
272 }; | 315 }; |
273 | 316 |
274 | 317 |
275 /* API */ | 318 /* API */ |
276 | 319 |
277 ext.windows = { | 320 ext.windows = { |
278 getAll: function(callback) { | 321 getAll: function(callback) |
279 chrome.windows.getAll(function(windows) { | 322 { |
280 callback(windows.map(function(win) { | 323 chrome.windows.getAll(function(windows) |
| 324 { |
| 325 callback(windows.map(function(win) |
| 326 { |
281 return new Window(win); | 327 return new Window(win); |
282 })); | 328 })); |
283 }); | 329 }); |
284 }, | 330 }, |
285 getLastFocused: function(callback) { | 331 getLastFocused: function(callback) |
286 chrome.windows.getLastFocused(function(win) { | 332 { |
| 333 chrome.windows.getLastFocused(function(win) |
| 334 { |
287 callback(new Window(win)); | 335 callback(new Window(win)); |
288 }); | 336 }); |
289 } | 337 } |
290 }; | 338 }; |
291 | 339 |
292 ext.tabs = { | 340 ext.tabs = { |
293 onBeforeNavigate: new BeforeNavigateTabEventTarget(), | 341 onBeforeNavigate: new BeforeNavigateTabEventTarget(), |
294 onCompleted: new CompletedTabEventTarget(), | 342 onCompleted: new CompletedTabEventTarget(), |
295 onActivated: new ActivatedTabEventTarget(), | 343 onActivated: new ActivatedTabEventTarget(), |
296 onRemoved: new RemovedTabEventTarget() | 344 onRemoved: new RemovedTabEventTarget() |
297 }; | 345 }; |
298 | 346 |
299 ext.webRequest = { | 347 ext.webRequest = { |
300 onBeforeRequest: new BeforeRequestEventTarget(), | 348 onBeforeRequest: new BeforeRequestEventTarget(), |
301 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged | 349 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged |
302 }; | 350 }; |
303 })(); | 351 })(); |
LEFT | RIGHT |