简体   繁体   English

仅在单击FireFox中的图像时才在上下文菜单中显示项目

[英]Show an item in context menu only when it clicked on an image in FireFox

[UPDATE] I may have explained the issue wrong: I know how to hide the menu element - but how to test if the clicked element is editable? [更新]我可能已经解释了错误的问题:我知道如何隐藏菜单元素-但是如何测试单击的元素是否可编辑? Chrome detects an editable element - how to achieve it in ff. Chrome检测到可编辑元素-如何在ff中实现它。

I'm trying to get my item in a the context menu to be shown only when it was clicked on an image 我试图将我的项目放在上下文菜单中,仅在单击图像时才显示

this is my emailpicture.js 这是我的emailpicture.js

code: 码:

function showHideItems(event)
{   
      var show = document.getElementById("emailImage");
      show.hidden = !(gContextMenu.onImage);
}

and the xul code: 和xul代码:

<?xml version="1.0"?>
<overlay id="emailpicture" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript" src="chrome://emailpicture/content/emailpicture.js"/>

<menupopup id="contentAreaContextMenu">
  <menuitem id="emailImage" label="Email This Image" onpopupshowing="showHideItems()" oncommand=""/>

</menupopup>


</overlay>

the result of this is showing on every object in the browser a bunch of items(might be all the items of FireFox) should i use the onpopupshowing event? 结果是在浏览器中的每个对象上显示了一堆项目(可能是FireFox的所有项目),我应该使用onpopupshowing事件吗? or another one? 或另一个? am i using it right? 我使用的对吗?

Thanks for the answers 感谢您的回答

If you look at the ContextMenu MDN page they have an example of just this. 如果您查看ContextMenu MDN页面,他们将提供一个示例。 The main issue with your current code is that you've put onpopupshowing on the <menuitem> when it should be on <menupopup> . 当前代码的主要问题是,您将onpopupshowing放在了<menupopup>上的<menuitem>上。 Furthermore, if you look at the MDN for document.popupNode it says you're encouraged to use .triggerNode instead. 此外,如果您查看document.popupNode的MDN,则建议您改用.triggerNode Additionally, looking at the popupshowing event reference we see it has a .target , but it isn't clear whether this is the <menupopup> (I assumed this below), the node that was clicked or something else (see comment in code). 另外,查看popupshowing事件引用,我们看到它有一个.target ,但是尚不清楚这是<menupopup> (我在下面假设了这一点),被单击的节点还是其他东西(请参见代码中的注释) 。

The following code makes use of the above and attaches the listener using .addEventListener to make sure there isn't a conflict with anything else that might use the onpopupshowing attribute. 以下代码利用了上述内容,并使用.addEventListener附加了侦听.addEventListener ,以确保与可能使用onpopupshowing属性的其他任何内容都不冲突。

In your emailpicture.js 在您的emailpicture.js中

// First define listener
function showHideItems(e) {
    var menupopup = e.target, // I believe `.target` is the <menupopup>, otherwise use `this`. 
        triggerNode = menupopup.triggerNode, // get node that was clicked to open context menu
        triggerIsImage = (triggerNode instanceof Components.interfaces.nsIImageLoadingContent && triggerNode.currentURI),
          // test if it is an image (as from MDN, I would've gone with `.nodeType` and `.nodeName` checks?)
        elmToHide = menupopup.getElementById('emailImage');
    if(elmToHide)
        elmToHide.hidden = !triggerIsImage;
}
// Then attach it to <menupopup>, remember the element has to exist before you can do this so you might want to wait for DOMContentLoaded
document.getElementById('contentAreaContextMenu')
    .addEventListener('popupshowing', showHideItems, false);

Then for the XUL don't include the attribute onpopupshowing . 然后对于XUL,不要包括onpopupshowing属性。

I'll point out that I've not tested this so if it isn't working, first check the even is fired then check .target is as I assumed (and possibly clarify MDN for others in future). 我要指出的是,我尚未对此进行测试,因此,如果它不起作用,请首先检查偶数是否被触发,然后再检查.target是否符合我的假设(并可能在将来为其他人澄清MDN)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM