Index: safari/background.js |
=================================================================== |
--- a/safari/background.js |
+++ b/safari/background.js |
@@ -523,12 +523,67 @@ |
ext.onMessage = new MessageEventTarget(safari.application); |
- // TODO: Implement context menu |
+ var contextMenu = []; |
+ var isContextMenuHidden = false; |
ext.contextMenus = { |
- create: function(title, contexts, onclick) {}, |
- removeAll: function(callback) {} |
+ addMenuItem: function(title, contexts, onclick) |
+ { |
+ contextMenu.push({ |
+ id: "block-element", |
Wladimir Palant
2014/01/17 15:36:55
So all menu items have the same ID?
How about usi
Thomas Greiner
2014/01/18 10:32:05
Done. According to Apple "The identifier must be u
|
+ title: title, |
+ item: null, |
+ contexts: contexts, |
+ onclick: onclick |
+ }); |
+ }, |
+ removeMenuItems: function() |
+ { |
+ contextMenu = []; |
+ }, |
+ showMenu: function() |
+ { |
+ isContextMenuHidden = false; |
+ }, |
+ hideMenu: function() |
+ { |
+ isContextMenuHidden = true; |
+ } |
}; |
+ // Create context menu items |
+ safari.application.addEventListener("contextmenu", function(event) |
+ { |
+ var context = event.userInfo.tagName.toLowerCase(); |
+ if (context == "img") |
+ context = "image"; |
Wladimir Palant
2014/01/17 15:36:55
Seeing that srcUrl can be null, it probably makes
Thomas Greiner
2014/01/18 10:32:05
Done.
|
+ |
+ for (var i = 0; i < contextMenu.length; i++) |
+ { |
+ if (isContextMenuHidden) |
+ return; |
Wladimir Palant
2014/01/17 15:36:55
Why is this check inside the loop? This belongs to
Thomas Greiner
2014/01/18 10:32:05
Done.
|
+ |
+ // Supported contexts are: all, audio, image, video |
+ var menuItem = contextMenu[i]; |
+ if (menuItem.contexts.indexOf("all") == -1 && menuItem.contexts.indexOf(context) == -1) |
+ return; |
Wladimir Palant
2014/01/17 15:36:55
This should be continue, not return - there can be
Thomas Greiner
2014/01/18 10:32:05
Done.
|
+ |
+ menuItem.item = event.contextMenu.appendContextMenuItem(menuItem.id, menuItem.title); |
Wladimir Palant
2014/01/17 15:36:55
I don't think that menuItem.item is being used, no
Thomas Greiner
2014/01/18 10:32:05
Done.
|
+ } |
+ }, 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 |