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

Side by Side Diff: chrome/ext/background.js

Issue 29336084: Issue 2426 - Open block.html as a popup window (Closed)
Patch Set: Reduce callback checking boilerplate Created Feb. 15, 2016, 3:45 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « blockElement.postload.js ('k') | ext/background.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 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":
90 rawInfo[property] = info[property]; 90 rawInfo[property] = info[property];
91 } 91 }
92 } 92 }
93 93
94 chrome.tabs.query(rawInfo, function(tabs) 94 chrome.tabs.query(rawInfo, function(tabs)
95 { 95 {
96 callback(tabs.map(function(tab) 96 callback(tabs.map(function(tab)
97 { 97 {
98 return new Page(tab); 98 return new Page(tab);
99 })); 99 }));
100 }); 100 });
101 }, 101 },
102 onLoading: new ext._EventTarget(), 102 onLoading: new ext._EventTarget(),
103 onActivated: new ext._EventTarget() 103 onActivated: new ext._EventTarget(),
104 onRemoved: new ext._EventTarget()
104 }; 105 };
105 106
106 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 107 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
107 { 108 {
108 if (changeInfo.status == "loading") 109 if (changeInfo.status == "loading")
109 ext.pages.onLoading._dispatch(new Page(tab)); 110 ext.pages.onLoading._dispatch(new Page(tab));
110 }); 111 });
111 112
112 chrome.webNavigation.onBeforeNavigate.addListener(function(details) 113 chrome.webNavigation.onBeforeNavigate.addListener(function(details)
113 { 114 {
(...skipping 17 matching lines...) Expand all
131 url: details.url 132 url: details.url
132 }) 133 })
133 ); 134 );
134 } 135 }
135 }); 136 });
136 } 137 }
137 }); 138 });
138 139
139 function forgetTab(tabId) 140 function forgetTab(tabId)
140 { 141 {
142 ext.pages.onRemoved._dispatch(tabId);
143
141 ext._removeFromAllPageMaps(tabId); 144 ext._removeFromAllPageMaps(tabId);
142 delete framesOfTabs[tabId]; 145 delete framesOfTabs[tabId];
143 } 146 }
144 147
145 chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId) 148 chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId)
146 { 149 {
147 forgetTab(removedTabId); 150 forgetTab(removedTabId);
148 }); 151 });
149 152
150 chrome.tabs.onRemoved.addListener(forgetTab); 153 chrome.tabs.onRemoved.addListener(forgetTab);
151 154
152 chrome.tabs.onActivated.addListener(details => 155 chrome.tabs.onActivated.addListener(function(details)
153 { 156 {
154 ext.pages.onActivated._dispatch(new Page({id: details.tabId})); 157 ext.pages.onActivated._dispatch(new Page({id: details.tabId}));
155 }); 158 });
156 159
157 160
158 /* Browser actions */ 161 /* Browser actions */
159 162
160 var BrowserAction = function(tabId) 163 var BrowserAction = function(tabId)
161 { 164 {
162 this._tabId = tabId; 165 this._tabId = tabId;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 269
267 chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) 270 chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs)
268 { 271 {
269 chrome.contextMenus.removeAll(function() 272 chrome.contextMenus.removeAll(function()
270 { 273 {
271 contextMenuUpdating = false; 274 contextMenuUpdating = false;
272 275
273 if (tabs.length == 0) 276 if (tabs.length == 0)
274 return; 277 return;
275 278
276 var items = contextMenuItems.get({_id: tabs[0].id}); 279 var items = contextMenuItems.get({id: tabs[0].id});
277 280
278 if (!items) 281 if (!items)
279 return; 282 return;
280 283
281 items.forEach(function(item) 284 items.forEach(function(item)
282 { 285 {
283 chrome.contextMenus.create({ 286 chrome.contextMenus.create({
284 title: item.title, 287 title: item.title,
285 contexts: item.contexts, 288 contexts: item.contexts,
286 onclick: function(info, tab) 289 onclick: function(info, tab)
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 if (callback) 582 if (callback)
580 callback(new Page(tab)); 583 callback(new Page(tab));
581 } 584 }
582 else 585 else
583 { 586 {
584 ext.pages.open(optionsUrl, callback); 587 ext.pages.open(optionsUrl, callback);
585 } 588 }
586 }); 589 });
587 }); 590 });
588 }; 591 };
592
593 /* Windows */
594 ext.windows = {
595 create: function(createData, callback)
596 {
597 chrome.windows.create(createData, function(createdWindow)
598 {
599 afterTabLoaded(callback)(createdWindow.tabs[0]);
600 });
601 }
602 };
589 })(); 603 })();
OLDNEW
« no previous file with comments | « blockElement.postload.js ('k') | ext/background.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld