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

时间:2014-02-15 15:43:57

标签: firefox-addon firefox-addon-sdk

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

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

main.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);
    }
});

数据/ menu.js

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

由于

1 个答案:

答案 0 :(得分:0)

我不知道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();

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