Arth
Arth

Reputation: 13110

Force context menu to appear for form inputs

I'm trying to develop a ff addon that allows a user to right-click on a form element and perform a task associated with it.

Unfortunately somebody decided that the context menu shouldn't appear for form inputs in ff and despite long discussions https://bugzilla.mozilla.org/show_bug.cgi?id=433168, they still don't appear for checkboxes, radios or selects.

I did find this: https://developer.mozilla.org/en-US/docs/Offering_a_context_menu_for_form_controls but I cannot think how to translate the code to work with the new add-on SDK. I tried dumping the javascript shown into a content script and also via the observer-service but to no avail.

I also cannot find the source for the recommended extension https://addons.mozilla.org/en-US/firefox/addon/form-control-context-menu/ which considering it was 'created specifically to demonstrate how to do this' is pretty frustrating.

This seems like very basic addon functionality, any help or links to easier documentation would be greatly appreciated.

** UPDATE **

I have added the following code in a file, required from main, that seems to do the trick.

var {WindowTracker} = require("window-utils");

var tracker = WindowTracker({
  onTrack: function(window){
    if (window.location.href == "chrome://browser/content/browser.xul") {    
      // This is a browser window, replace
      // window.nsContextMenu.prototype.setTarget function
      window.setTargetOriginal = window.nsContextMenu.prototype.setTarget;

      window.nsContextMenu.prototype.setTarget = function(aNode, aRangeParent, aRangeOffset) {
        window.setTargetOriginal.apply(this, arguments);
        this.shouldDisplay = true;
      };
    };
  }
, onUntrack: function(window) {
    if (window.location.href == "chrome://browser/content/browser.xul") {
      // In case we were called because the extension is uninstalled - restore
      // original window.nsContextMenu.prototype.setTarget function
      window.nsContextMenu.prototype.setTarget = window.setTargetOriginal;
    };
  }
});

Unfortunately this still does not bring up a context menu for disabled inputs, but this is not a show-stopper for me.

Many Thanks

Upvotes: 0

Views: 446

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57651

The important piece of code in this extension can be seen here. It is very simple - it replaces nsContextMenu.prototype.setTarget function in each browser window and makes sure that it sets shouldDisplay flag for form controls.

The only problem translating this to Add-on SDK is that the high-level modules don't give you direct access to browser windows. You have to use the deprecated window-utils module. Something like this should work:

var {WindowTracker} = require("sdk/deprecated/window-utils");
var tracker = WindowTracker({
  onTrack: function(window)
  {
    if (window.location.href == "chrome://browser/content/browser.xul")
    {
      // This is a browser window, replace
      // window.nsContextMenu.prototype.setTarget function
    }
  },

  onUntrack: function(window)
  {
    if (window.location.href == "chrome://browser/content/browser.xul")
    {
      // In case we were called because the extension is uninstalled - restore
      // original window.nsContextMenu.prototype.setTarget function
    }
  }
});

Note that WindowTracker is supposed to be replaced in some future SDK version. Also, for reference: nsContextMenu implementation

Upvotes: 2

Related Questions