本文介绍了从Firefox的上下文菜单中获取menuitem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个Firefox扩展,它将菜单项添加到上下文菜单中,现在我想隐藏项目,除非点击的上下文是文本。
由于某种原因oncontextmenu事件是
没有触发(屏幕上没有显示上下文打开)。

我尝试改变,但仍然没有警告被解雇,任何人有一个想法,我可能在这里做错了吗?谢谢!

 <?xml version =1.0?> 
< overlay xmlns =http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul>
< script>
函数contextClicked(){
alert('context open')
var saveToFileItem = document.getElementById(saveToFile);
}
< / script>

< menupopup id =contentAreaContextMenuoncontextmenu =contextClicked();>
< script type =application / javascriptsrc =chrome://ffext/content/overlay.js/>
< / menupopup>
< / overlay>


解决方案

不要这样做 - c $ c> onfoo 事件属性只能有一个事件处理程序。如果成功,您可能会覆盖由Firefox定义的默认上下文菜单处理程序。您应该使用 - 即 popupshowing popupshown 。上下文菜单初始化代码通常附加到 popupshowinging 事件上。所以你应该把类似于这个的代码放到你的 code> overlay.js file:
$ b

  //在窗口加载之前,不要试图做任何事情
window.addEventListener(load,function()
{
function contextClicked(event)
{
alert( 'context $'
var saveToFileItem = document.getElementById(saveToFile);
}

//添加上下文作为事件监听器添加到上下文菜单
var contextMenu = document.getElementById(contentAreaContextMenu);
contextMenu.addEventListener(popupshowing,contextClicked,false);
},false);

请注意,这段代码定义了 contextClicked 函数 - 这是我通常会推荐的方法。如果您在全局范围内定义事物,则可能会遇到与现有Firefox代码或其他扩展名的名称冲突 - 所有这些代码都在相同的名称空间中运行。通过在匿名函数中声明变量和函数,您完全可以避免这个问题。


I've written a Firefox extension which appends a menuitem onto the context menu and now i want to hide the item unless context of the click is on text.
For some reason the oncontextmenu event is never triggered ('context opened' is not shown on screen).
I've tried changing the to but still no alert being fired, anyone got an idea what i might be doing wrong here? thanks!

<?xml version="1.0"?>
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <script>
    function contextClicked(){
        alert('context opened')
        var saveToFileItem = document.getElementById("saveToFile");
    }
    </script>

    <menupopup id="contentAreaContextMenu" oncontextmenu="contextClicked();">
        <script type="application/javascript" src="chrome://ffext/content/overlay.js"/>
        <menuitem id="saveToFile" label="Save to File" oncommand="ffext.run();"/>
    </menupopup>
</overlay>
解决方案

Don't do it like this - with onfoo event properties there can be only one event handler. If you succeed you will likely override the default context menu handler defined by Firefox. You should be using addEventListener instead which allows adding multiple event listeners for the same event. And of course, you are using the wrong event. The contextmenu event fires on the element that the user clicks on, not on the context menu. XUL processes this event by opening the context menu, contentAreaContextMenu in this case. The context menu then gets its own events - namely popupshowing and popupshown. Context menu initialization code usually attaches to the popupshowing event.

So you should put code similar to this into your overlay.js file:

// Do not try to do anything before the window loads
window.addEventListener("load", function()
{
   function contextClicked(event)
   {
     alert('context opened')
     var saveToFileItem = document.getElementById("saveToFile");
   }

   // Add contextClicked as event listener to the context menu
   var contextMenu = document.getElementById("contentAreaContextMenu");
   contextMenu.addEventListener("popupshowing", contextClicked, false);
}, false);

Note that this code defines contextClicked inside a function - this is an approach that I would generally recommend. If you define things globally you can run into a name conflict with existing Firefox code or other extensions - all of them run their code in the same namespace. By declaring your variables and functions inside an anonymous function you completely avoid this issue.

这篇关于从Firefox的上下文菜单中获取menuitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:49
查看更多