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

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

Issue 5095280043098112: Issue 375 - Make context menus per page (Closed)
Patch Set: Created April 24, 2014, 1:22 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
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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; 25 this._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 }; 29 };
29 Page.prototype = { 30 Page.prototype = {
30 get url() 31 get url()
31 { 32 {
32 // 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
33 // 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.
34 if (this._url != null) 35 if (this._url != null)
35 return this._url; 36 return this._url;
36 37
37 // 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.
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 { 161 {
161 chrome.browserAction.setBadgeText({ 162 chrome.browserAction.setBadgeText({
162 tabId: this._tabId, 163 tabId: this._tabId,
163 text: badge.number.toString() 164 text: badge.number.toString()
164 }); 165 });
165 } 166 }
166 } 167 }
167 }; 168 };
168 169
169 170
171 /* Context menus */
172
173 var contextMenuItems = new ext.PageMap();
174 var contextMenuUpdating = false;
175
176 var updateContextMenu = function()
177 {
178 if (contextMenuUpdating)
179 return;
180
181 contextMenuUpdating = true;
182
183 chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs)
184 {
185 chrome.contextMenus.removeAll(function()
186 {
187 contextMenuUpdating = false;
188
189 if (tabs.length == 0)
190 return;
191
192 var items = contextMenuItems.get({_id: tabs[0].id});
193
194 if (!items)
195 return;
196
197 for (var i = 0; i < items.length; i++)
198 {
199 chrome.contextMenus.create({
200 title: items[i].title,
201 contexts: items[i].contexts,
202 onclick: items[i].onclick
203 });
204 }
205 });
206 });
207 };
208
209 var ContextMenus = function(page)
210 {
211 this._page = page;
212 };
213 ContextMenus.prototype = {
214 create: function(item)
215 {
216 var items = contextMenuItems.get(this._page);
217 if (!items)
218 contextMenuItems.set(this._page, items = []);
219
220 items.push(item);
221 updateContextMenu();
Wladimir Palant 2014/04/25 11:35:50 How many unnecessary updates will this trigger whe
Sebastian Noack 2014/04/25 11:48:03 This was considered and addressed by baling out in
222 },
223 removeAll: function()
224 {
225 contextMenuItems.delete(this._page);
226 updateContextMenu();
227 }
228 };
229
230 chrome.tabs.onActivated.addListener(updateContextMenu);
231
232 chrome.windows.onFocusChanged.addListener(function(windowId)
233 {
234 if (windowId != chrome.windows.WINDOW_ID_NONE)
235 updateContextMenu();
236 });
237
238
170 /* Web requests */ 239 /* Web requests */
171 240
172 var framesOfTabs = {__proto__: null}; 241 var framesOfTabs = {__proto__: null};
173 242
174 ext.getFrame = function(tabId, frameId) 243 ext.getFrame = function(tabId, frameId)
175 { 244 {
176 return (framesOfTabs[tabId] || {})[frameId]; 245 return (framesOfTabs[tabId] || {})[frameId];
177 }; 246 };
178 247
179 ext.webRequest = { 248 ext.webRequest = {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 catch (e) 322 catch (e)
254 { 323 {
255 // recent versions of Chrome cancel the request when an error occurs in 324 // recent versions of Chrome cancel the request when an error occurs in
256 // the onBeforeRequest listener. However in our case it is preferred, to 325 // the onBeforeRequest listener. However in our case it is preferred, to
257 // let potentially some ads through, rather than blocking legit requests. 326 // let potentially some ads through, rather than blocking legit requests.
258 console.error(e); 327 console.error(e);
259 } 328 }
260 }, {urls: ["<all_urls>"]}, ["blocking"]); 329 }, {urls: ["<all_urls>"]}, ["blocking"]);
261 330
262 331
263 /* Context menus */
264
265 var contextMenuItems = [];
266 var isContextMenuHidden = true;
267
268 ext.contextMenus = {
269 addMenuItem: function(title, contexts, onclick)
270 {
271 contextMenuItems.push({
272 title: title,
273 contexts: contexts,
274 onclick: function(info, tab)
275 {
276 onclick(info.srcUrl, new Page(tab));
277 }
278 });
279 this.showMenuItems();
280 },
281 removeMenuItems: function()
282 {
283 contextMenuItems = [];
284 this.hideMenuItems();
285 },
286 showMenuItems: function()
287 {
288 if (!isContextMenuHidden)
289 return;
290
291 chrome.contextMenus.removeAll(function()
292 {
293 for (var i = 0; i < contextMenuItems.length; i++)
294 {
295 var item = contextMenuItems[i];
296 chrome.contextMenus.create({
297 title: item.title,
298 contexts: item.contexts,
299 onclick: item.onclick
300 });
301 }
302 });
303 isContextMenuHidden = false;
304 },
305 hideMenuItems: function()
306 {
307 if (isContextMenuHidden)
308 return;
309
310 chrome.contextMenus.removeAll();
311 isContextMenuHidden = true;
312 }
313 };
314
315
316 /* Message passing */ 332 /* Message passing */
317 333
318 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse ) 334 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse )
319 { 335 {
320 var sender = { 336 var sender = {
321 page: new Page(rawSender.tab), 337 page: new Page(rawSender.tab),
322 frame: { 338 frame: {
323 url: rawSender.url, 339 url: rawSender.url,
324 get parent() 340 get parent()
325 { 341 {
(...skipping 14 matching lines...) Expand all
340 }; 356 };
341 357
342 return ext.onMessage._dispatch(message, sender, sendResponse); 358 return ext.onMessage._dispatch(message, sender, sendResponse);
343 }); 359 });
344 360
345 361
346 /* Storage */ 362 /* Storage */
347 363
348 ext.storage = localStorage; 364 ext.storage = localStorage;
349 })(); 365 })();
OLDNEW
« no previous file with comments | « background.js ('k') | safari/ext/background.js » ('j') | safari/ext/background.js » ('J')

Powered by Google App Engine
This is Rietveld