| 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 |