使用附加SDK的Firefox中的自定义上下文菜单? [英] Custom context menu in Firefox with Add-on SDK?

查看:70
本文介绍了使用附加SDK的Firefox中的自定义上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在显示的Firefox上下文菜单中添加一个菜单项 仅当用户右键单击特定的URL时.我有一个功能来测试网址. 我曾经通过订阅"popupshowing"事件来做到这一点,并且:

I wish to add a single menu item to the firefox context menu that shows up only if the user right-clicks a specific url. I have a function to test the url. I used to do this by subscribing to "popupshowing" event and:

var item = document.getElementById("custom-menu-id");
if (item) // show only for specific links
    item.hidden = gContextMenu.onLink && acceptableURL(gContextMenu.linkURL);

我现在正在尝试使用附加SDK,但是在那里我不再可以访问gContextMenu. 文档中的此代码段对我不起作用:

I'm trying now to use the Add-on SDK, but there I no longer have access to gContextMenu. This snippet from the documentation doesn't work for me:

var cm = require("sdk/context-menu");
cm.Item({
    label: "Copy name to clipboard",
    context: cm.URLContext("http://scholar.google*"), 
    contentScript: 'self.on("context", function(node) {return true; });'
});

在这里,我认为应该有可能获得诸如node.URL之类的东西并对其进行测试, 但这是行不通的.也许有人会建议如何从sdk中访问gContextMenu或如何从节点或其他内容中获取URL.

Here I'd think that it should be possible to get something like node.URL and test that, but it doesn't work. Maybe someone could suggest either how to get access to gContextMenu from the sdk or how to get URL from node or something else.

推荐答案

此代码仅应在右键单击指向 stackoverflow.com 的链接时显示菜单项:

This code should only show the menu item when right-clicking on links directed at stackoverflow.com:

在主模块 main.js 中:

exports.main = function() {
    require("sdk/context-menu").Item({
        label: "stack overflow link",
        context:  require("sdk/context-menu").SelectorContext("a[href]"), 
        contentScriptFile: require("sdk/self").data.url("check-node.js"),
        onMessage: function(msg){},
    });
};

在您的内容脚本(或内容脚本文件;在本例中为 check-node.js )中:

In your content script (or content script file; in this case, check-node.js):

self.on("click",function(node,data){
    self.postMessage("click");
});
self.on("context", function(node){
    if(node.href && node.href.match(/^https?:\/\/(\w+\.)*stackoverflow\.com(\/.*)?$/))return true;  //show in context menu if return value is true.
});

回复:您的示例代码.您具有URLContext,它确定菜单项显示在哪些页面上,并且此片段self.on("context", function(node) {return true; });使菜单项在满足URLContext条件时始终显示.使用SelectorContext代替.并如上所述测试node.href,仅当您希望显示菜单项时才返回true.

Re: Your sample code. You have URLContext which determines what pages your menu items show up on and this snippet self.on("context", function(node) {return true; }); causes the menu item to always show when URLContext conditions are met. Use SelectorContext instead. And test the node.href as shown above, returning true only if you want the menu item to show.

这篇关于使用附加SDK的Firefox中的自定义上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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