| Left: | ||
| Right: |
| 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 (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 | 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 } | |
| 75 | |
| 60 ext.pages = { | 76 ext.pages = { |
| 61 open: function(url, callback) | 77 open: function(url, callback) |
| 62 { | 78 { |
| 63 if (callback) | 79 if (callback) |
| 64 { | 80 chrome.tabs.create({url: url}, afterTabLoaded(callback)); |
|
Sebastian Noack
2016/02/15 14:53:44
Why didn't you go for |callback && afterTabLoaded(
kzar
2016/02/15 15:46:06
Done.
| |
| 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 | 81 else |
| 79 chrome.tabs.create({url: url}); | 82 chrome.tabs.create({url: url}); |
| 80 }, | 83 }, |
| 81 query: function(info, callback) | 84 query: function(info, callback) |
| 82 { | 85 { |
| 83 var rawInfo = {}; | 86 var rawInfo = {}; |
| 84 for (var property in info) | 87 for (var property in info) |
| 85 { | 88 { |
| 86 switch (property) | 89 switch (property) |
| 87 { | 90 { |
| 88 case "active": | 91 case "active": |
| 89 case "lastFocusedWindow": | 92 case "lastFocusedWindow": |
| 90 rawInfo[property] = info[property]; | 93 rawInfo[property] = info[property]; |
| 91 } | 94 } |
| 92 } | 95 } |
| 93 | 96 |
| 94 chrome.tabs.query(rawInfo, function(tabs) | 97 chrome.tabs.query(rawInfo, function(tabs) |
| 95 { | 98 { |
| 96 callback(tabs.map(function(tab) | 99 callback(tabs.map(function(tab) |
| 97 { | 100 { |
| 98 return new Page(tab); | 101 return new Page(tab); |
| 99 })); | 102 })); |
| 100 }); | 103 }); |
| 101 }, | 104 }, |
| 102 onLoading: new ext._EventTarget(), | 105 onLoading: new ext._EventTarget(), |
| 103 onActivated: new ext._EventTarget() | 106 onActivated: new ext._EventTarget(), |
| 107 onRemoved: new ext._EventTarget() | |
| 104 }; | 108 }; |
| 105 | 109 |
| 106 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) | 110 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) |
| 107 { | 111 { |
| 108 if (changeInfo.status == "loading") | 112 if (changeInfo.status == "loading") |
| 109 ext.pages.onLoading._dispatch(new Page(tab)); | 113 ext.pages.onLoading._dispatch(new Page(tab)); |
| 110 }); | 114 }); |
| 111 | 115 |
| 112 chrome.webNavigation.onBeforeNavigate.addListener(function(details) | 116 chrome.webNavigation.onBeforeNavigate.addListener(function(details) |
| 113 { | 117 { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 131 url: details.url | 135 url: details.url |
| 132 }) | 136 }) |
| 133 ); | 137 ); |
| 134 } | 138 } |
| 135 }); | 139 }); |
| 136 } | 140 } |
| 137 }); | 141 }); |
| 138 | 142 |
| 139 function forgetTab(tabId) | 143 function forgetTab(tabId) |
| 140 { | 144 { |
| 145 ext.pages.onRemoved._dispatch(tabId); | |
| 146 | |
| 141 ext._removeFromAllPageMaps(tabId); | 147 ext._removeFromAllPageMaps(tabId); |
| 142 delete framesOfTabs[tabId]; | 148 delete framesOfTabs[tabId]; |
| 143 } | 149 } |
| 144 | 150 |
| 145 chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId) | 151 chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId) |
| 146 { | 152 { |
| 147 forgetTab(removedTabId); | 153 forgetTab(removedTabId); |
| 148 }); | 154 }); |
| 149 | 155 |
| 150 chrome.tabs.onRemoved.addListener(forgetTab); | 156 chrome.tabs.onRemoved.addListener(forgetTab); |
| 151 | 157 |
| 152 chrome.tabs.onActivated.addListener(details => | 158 chrome.tabs.onActivated.addListener(function(details) |
| 153 { | 159 { |
| 154 ext.pages.onActivated._dispatch(new Page({id: details.tabId})); | 160 ext.pages.onActivated._dispatch(new Page({id: details.tabId})); |
| 155 }); | 161 }); |
| 156 | 162 |
| 157 | 163 |
| 158 /* Browser actions */ | 164 /* Browser actions */ |
| 159 | 165 |
| 160 var BrowserAction = function(tabId) | 166 var BrowserAction = function(tabId) |
| 161 { | 167 { |
| 162 this._tabId = tabId; | 168 this._tabId = tabId; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 | 272 |
| 267 chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) | 273 chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) |
| 268 { | 274 { |
| 269 chrome.contextMenus.removeAll(function() | 275 chrome.contextMenus.removeAll(function() |
| 270 { | 276 { |
| 271 contextMenuUpdating = false; | 277 contextMenuUpdating = false; |
| 272 | 278 |
| 273 if (tabs.length == 0) | 279 if (tabs.length == 0) |
| 274 return; | 280 return; |
| 275 | 281 |
| 276 var items = contextMenuItems.get({_id: tabs[0].id}); | 282 var items = contextMenuItems.get({id: tabs[0].id}); |
| 277 | 283 |
| 278 if (!items) | 284 if (!items) |
| 279 return; | 285 return; |
| 280 | 286 |
| 281 items.forEach(function(item) | 287 items.forEach(function(item) |
| 282 { | 288 { |
| 283 chrome.contextMenus.create({ | 289 chrome.contextMenus.create({ |
| 284 title: item.title, | 290 title: item.title, |
| 285 contexts: item.contexts, | 291 contexts: item.contexts, |
| 286 onclick: function(info, tab) | 292 onclick: function(info, tab) |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 579 if (callback) | 585 if (callback) |
| 580 callback(new Page(tab)); | 586 callback(new Page(tab)); |
| 581 } | 587 } |
| 582 else | 588 else |
| 583 { | 589 { |
| 584 ext.pages.open(optionsUrl, callback); | 590 ext.pages.open(optionsUrl, callback); |
| 585 } | 591 } |
| 586 }); | 592 }); |
| 587 }); | 593 }); |
| 588 }; | 594 }; |
| 595 | |
| 596 /* Windows */ | |
| 597 ext.windows = { | |
| 598 create: function(createData, callback) | |
| 599 { | |
| 600 if (callback) | |
|
Sebastian Noack
2016/02/15 14:53:44
It seems, creating a window without callback is a
kzar
2016/02/15 15:46:06
Done.
| |
| 601 chrome.windows.create(createData, function(createdWindow) | |
| 602 { | |
| 603 afterTabLoaded(callback)(createdWindow.tabs[0]); | |
| 604 }); | |
| 605 else | |
| 606 chrome.windows.create(createData); | |
| 607 } | |
| 608 }; | |
| 589 })(); | 609 })(); |
| OLD | NEW |