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: Corrected width and height of clickhide popup Created Jan. 7, 2014, 5:37 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
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

Powered by Google App Engine
This is Rietveld