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

Unified Diff: safari/background.js

Issue 5589897452716032: Implemented ext.contextMenus for Safari (Closed)
Patch Set: Created Jan. 18, 2014, 10:29 a.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 | « options.js ('k') | safari/content.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: safari/background.js
===================================================================
--- a/safari/background.js
+++ b/safari/background.js
@@ -523,12 +523,70 @@
ext.onMessage = new MessageEventTarget(safari.application);
- // TODO: Implement context menu
+ var contextMenuItems = [];
+ var isContextMenuHidden = true;
ext.contextMenus = {
- create: function(title, contexts, onclick) {},
- removeAll: function(callback) {}
+ addMenuItem: function(title, contexts, onclick)
+ {
+ contextMenuItems.push({
+ id: String(contextMenuItems.length),
+ title: title,
+ contexts: contexts,
+ onclick: onclick
+ });
+ this.showMenuItems();
+ },
+ removeMenuItems: function()
+ {
+ contextMenuItems = [];
+ this.hideMenuItems();
+ },
+ showMenuItems: function()
+ {
+ isContextMenuHidden = false;
+ },
+ hideMenuItems: function()
+ {
+ isContextMenuHidden = true;
+ }
};
+ // Create context menu items
+ safari.application.addEventListener("contextmenu", function(event)
+ {
+ if (isContextMenuHidden)
+ return;
+
+ var context = event.userInfo.tagName.toLowerCase();
Wladimir Palant 2014/01/18 11:13:39 Nit: toLowerCase() is no longer necessary now that
+ if (context == "img")
+ context = "image";
+ if (!event.userInfo.srcUrl)
+ context = null;
+
+ for (var i = 0; i < contextMenuItems.length; i++)
+ {
+ // Supported contexts are: all, audio, image, video
+ var menuItem = contextMenuItems[i];
+ if (menuItem.contexts.indexOf("all") == -1 && menuItem.contexts.indexOf(context) == -1)
+ continue;
+
+ event.contextMenu.appendContextMenuItem(menuItem.id, menuItem.title);
+ }
+ }, false);
+
+ // Handle context menu item clicks
+ safari.application.addEventListener("command", function(event)
+ {
+ for (var i = 0; i < contextMenu.length; i++)
+ {
+ if (contextMenu[i].id == event.command)
+ {
+ contextMenu[i].onclick(event.userInfo.srcUrl, new Tab(safari.application.activeBrowserWindow.activeTab));
+ break;
+ }
+ }
+ }, false);
+
// Safari will load the bubble once, and then show it everytime the icon is
// clicked. While Chrome loads it everytime you click the icon. So in order to
// force the same behavior in Safari, we are going to reload the page of the
« no previous file with comments | « options.js ('k') | safari/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld