[Greasemonkey] unregisterMenuCommand problem

Prakash Kailasa pk-moz at kailasa.net
Mon Apr 25 18:23:11 EDT 2005


I am trying to add the GM_unregisterMenuCommand method to
Greasemonkey.  I thought it would be pretty simple (ha!), but I am
having a bit of a problem.

Here's the diff -uw output:

--- greasemonkey.js.orig        2005-04-21 23:42:42.000000000 -0400
+++ greasemonkey.js     2005-04-25 16:58:54.000000000 -0400
@@ -26,6 +26,11 @@
       function(commandName, commandCallback) {
         registerMenuCommand(docId, commandName, commandCallback);
       };
+
+    e.explicitOriginalTarget.defaultView.GM_unregisterMenuCommand =
+      function(commandName) {
+        unregisterMenuCommand(docId, commandName);
+      };
   }

   this.unloadDoc = function(e) {
@@ -93,6 +98,23 @@
     docMenuCommands[docId].insertBefore(menuItem, nextNode);
   }

+  function unregisterMenuCommand(docId, commandName) {
+    var currentItems = docMenuCommands[docId].childNodes;
+    var menuItem;
+
+    var i = 0;
+    while (i < currentItems.length) {
+      if (commandName.toLowerCase() == currentItems[i].getAttribute('label').toLowerCase()) {
+        menuItem = currentItems[i];
+        break;
+      }
+      i++;
+    }
+    if (menuItem) {
+      var removed = docMenuCommands[docId].removeChild(menuItem);
+    }
+  }
+

When y user script starts, it registers one of the two commands based on a boolean flag. The handler for the registered command acts like a toggle switch (when the menu item is selected it unregisters itself and registers the other command).

However after the handler is called, the 'User Script Commands' menu is inoperable. It is not disabled but selecting does not show the submenu for the registered commands.

Here's my user script code (simplified to show only relevant parts):

  // startup
  if (flag) {	// enabled
    // code to enable things
    ...
    GM_registerMenuCommand('Disable', disable_func);
  } else {
    // code to disable things
     ...
    GM_registerMenuCommand('Enable', enable_func);
  }

  // handlers
  function disable_func() {
    GM_unregisterMenuCommand('Disable');
    GM_registerMenuCommand('Enable', enable_func);
  }

  function enable_func() {
    GM_unregisterMenuCommand('Enable');
    GM_registerMenuCommand('Disable', disable_func);
  }

Please help me where I am going wrong.

Thanks,
/prakash

-- 
|You are in a maze of little twisting passages, all alike.             |


More information about the Greasemonkey mailing list