| 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-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() | 18 (function() |
| 19 { | 19 { |
| 20 /* Pages */ | 20 /* Pages */ |
| 21 | 21 |
| 22 var Page = ext.Page = function(tab) | 22 var Page = ext.Page = function(tab) |
| 23 { | 23 { |
| 24 this._id = tab.id; | 24 this.id = tab.id; |
| 25 this._url = tab.url && new URL(tab.url); | 25 this._url = tab.url && new URL(tab.url); |
| 26 | 26 |
| 27 this.browserAction = new BrowserAction(tab.id); | 27 this.browserAction = new BrowserAction(tab.id); |
| 28 this.contextMenus = new ContextMenus(this); | 28 this.contextMenus = new ContextMenus(this); |
| 29 }; | 29 }; |
| 30 Page.prototype = { | 30 Page.prototype = { |
| 31 get url() | 31 get url() |
| 32 { | 32 { |
| 33 // usually our Page objects are created from Chrome's Tab objects, which | 33 // usually our Page objects are created from Chrome's Tab objects, which |
| 34 // provide the url. So we can return the url given in the constructor. | 34 // provide the url. So we can return the url given in the constructor. |
| 35 if (this._url) | 35 if (this._url) |
| 36 return this._url; | 36 return this._url; |
| 37 | 37 |
| 38 // but sometimes we only have the tab id when we create a Page object. | 38 // but sometimes we only have the tab id when we create a Page object. |
| 39 // In that case we get the url from top frame of the tab, recorded by | 39 // In that case we get the url from top frame of the tab, recorded by |
| 40 // the onBeforeRequest handler. | 40 // the onBeforeRequest handler. |
| 41 var frames = framesOfTabs[this._id]; | 41 var frames = framesOfTabs[this.id]; |
| 42 if (frames) | 42 if (frames) |
| 43 { | 43 { |
| 44 var frame = frames[0]; | 44 var frame = frames[0]; |
| 45 if (frame) | 45 if (frame) |
| 46 return frame.url; | 46 return frame.url; |
| 47 } | 47 } |
| 48 }, | 48 }, |
| 49 sendMessage: function(message, responseCallback) | 49 sendMessage: function(message, responseCallback) |
| 50 { | 50 { |
| 51 chrome.tabs.sendMessage(this._id, message, responseCallback); | 51 chrome.tabs.sendMessage(this.id, message, responseCallback); |
| 52 } | 52 } |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 ext._getPage = function(id) | 55 ext.getPage = function(id) |
| 56 { | 56 { |
| 57 return new Page({id: parseInt(id, 10)}); | 57 return new Page({id: parseInt(id, 10)}); |
| 58 }; | 58 }; |
| 59 | |
| 60 function afterTabLoaded(callback) | |
| 61 { | |
| 62 return function(openedTab) | |
| 63 { | |
| 64 var onUpdated = function(tabId, changeInfo, tab) | |
| 65 { | |
| 66 if (tabId == openedTab.id && changeInfo.status == "complete") | |
| 67 { | |
| 68 chrome.tabs.onUpdated.removeListener(onUpdated); | |
| 69 callback(new Page(openedTab)); | |
| 70 } | |
| 71 }; | |
| 72 chrome.tabs.onUpdated.addListener(onUpdated); | |
| 73 }; | |
| 74 } | |
| 59 | 75 |
| 60 ext.pages = { | 76 ext.pages = { |
| 61 open: function(url, callback) | 77 open: function(url, callback) |
| 62 { | 78 { |
| 63 if (callback) | 79 chrome.tabs.create({url: url}, callback && afterTabLoaded(callback)); |
| 64 { | |
| 65 chrome.tabs.create({url: url}, function(openedTab) | |
| 66 { | |
| 67 var onUpdated = function(tabId, changeInfo, tab) | |
| 68 { | |
| 69 if (tabId == openedTab.id && changeInfo.status == "complete") | |
| 70 { | |
| 71 chrome.tabs.onUpdated.removeListener(onUpdated); | |
| 72 callback(new Page(tab)); | |
| 73 } | |
| 74 }; | |
| 75 chrome.tabs.onUpdated.addListener(onUpdated); | |
| 76 }); | |
| 77 } | |
| 78 else | |
| 79 chrome.tabs.create({url: url}); | |
| 80 }, | 80 }, |
| 81 query: function(info, callback) | 81 query: function(info, callback) |
| 82 { | 82 { |
| 83 var rawInfo = {}; | 83 var rawInfo = {}; |
| 84 for (var property in info) | 84 for (var property in info) |
| 85 { | 85 { |
| 86 switch (property) | 86 switch (property) |
| 87 { | 87 { |
| 88 case "active": | 88 case "active": |
| 89 case "lastFocusedWindow": | 89 case "lastFocusedWindow": |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 url: details.url | 132 url: details.url |
| 133 }) | 133 }) |
| 134 ); | 134 ); |
| 135 } | 135 } |
| 136 }); | 136 }); |
| 137 } | 137 } |
| 138 }); | 138 }); |
| 139 | 139 |
| 140 function forgetTab(tabId) | 140 function forgetTab(tabId) |
| 141 { | 141 { |
| 142 ext.pages.onRemoved._dispatch(tabId); | 142 ext.pages.onRemoved._dispatch(tabId); |
|
Sebastian Noack
2016/02/10 12:26:56
I guess we should dispatch the event with a Page o
kzar
2016/02/10 14:20:16
I wasn't sure how well it would work using a Page
| |
| 143 | 143 |
| 144 ext._removeFromAllPageMaps(tabId); | 144 ext._removeFromAllPageMaps(tabId); |
| 145 delete framesOfTabs[tabId]; | 145 delete framesOfTabs[tabId]; |
| 146 } | 146 } |
| 147 | 147 |
| 148 chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId) | 148 chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId) |
| 149 { | 149 { |
| 150 forgetTab(removedTabId); | 150 forgetTab(removedTabId); |
| 151 }); | 151 }); |
| 152 | 152 |
| 153 chrome.tabs.onRemoved.addListener(forgetTab); | 153 chrome.tabs.onRemoved.addListener(forgetTab); |
| 154 | 154 |
| 155 chrome.tabs.onActivated.addListener(details => | 155 chrome.tabs.onActivated.addListener(function(details) |
| 156 { | 156 { |
| 157 ext.pages.onActivated._dispatch(new Page({id: details.tabId})); | 157 ext.pages.onActivated._dispatch(new Page({id: details.tabId})); |
| 158 }); | 158 }); |
| 159 | 159 |
| 160 | 160 |
| 161 /* Browser actions */ | 161 /* Browser actions */ |
| 162 | 162 |
| 163 var BrowserAction = function(tabId) | 163 var BrowserAction = function(tabId) |
| 164 { | 164 { |
| 165 this._tabId = tabId; | 165 this._tabId = tabId; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 | 269 |
| 270 chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) | 270 chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) |
| 271 { | 271 { |
| 272 chrome.contextMenus.removeAll(function() | 272 chrome.contextMenus.removeAll(function() |
| 273 { | 273 { |
| 274 contextMenuUpdating = false; | 274 contextMenuUpdating = false; |
| 275 | 275 |
| 276 if (tabs.length == 0) | 276 if (tabs.length == 0) |
| 277 return; | 277 return; |
| 278 | 278 |
| 279 var items = contextMenuItems.get({_id: tabs[0].id}); | 279 var items = contextMenuItems.get({id: tabs[0].id}); |
| 280 | 280 |
| 281 if (!items) | 281 if (!items) |
| 282 return; | 282 return; |
| 283 | 283 |
| 284 items.forEach(function(item) | 284 items.forEach(function(item) |
| 285 { | 285 { |
| 286 chrome.contextMenus.create({ | 286 chrome.contextMenus.create({ |
| 287 title: item.title, | 287 title: item.title, |
| 288 contexts: item.contexts, | 288 contexts: item.contexts, |
| 289 onclick: function(info, tab) | 289 onclick: function(info, tab) |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 585 else | 585 else |
| 586 { | 586 { |
| 587 ext.pages.open(optionsUrl, callback); | 587 ext.pages.open(optionsUrl, callback); |
| 588 } | 588 } |
| 589 }); | 589 }); |
| 590 }); | 590 }); |
| 591 }; | 591 }; |
| 592 | 592 |
| 593 /* Windows */ | 593 /* Windows */ |
| 594 ext.windows = { | 594 ext.windows = { |
| 595 create: (createData, callback) => | 595 create: function(createData, callback) |
|
Sebastian Noack
2016/02/10 12:26:56
Nit: I'm not sure if using an arrow function is ap
kzar
2016/02/10 14:20:16
Done.
| |
| 596 { | 596 { |
| 597 if (callback) | 597 chrome.windows.create(createData, function(createdWindow) |
|
Sebastian Noack
2016/02/10 12:26:57
The logic here is duplicated from ext.tabs.open. W
kzar
2016/02/10 14:20:16
Done.
| |
| 598 chrome.windows.create(createData, createdWindow => | 598 { |
| 599 { | 599 afterTabLoaded(callback)(createdWindow.tabs[0]); |
| 600 let createdTab = createdWindow.tabs[0]; | 600 }); |
| 601 var onUpdated = (tabId, changeInfo, tab) => | |
| 602 { | |
| 603 if (tabId == createdTab.id && changeInfo.status == "complete") | |
| 604 { | |
| 605 chrome.tabs.onUpdated.removeListener(onUpdated); | |
| 606 callback(new Page(createdTab)); | |
| 607 } | |
| 608 }; | |
| 609 chrome.tabs.onUpdated.addListener(onUpdated); | |
| 610 }); | |
| 611 else | |
| 612 chrome.windows.create(createData); | |
| 613 } | 601 } |
| 614 }; | 602 }; |
| 615 })(); | 603 })(); |
| LEFT | RIGHT |