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

Side by Side Diff: ext/background.js

Issue 29511561: Issue 5347 - Check for contextMenus API support (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Use consistent style for feature detection Created Aug. 15, 2017, 4:38 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 | « no previous file | lib/filterComposer.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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 }; 68 };
69 69
70 /* Pages */ 70 /* Pages */
71 71
72 let Page = ext.Page = function(tab) 72 let Page = ext.Page = function(tab)
73 { 73 {
74 this.id = tab.id; 74 this.id = tab.id;
75 this._url = tab.url && new URL(tab.url); 75 this._url = tab.url && new URL(tab.url);
76 76
77 this.browserAction = new BrowserAction(tab.id); 77 this.browserAction = new BrowserAction(tab.id);
78 this.contextMenus = new ContextMenus(this); 78
79 if ("contextMenus" in chrome)
Wladimir Palant 2017/08/16 11:04:00 Please add a comment explaining which platform thi
Manish Jethani 2017/08/16 12:09:45 Done.
80 this.contextMenus = new ContextMenus(this);
79 }; 81 };
80 Page.prototype = { 82 Page.prototype = {
81 get url() 83 get url()
82 { 84 {
83 // usually our Page objects are created from Chrome's Tab objects, which 85 // usually our Page objects are created from Chrome's Tab objects, which
84 // provide the url. So we can return the url given in the constructor. 86 // provide the url. So we can return the url given in the constructor.
85 if (this._url) 87 if (this._url)
86 return this._url; 88 return this._url;
87 89
88 // but sometimes we only have the tab id when we create a Page object. 90 // but sometimes we only have the tab id when we create a Page object.
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 413
412 if ("color" in badge) 414 if ("color" in badge)
413 this._addChange("badgeColor", badge.color); 415 this._addChange("badgeColor", badge.color);
414 } 416 }
415 } 417 }
416 }; 418 };
417 419
418 420
419 /* Context menus */ 421 /* Context menus */
420 422
421 let contextMenuItems = new ext.PageMap(); 423 let ContextMenus = null;
422 let contextMenuUpdating = false;
423 424
424 let updateContextMenu = () => 425 if ("contextMenus" in chrome)
Wladimir Palant 2017/08/16 11:04:00 If I see it correctly, the point of this check is
Manish Jethani 2017/08/16 12:09:45 Right, I was going for optimizing for mobile, but
Wladimir Palant 2017/08/16 13:21:56 Respectfully, I disagree. Limited resources or not
Manish Jethani 2017/08/16 19:39:09 Yeah, I meant optimizing for mobile in general, no
425 { 426 {
426 if (contextMenuUpdating) 427 let contextMenuItems = new ext.PageMap();
427 return; 428 let contextMenuUpdating = false;
428 429
429 contextMenuUpdating = true; 430 let updateContextMenu = () =>
431 {
432 if (contextMenuUpdating)
433 return;
430 434
431 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => 435 contextMenuUpdating = true;
432 { 436
433 chrome.contextMenus.removeAll(() => 437 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
434 { 438 {
435 contextMenuUpdating = false; 439 chrome.contextMenus.removeAll(() =>
440 {
441 contextMenuUpdating = false;
436 442
437 if (tabs.length == 0) 443 if (tabs.length == 0)
438 return; 444 return;
439 445
440 let items = contextMenuItems.get({id: tabs[0].id}); 446 let items = contextMenuItems.get({id: tabs[0].id});
441 447
442 if (!items) 448 if (!items)
443 return; 449 return;
444 450
445 items.forEach(item => 451 items.forEach(item =>
446 { 452 {
447 chrome.contextMenus.create({ 453 chrome.contextMenus.create({
448 title: item.title, 454 title: item.title,
449 contexts: item.contexts, 455 contexts: item.contexts,
450 onclick(info, tab) 456 onclick(info, tab)
451 { 457 {
452 item.onclick(new Page(tab)); 458 item.onclick(new Page(tab));
453 } 459 }
460 });
454 }); 461 });
455 }); 462 });
456 }); 463 });
457 }); 464 };
458 };
459 465
460 let ContextMenus = function(page) 466 ContextMenus = function(page)
461 {
462 this._page = page;
463 };
464 ContextMenus.prototype = {
465 create(item)
466 { 467 {
467 let items = contextMenuItems.get(this._page); 468 this._page = page;
468 if (!items) 469 };
469 contextMenuItems.set(this._page, items = []); 470 ContextMenus.prototype = {
471 create(item)
472 {
473 let items = contextMenuItems.get(this._page);
474 if (!items)
475 contextMenuItems.set(this._page, items = []);
470 476
471 items.push(item); 477 items.push(item);
472 updateContextMenu(); 478 updateContextMenu();
473 }, 479 },
474 remove(item) 480 remove(item)
475 {
476 let items = contextMenuItems.get(this._page);
477 if (items)
478 { 481 {
479 let index = items.indexOf(item); 482 let items = contextMenuItems.get(this._page);
480 if (index != -1) 483 if (items)
481 { 484 {
482 items.splice(index, 1); 485 let index = items.indexOf(item);
483 updateContextMenu(); 486 if (index != -1)
487 {
488 items.splice(index, 1);
489 updateContextMenu();
490 }
484 } 491 }
485 } 492 }
486 } 493 };
487 };
488 494
489 chrome.tabs.onActivated.addListener(updateContextMenu); 495 chrome.tabs.onActivated.addListener(updateContextMenu);
490 496
491 chrome.windows.onFocusChanged.addListener(windowId => 497 chrome.windows.onFocusChanged.addListener(windowId =>
492 { 498 {
493 if (windowId != chrome.windows.WINDOW_ID_NONE) 499 if (windowId != chrome.windows.WINDOW_ID_NONE)
494 updateContextMenu(); 500 updateContextMenu();
495 }); 501 });
502 }
496 503
497 504
498 /* Web requests */ 505 /* Web requests */
499 506
500 let framesOfTabs = new Map(); 507 let framesOfTabs = new Map();
501 508
502 ext.getFrame = (tabId, frameId) => 509 ext.getFrame = (tabId, frameId) =>
503 { 510 {
504 let frames = framesOfTabs.get(tabId); 511 let frames = framesOfTabs.get(tabId);
505 return frames && frames.get(frameId); 512 return frames && frames.get(frameId);
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 ext.windows = { 746 ext.windows = {
740 create(createData, callback) 747 create(createData, callback)
741 { 748 {
742 chrome.windows.create(createData, createdWindow => 749 chrome.windows.create(createData, createdWindow =>
743 { 750 {
744 afterTabLoaded(callback)(createdWindow.tabs[0]); 751 afterTabLoaded(callback)(createdWindow.tabs[0]);
745 }); 752 });
746 } 753 }
747 }; 754 };
748 }()); 755 }());
OLDNEW
« no previous file with comments | « no previous file | lib/filterComposer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld