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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/filterComposer.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ext/background.js
===================================================================
--- a/ext/background.js
+++ b/ext/background.js
@@ -70,17 +70,19 @@
/* Pages */
let Page = ext.Page = function(tab)
{
this.id = tab.id;
this._url = tab.url && new URL(tab.url);
this.browserAction = new BrowserAction(tab.id);
- this.contextMenus = new ContextMenus(this);
+
+ 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.
+ this.contextMenus = new ContextMenus(this);
};
Page.prototype = {
get url()
{
// usually our Page objects are created from Chrome's Tab objects, which
// provide the url. So we can return the url given in the constructor.
if (this._url)
return this._url;
@@ -413,91 +415,96 @@
this._addChange("badgeColor", badge.color);
}
}
};
/* Context menus */
- let contextMenuItems = new ext.PageMap();
- let contextMenuUpdating = false;
+ let ContextMenus = null;
- let updateContextMenu = () =>
+ 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
{
- if (contextMenuUpdating)
- return;
+ let contextMenuItems = new ext.PageMap();
+ let contextMenuUpdating = false;
- contextMenuUpdating = true;
-
- chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
+ let updateContextMenu = () =>
{
- chrome.contextMenus.removeAll(() =>
- {
- contextMenuUpdating = false;
+ if (contextMenuUpdating)
+ return;
+
+ contextMenuUpdating = true;
- if (tabs.length == 0)
- return;
+ chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs =>
+ {
+ chrome.contextMenus.removeAll(() =>
+ {
+ contextMenuUpdating = false;
- let items = contextMenuItems.get({id: tabs[0].id});
+ if (tabs.length == 0)
+ return;
- if (!items)
- return;
+ let items = contextMenuItems.get({id: tabs[0].id});
- items.forEach(item =>
- {
- chrome.contextMenus.create({
- title: item.title,
- contexts: item.contexts,
- onclick(info, tab)
- {
- item.onclick(new Page(tab));
- }
+ if (!items)
+ return;
+
+ items.forEach(item =>
+ {
+ chrome.contextMenus.create({
+ title: item.title,
+ contexts: item.contexts,
+ onclick(info, tab)
+ {
+ item.onclick(new Page(tab));
+ }
+ });
});
});
});
- });
- };
+ };
- let ContextMenus = function(page)
- {
- this._page = page;
- };
- ContextMenus.prototype = {
- create(item)
+ ContextMenus = function(page)
{
- let items = contextMenuItems.get(this._page);
- if (!items)
- contextMenuItems.set(this._page, items = []);
+ this._page = page;
+ };
+ ContextMenus.prototype = {
+ create(item)
+ {
+ let items = contextMenuItems.get(this._page);
+ if (!items)
+ contextMenuItems.set(this._page, items = []);
- items.push(item);
- updateContextMenu();
- },
- remove(item)
- {
- let items = contextMenuItems.get(this._page);
- if (items)
+ items.push(item);
+ updateContextMenu();
+ },
+ remove(item)
{
- let index = items.indexOf(item);
- if (index != -1)
+ let items = contextMenuItems.get(this._page);
+ if (items)
{
- items.splice(index, 1);
- updateContextMenu();
+ let index = items.indexOf(item);
+ if (index != -1)
+ {
+ items.splice(index, 1);
+ updateContextMenu();
+ }
}
}
- }
- };
+ };
- chrome.tabs.onActivated.addListener(updateContextMenu);
+ chrome.tabs.onActivated.addListener(updateContextMenu);
- chrome.windows.onFocusChanged.addListener(windowId =>
- {
- if (windowId != chrome.windows.WINDOW_ID_NONE)
- updateContextMenu();
- });
+ chrome.windows.onFocusChanged.addListener(windowId =>
+ {
+ if (windowId != chrome.windows.WINDOW_ID_NONE)
+ updateContextMenu();
+ });
+ }
/* Web requests */
let framesOfTabs = new Map();
ext.getFrame = (tabId, frameId) =>
{
« 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