强制上下文菜单显示表单输入 [英] Force context menu to appear for form inputs

查看:151
本文介绍了强制上下文菜单显示表单输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发一个ff插件,允许用户右键单击表单元素并执行与之相关的任务。

不幸的是有人决定上下文菜单不应该出现在ff中的表单输入,尽管长时间的讨论 https:// bugzilla.mozilla.org/show_bug.cgi?id=433168 ,他们仍然不出现复选框,收音机或选择。



我找到了这个: https://developer.mozilla.org/zh-CN/docs/Offering_a_context_menu_for_form_controls但我想不出如何翻译代码来使用新的附加SDK。
我试图把JavaScript显示到内容脚本中,并通过观察者服务,但无济于事。



我也无法找到推荐的扩展程序的源代码 https://addons.mozilla.org/en-US/firefox/addon/form-control-context-menu/ 考虑它是专门创建演示如何做到这一点非常令人沮丧。
$ b $这似乎是非常基本的附加功能,任何帮助或链接到更简单的文档将不胜感激。



** UPDATE **



我在文件中添加了以下代码,
$ b

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

var tracker = WindowTracker({
onTrack:function(window){
if(window.location.href ==chrome://browser/content/browser.xul ){
//这是一个浏览器窗口,替换
// window.nsContextMenu.prototype.setTarget函数
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 ){
//如果因为扩展被卸载而被调用 - 恢复
//原始的window.nsContextMenu.prototype.setTarget函数
window.nsContextMenu.prototype.setTarget = window。 setTargetOriginal;
};
}
});

不幸的是,这仍然没有为禁用的输入提供上下文菜单,但是这不是一个show-对于我来说,这是一个重要的限制。



非常感谢 代码在这个扩展名可以看到此处。这非常简单 - 它将在每个浏览器窗口中替换 nsContextMenu.prototype.setTarget 函数,并确保它设置 shouldDisplay 标志为表单控件。

将其转换为附加SDK的唯一问题是高级模块不能直接访问浏览器窗口。您必须使用已弃用的 window-utils 模块。这样的事情应该工作:
$ b

  var {WindowTracker} = require(sdk / deprecated / window -utils); 
var tracker = WindowTracker({
onTrack:function(window)
{
if(window.location.href ==chrome://browser/content/browser.xul )
{
//这是一个浏览器窗口,替换
// window.nsContextMenu.prototype.setTarget函数
}
},

onUntrack:function(window)
{
if(window.location.href ==chrome://browser/content/browser.xul)
{
//如果因为扩展名被卸载而被调用 - 恢复
//原始的window.nsContextMenu.prototype.setTarget函数
}
}
});

请注意, WindowTracker 应该是<在未来的某个SDK版本中将被替换为。另外,供参考: nsContextMenu 执行


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

解决方案

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

这篇关于强制上下文菜单显示表单输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆