简体   繁体   English

我的firefox插件代码正在破坏导航栏

[英]My firefox addon code is destroying navigation bar

I want to add a toolbar button before the firefox search container in my addon. 我想在我的插件中的firefox搜索容器之前添加一个工具栏按钮。 But it is completely clearing my navigation bar. 但它完全清除了我的导航栏。 在此输入图像描述 I suspect the offending code is due to an empty array or something but i cant be certain. 我怀疑违规代码是由于一个空数组或其他东西,但我无法确定。

//insert before search container
if(navBar && navBar.currentSet.indexOf("mybutton-id")== -1 )//navBar exist and our button doesnt
{
    var arrayCurrentSet= navBar.currentSet.split(',');
    var arrayFinalSet= [];//empty at first
    if(arrayCurrentSet.indexOf("search-container") != -1)//if search-container exists in current set
    {
        // check item by item in current set
        var i= null;
        while(i=arrayCurrentSet.shift() != undefined)
        {
            if(i == "search-container")//"search-container" found !!
            {
                /*insert our button after it but only if our button does not already exist*/
                if(arrayFinalSet.indexOf("mybutton-id") == -1) arrayFinalSet.push("mybutton-id");
            }
            arrayFinalSet.push(i); 
            dump("arrayFinalSet "+ i);
        }
    }
    else //damn search-container doesnt exist
    {
        arrayFinalSet= arrayCurrentSet;
        arrayFinalSet.push("mybutton-id");//add our button to the end of whatever is available in nav bar
    }
    //set new navBar
    navBar.currentSet= arrayFinalSet.join(',');  
}

The full code is available 完整的代码可用

https://builder.addons.mozilla.org/addon/1052494/latest/ https://builder.addons.mozilla.org/addon/1052494/latest/

http://jsfiddle.net/CQ4wA/ http://jsfiddle.net/CQ4wA/

I'm not too sure why the navigation bar has been removed, but I think it would be better to approach this from a different angle. 我不太清楚为什么导航栏已被移除,但我认为从不同的角度来看这个更好。 Rather than messing around with an array of strings, try using DOM methods instead. 不要乱用字符串数组,而是尝试使用DOM方法。

eg 例如

var sC=navBar.querySelector("#search-container");
navBar.insertBefore(btn, sC);

The code you have here seems to work - but the toolbar needs to find your button somehow. 你在这里的代码似乎工作 - 但工具栏需要以某种方式找到你的按钮。 Your current code doesn't even insert the button into the document, meaning that the toolbar has no chance to find it by its ID. 您当前的代码甚至没有将按钮插入到文档中,这意味着工具栏无法通过其ID找到它。 It should be in the toolbar palette palette however, the palette also determines which buttons the user can choose from when customizing the toolbar. 它应该在工具栏调色板中,但是,调色板还确定用户在自定义工具栏时可以选择的按钮。 So you probably want to do something like this first: 所以你可能想先做这样的事情:

var toolbox = navBar.toolbox;
toolbox.palette.appendChild(btn);

You might also want to simplify your code: 您可能还想简化代码:

var arrayCurrentSet = navBar.currentSet.split(',');
var insertionPoint = arrayCurrentSet.indexOf("search-container");
if (insertionPoint >= 0)
  arrayCurrentSet.splice(insertionPoint, 0, "mybutton-id");
else
  arrayCurrentSet.push("mybutton-id");
navBar.currentSet = arrayCurrentSet.join(',');

And finally, you probably want to make the browser remember the current button set, it doesn't happen automatically: 最后,您可能希望让浏览器记住当前的按钮集,它不会自动发生:

document.persist(navBar.id, "currentset");

Note that the button that will be inserted into the toolbar is not the same as the button you added to the palette - the toolbar code clones the button, with one copy being left in the palette. 请注意,将插入工具栏的按钮与添加到调色板的按钮不同 - 工具栏代码克隆按钮,调色板中保留一个副本。 So event listeners added via addEventListener will sadly be lost. 因此,通过addEventListener添加的事件监听器将遗憾地丢失。 It is better to use a command attribute and insert a <command> element into the document that you will attach your listener to. 最好使用command属性并将<command>元素插入要将监听器附加到的文档中。

Note : in XUL you usually want the command and not the click event - unless you are really interested in mouse clicks only and want to ignore the button being triggered by keyboard or other means. 注意 :在XUL中你通常需要command而不是click事件 - 除非你真的只对鼠标点击感兴趣并且想要忽略键盘或其他方式触发的按钮。

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

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