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

Delta Between Two Patch Sets: ext/background.js

Issue 29511561: Issue 5347 - Check for contextMenus API support (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Use consistent style for feature detection Created Aug. 15, 2017, 4:38 p.m.
Right Patch Set: Remove unnecessary checks Created Aug. 16, 2017, 7:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 78 this.contextMenus = new ContextMenus(this);
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);
81 }; 79 };
82 Page.prototype = { 80 Page.prototype = {
83 get url() 81 get url()
84 { 82 {
85 // usually our Page objects are created from Chrome's Tab objects, which 83 // usually our Page objects are created from Chrome's Tab objects, which
86 // provide the url. So we can return the url given in the constructor. 84 // provide the url. So we can return the url given in the constructor.
87 if (this._url) 85 if (this._url)
88 return this._url; 86 return this._url;
89 87
90 // but sometimes we only have the tab id when we create a Page object. 88 // 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
413 411
414 if ("color" in badge) 412 if ("color" in badge)
415 this._addChange("badgeColor", badge.color); 413 this._addChange("badgeColor", badge.color);
416 } 414 }
417 } 415 }
418 }; 416 };
419 417
420 418
421 /* Context menus */ 419 /* Context menus */
422 420
423 let ContextMenus = null; 421 let contextMenuItems = new ext.PageMap();
424 422 let contextMenuUpdating = false;
425 if ("contextMenus" in chrome) 423
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
426 { 424 let updateContextMenu = () =>
427 let contextMenuItems = new ext.PageMap(); 425 {
428 let contextMenuUpdating = false; 426 // Firefox for Android does not support context menus.
429 427 // https://bugzilla.mozilla.org/show_bug.cgi?id=1269062
430 let updateContextMenu = () => 428 if (!("contextMenus" in chrome) || contextMenuUpdating)
431 { 429 return;
432 if (contextMenuUpdating) 430
433 return; 431 contextMenuUpdating = true;
434 432
435 contextMenuUpdating = true; 433 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
436 434 {
437 chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => 435 chrome.contextMenus.removeAll(() =>
438 { 436 {
439 chrome.contextMenus.removeAll(() => 437 contextMenuUpdating = false;
440 { 438
441 contextMenuUpdating = false; 439 if (tabs.length == 0)
442 440 return;
443 if (tabs.length == 0) 441
444 return; 442 let items = contextMenuItems.get({id: tabs[0].id});
445 443
446 let items = contextMenuItems.get({id: tabs[0].id}); 444 if (!items)
447 445 return;
448 if (!items) 446
449 return; 447 items.forEach(item =>
450 448 {
451 items.forEach(item => 449 chrome.contextMenus.create({
452 { 450 title: item.title,
453 chrome.contextMenus.create({ 451 contexts: item.contexts,
454 title: item.title, 452 onclick(info, tab)
455 contexts: item.contexts, 453 {
456 onclick(info, tab) 454 item.onclick(new Page(tab));
457 { 455 }
458 item.onclick(new Page(tab));
459 }
460 });
461 }); 456 });
462 }); 457 });
463 }); 458 });
464 }; 459 });
465 460 };
466 ContextMenus = function(page) 461
467 { 462 let ContextMenus = function(page)
468 this._page = page; 463 {
469 }; 464 this._page = page;
470 ContextMenus.prototype = { 465 };
471 create(item) 466 ContextMenus.prototype = {
472 { 467 create(item)
473 let items = contextMenuItems.get(this._page); 468 {
474 if (!items) 469 let items = contextMenuItems.get(this._page);
475 contextMenuItems.set(this._page, items = []); 470 if (!items)
476 471 contextMenuItems.set(this._page, items = []);
477 items.push(item); 472
478 updateContextMenu(); 473 items.push(item);
479 }, 474 updateContextMenu();
480 remove(item) 475 },
481 { 476 remove(item)
482 let items = contextMenuItems.get(this._page); 477 {
483 if (items) 478 let items = contextMenuItems.get(this._page);
484 { 479 if (items)
485 let index = items.indexOf(item); 480 {
486 if (index != -1) 481 let index = items.indexOf(item);
487 { 482 if (index != -1)
488 items.splice(index, 1); 483 {
489 updateContextMenu(); 484 items.splice(index, 1);
490 } 485 updateContextMenu();
491 } 486 }
492 } 487 }
493 }; 488 }
494 489 };
495 chrome.tabs.onActivated.addListener(updateContextMenu); 490
496 491 chrome.tabs.onActivated.addListener(updateContextMenu);
497 chrome.windows.onFocusChanged.addListener(windowId => 492
498 { 493 chrome.windows.onFocusChanged.addListener(windowId =>
499 if (windowId != chrome.windows.WINDOW_ID_NONE) 494 {
500 updateContextMenu(); 495 if (windowId != chrome.windows.WINDOW_ID_NONE)
501 }); 496 updateContextMenu();
502 } 497 });
503 498
504 499
505 /* Web requests */ 500 /* Web requests */
506 501
507 let framesOfTabs = new Map(); 502 let framesOfTabs = new Map();
508 503
509 ext.getFrame = (tabId, frameId) => 504 ext.getFrame = (tabId, frameId) =>
510 { 505 {
511 let frames = framesOfTabs.get(tabId); 506 let frames = framesOfTabs.get(tabId);
512 return frames && frames.get(frameId); 507 return frames && frames.get(frameId);
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
746 ext.windows = { 741 ext.windows = {
747 create(createData, callback) 742 create(createData, callback)
748 { 743 {
749 chrome.windows.create(createData, createdWindow => 744 chrome.windows.create(createData, createdWindow =>
750 { 745 {
751 afterTabLoaded(callback)(createdWindow.tabs[0]); 746 afterTabLoaded(callback)(createdWindow.tabs[0]);
752 }); 747 });
753 } 748 }
754 }; 749 };
755 }()); 750 }());
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld