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. 7, 2014, 11:57 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
« include.postload.js ('K') | « options.js ('k') | no next file » | 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,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",
+ 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";
+
+ for (var i = 0; i < contextMenu.length; i++)
+ {
+ if (isContextMenuHidden)
+ return;
+
+ // Supported contexts are: all, audio, image, video
+ var menuItem = contextMenu[i];
+ if (menuItem.contexts.indexOf("all") == -1 && menuItem.contexts.indexOf(context) == -1)
+ return;
+
+ menuItem.item = 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
« include.postload.js ('K') | « options.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld