在火狐插件 SDK 中设置上下文项位置



我正在编写一个扩展,涉及将项目添加到Firefox的上下文菜单中,但它附加到菜单的末尾,我找不到任何使用插件SDK(insertBefore/insertAfter)自定义项目位置的指针,我知道如何使用XUL完成此操作,但是我正在尝试使用插件SDK或某种插件SDK/XUL组合来做到这一点

这是与上下文菜单相关的代码片段

主.js

var pageMod = require("sdk/page-mod");
var data = require("sdk/self").data;
var tabs = require("sdk/tabs");
var cm = require("sdk/context-menu");
pageMod.PageMod({
  include: "*.youtube.com",
  contentScriptFile: data.url("page.js"),
  onAttach: function (worker) {
    worker.port.emit('link', data.url('convertbutton.png'));
}});
cm.Item({
  label: "Convert File",
  image: data.url("bighdconverterlogo128png.png"),
    context: [
    cm.URLContext(["*.youtube.com"]),
    cm.PageContext()
  ],
  contentScriptFile: data.url("menu.js"),
onMessage: function(vUrl){
        tabs.open(vUrl);
    }
});

数据/菜单.js

self.on("click", function(){
self.postMessage('http://hdconverter.co/' + 'c.php?url=' + window.location.href);
});

谢谢

我不知道 SDK,但对于非 SDK 插件来说很容易。 但是因为您没有样板设置,所以看起来会很长。 将此代码添加到底部的插件中:

var positionToInsertMenu = 0; //set the position you want it at here
var myLabelText = 'Convert File';
const {interfaces: Ci,utils: Cu} = Components;
Cu.import('resource://gre/modules/Services.jsm');
/*start - windowlistener*/
var windowListener = {
    //DO NOT EDIT HERE
    onOpenWindow: function (aXULWindow) {
        // Wait for the window to finish loading
        let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
        aDOMWindow.addEventListener("load", function () {
            aDOMWindow.removeEventListener("load", arguments.callee, false);
            windowListener.loadIntoWindow(aDOMWindow, aXULWindow);
        }, false);
    },
    onCloseWindow: function (aXULWindow) {},
    onWindowTitleChange: function (aXULWindow, aNewTitle) {},
    register: function () {
        // Load into any existing windows
        let XULWindows = Services.wm.getXULWindowEnumerator(null);
        while (XULWindows.hasMoreElements()) {
            let aXULWindow = XULWindows.getNext();
            let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
            windowListener.loadIntoWindow(aDOMWindow, aXULWindow);
        }
        // Listen to new windows
        Services.wm.addListener(windowListener);
    },
    unregister: function () {
        // Unload from any existing windows
        let XULWindows = Services.wm.getXULWindowEnumerator(null);
        while (XULWindows.hasMoreElements()) {
            let aXULWindow = XULWindows.getNext();
            let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
            windowListener.unloadFromWindow(aDOMWindow, aXULWindow);
        }
        //Stop listening so future added windows dont get this attached
        Services.wm.removeListener(windowListener);
    },
    //END - DO NOT EDIT HERE
    loadIntoWindow: function (aDOMWindow, aXULWindow) {
        if (!aDOMWindow) {
            return;
        }
        var contentAreaContextMenu = aDOMWindow.document.getElementById('contentAreaContextMenu');
        var myMenuItem;
        if (contentAreaContextMenu) {
            var menuItems = contentAreaContextMenu.querySelector('menuitem');
            [].forEach.call(menuItems, function(item) {
                if (item.getAttribute('label') == myLabelText) {
                    myMenuItem = item;
                }
            });
            contentAreaContextMenu.removeChild(myMenuItem);
            if (contentAreaContextMenu.childNodes.length >= positionToInsertMenu) { //position is greater then number of childNodes so append to end
                contentAreaContextMenu.appendChild(myMenuItem);
            } else {
                contentAreaContextMenu.insertBefore(myMenuItem, contentAreaContextMenu.childNodes[thePosition]);
            }
        }
    },
    unloadFromWindow: function (aDOMWindow, aXULWindow) {
        if (!aDOMWindow) {
            return;
        }
        var myMenuItem = aDOMWindow.document.getElementById('myMenuItem');
        if (myMenuItem) {
            myMenuItem.parentNode.removeChild(myMenuItem);
        }
    }
};
windowListener.register();

卸载插件时,添加以下内容:

windowListener.unregister();

我从模板中复制粘贴并快速修改了它。 为了使位置准确,您可能需要考虑哪些菜单项是隐藏的,哪些不是

最新更新