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