Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 } | 97 } |
98 }, | 98 }, |
99 sendMessage(message, responseCallback) | 99 sendMessage(message, responseCallback) |
100 { | 100 { |
101 browser.tabs.sendMessage(this.id, message, responseCallback); | 101 browser.tabs.sendMessage(this.id, message, responseCallback); |
102 } | 102 } |
103 }; | 103 }; |
104 | 104 |
105 ext.getPage = id => new Page({id: parseInt(id, 10)}); | 105 ext.getPage = id => new Page({id: parseInt(id, 10)}); |
106 | 106 |
107 function afterTabLoaded(callback) | |
108 { | |
109 return openedTab => | |
110 { | |
111 let onUpdated = (tabId, changeInfo, tab) => | |
112 { | |
113 if (tabId == openedTab.id && changeInfo.status == "complete") | |
114 { | |
115 browser.tabs.onUpdated.removeListener(onUpdated); | |
116 callback(new Page(openedTab)); | |
117 } | |
118 }; | |
119 browser.tabs.onUpdated.addListener(onUpdated); | |
120 }; | |
121 } | |
122 | |
123 ext.pages = { | 107 ext.pages = { |
124 onLoading: new ext._EventTarget(), | 108 onLoading: new ext._EventTarget(), |
125 onActivated: new ext._EventTarget(), | 109 onActivated: new ext._EventTarget(), |
126 onRemoved: new ext._EventTarget() | 110 onRemoved: new ext._EventTarget() |
127 }; | 111 }; |
128 | 112 |
129 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => | 113 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => |
130 { | 114 { |
131 if (changeInfo.status == "loading") | 115 if (changeInfo.status == "loading") |
132 ext.pages.onLoading._dispatch(new Page(tab)); | 116 ext.pages.onLoading._dispatch(new Page(tab)); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 return; | 236 return; |
253 } | 237 } |
254 } | 238 } |
255 | 239 |
256 updatePageFrameStructure(details.frameId, details.tabId, details.url, | 240 updatePageFrameStructure(details.frameId, details.tabId, details.url, |
257 details.parentFrameId); | 241 details.parentFrameId); |
258 }, | 242 }, |
259 {types: ["main_frame", "sub_frame"], urls: ["http://*/*", "https://*/*"]}, | 243 {types: ["main_frame", "sub_frame"], urls: ["http://*/*", "https://*/*"]}, |
260 ["responseHeaders"]); | 244 ["responseHeaders"]); |
261 | 245 |
246 browser.webNavigation.onBeforeNavigate.addListener(details => | |
247 { | |
248 // Requests can be made by about:blank frames before the frame's | |
249 // onCommitted event has fired, so we update the frame structure | |
250 // for those now. | |
251 if (details.url == "about:blank") | |
252 { | |
253 updatePageFrameStructure(details.frameId, details.tabId, details.url, | |
254 details.parentFrameId); | |
255 } | |
256 }); | |
257 | |
262 browser.webNavigation.onCommitted.addListener(details => | 258 browser.webNavigation.onCommitted.addListener(details => |
263 { | 259 { |
264 // We have to update the frame structure for documents that weren't | 260 // We have to update the frame structure for documents that weren't |
265 // loaded over HTTP (including documents cached by Service Workers), | 261 // loaded over HTTP (including documents cached by Service Workers), |
266 // when the navigation occurs. However, we must be careful to not | 262 // when the navigation occurs. However, we must be careful to not |
267 // update the state of the same document twice, otherewise the number | 263 // update the state of the same document twice, otherewise the number |
268 // of any ads blocked already and any recorded sitekey could get lost. | 264 // of any ads blocked already and any recorded sitekey could get lost. |
269 let frame = ext.getFrame(details.tabId, details.frameId); | 265 let frame = ext.getFrame(details.tabId, details.frameId); |
270 if (!frame || frame.url.href != details.url) | 266 if (!frame || frame.url.href != details.url) |
271 { | 267 { |
(...skipping 26 matching lines...) Expand all Loading... | |
298 /* Browser actions */ | 294 /* Browser actions */ |
299 | 295 |
300 let BrowserAction = function(tabId) | 296 let BrowserAction = function(tabId) |
301 { | 297 { |
302 this._tabId = tabId; | 298 this._tabId = tabId; |
303 this._changes = null; | 299 this._changes = null; |
304 }; | 300 }; |
305 BrowserAction.prototype = { | 301 BrowserAction.prototype = { |
306 _applyChanges() | 302 _applyChanges() |
307 { | 303 { |
308 if ("iconPath" in this._changes) | 304 return Promise.all(Object.keys(this._changes).map(change => |
309 { | 305 { |
310 // Firefox for Android displays the browser action not as an icon but | 306 // Firefox for Android displays the browser action not as an icon but |
311 // as a menu item. There is no icon, but such an option may be added in | 307 // as a menu item. There is no icon, but such an option may be added |
312 // the future. | 308 // in the future. |
313 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 | 309 // https://bugzilla.mozilla.org/show_bug.cgi?id=1331746 |
314 if ("setIcon" in browser.browserAction) | 310 if (change == "iconPath" && "setIcon" in browser.browserAction) |
315 { | 311 { |
316 let path = { | 312 let path = { |
317 16: this._changes.iconPath.replace("$size", "16"), | 313 16: this._changes.iconPath.replace("$size", "16"), |
318 19: this._changes.iconPath.replace("$size", "19"), | 314 19: this._changes.iconPath.replace("$size", "19"), |
319 20: this._changes.iconPath.replace("$size", "20"), | 315 20: this._changes.iconPath.replace("$size", "20"), |
320 32: this._changes.iconPath.replace("$size", "32"), | 316 32: this._changes.iconPath.replace("$size", "32"), |
321 38: this._changes.iconPath.replace("$size", "38"), | 317 38: this._changes.iconPath.replace("$size", "38"), |
322 40: this._changes.iconPath.replace("$size", "40") | 318 40: this._changes.iconPath.replace("$size", "40") |
323 }; | 319 }; |
324 try | 320 try |
325 { | 321 { |
326 browser.browserAction.setIcon({tabId: this._tabId, path}, () => | 322 return browser.browserAction.setIcon({tabId: this._tabId, path}); |
327 { | |
328 browser.runtime.lastError; | |
329 }); | |
330 } | 323 } |
331 catch (e) | 324 catch (e) |
332 { | 325 { |
333 // Edge throws if passed icon sizes different than 19,20,38,40px. | 326 // Edge throws if passed icon sizes different than 19,20,38,40px. |
334 delete path[16]; | 327 delete path[16]; |
335 delete path[32]; | 328 delete path[32]; |
336 browser.browserAction.setIcon({tabId: this._tabId, path}, () => | 329 return browser.browserAction.setIcon({tabId: this._tabId, path}); |
337 { | |
338 browser.runtime.lastError; | |
339 }); | |
340 } | 330 } |
341 } | 331 } |
342 } | 332 |
343 | |
344 if ("badgeText" in this._changes) | |
345 { | |
346 // There is no badge on Firefox for Android; the browser action is | 333 // There is no badge on Firefox for Android; the browser action is |
347 // simply a menu item. | 334 // simply a menu item. |
348 if ("setBadgeText" in browser.browserAction) | 335 if (change == "badgeText" && "setBadgeText" in browser.browserAction) |
349 { | 336 return browser.browserAction.setBadgeText({ |
350 browser.browserAction.setBadgeText({ | |
351 tabId: this._tabId, | 337 tabId: this._tabId, |
352 text: this._changes.badgeText | 338 text: this._changes.badgeText |
353 }); | 339 }); |
354 } | 340 |
355 } | |
356 | |
357 if ("badgeColor" in this._changes) | |
358 { | |
359 // There is no badge on Firefox for Android; the browser action is | 341 // There is no badge on Firefox for Android; the browser action is |
360 // simply a menu item. | 342 // simply a menu item. |
361 if ("setBadgeBackgroundColor" in browser.browserAction) | 343 if (change == "badgeColor" && |
362 { | 344 "setBadgeBackgroundColor" in browser.browserAction) |
363 browser.browserAction.setBadgeBackgroundColor({ | 345 return browser.browserAction.setBadgeBackgroundColor({ |
364 tabId: this._tabId, | 346 tabId: this._tabId, |
365 color: this._changes.badgeColor | 347 color: this._changes.badgeColor |
366 }); | 348 }); |
349 })); | |
350 }, | |
351 _addChange(name, value) | |
352 { | |
353 let onReplaced = (addedTabId, removedTabId) => | |
Jon Sonesen
2018/07/10 23:01:07
Since I had trouble removing the check below this
| |
354 { | |
355 if (addedTabId == this._tabId) | |
356 { | |
357 browser.tabs.onReplaced.removeListener(onReplaced); | |
358 this._applyChanges().then(() => | |
359 { | |
360 this._changes = null; | |
361 }); | |
367 } | 362 } |
368 } | 363 }; |
369 | 364 if (!this._changes) |
370 this._changes = null; | 365 this._changes = {}; |
371 }, | 366 |
372 _queueChanges() | 367 this._changes[name] = value; |
373 { | 368 if (!browser.tabs.onReplaced.hasListener(onReplaced)) |
Jon Sonesen
2018/07/10 23:01:07
Replacing this check with the applyChanges boolean
| |
374 browser.tabs.get(this._tabId, () => | 369 { |
375 { | 370 this._applyChanges().then(() => |
376 // If the tab is prerendered, browser.tabs.get() sets | 371 { |
377 // browser.runtime.lastError and we have to delay our changes | 372 this._changes = null; |
378 // until the currently visible tab is replaced with the | 373 }).catch(() => |
Jon Sonesen
2018/07/10 23:01:08
I still think this is more readable than the callb
| |
379 // prerendered tab. Otherwise browser.browserAction.set* fails. | 374 { |
380 if (browser.runtime.lastError) | 375 // If the tab is prerendered, browser.browserAction.set* fails |
381 { | 376 // and we have to delay our changes until the currently visible tab |
382 let onReplaced = (addedTabId, removedTabId) => | 377 // is replaced with the prerendered tab. |
383 { | |
384 if (addedTabId == this._tabId) | |
385 { | |
386 browser.tabs.onReplaced.removeListener(onReplaced); | |
387 this._applyChanges(); | |
388 } | |
389 }; | |
390 browser.tabs.onReplaced.addListener(onReplaced); | 378 browser.tabs.onReplaced.addListener(onReplaced); |
391 } | 379 }); |
392 else | 380 } |
393 { | |
394 this._applyChanges(); | |
395 } | |
396 }); | |
397 }, | |
398 _addChange(name, value) | |
399 { | |
400 if (!this._changes) | |
401 { | |
402 this._changes = {}; | |
403 this._queueChanges(); | |
404 } | |
405 | |
406 this._changes[name] = value; | |
407 }, | 381 }, |
408 setIcon(path) | 382 setIcon(path) |
409 { | 383 { |
410 this._addChange("iconPath", path); | 384 this._addChange("iconPath", path); |
411 }, | 385 }, |
412 setBadge(badge) | 386 setBadge(badge) |
413 { | 387 { |
414 if (!badge) | 388 if (!badge) |
415 { | 389 { |
416 this._addChange("badgeText", ""); | 390 this._addChange("badgeText", ""); |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
576 return frames.get(0) || null; | 550 return frames.get(0) || null; |
577 } | 551 } |
578 }; | 552 }; |
579 } | 553 } |
580 | 554 |
581 return ext.onMessage._dispatch( | 555 return ext.onMessage._dispatch( |
582 message, sender, sendResponse | 556 message, sender, sendResponse |
583 ).includes(true); | 557 ).includes(true); |
584 }); | 558 }); |
585 } | 559 } |
LEFT | RIGHT |