自动将toolbarbutton安装到firefox nav-bar,而不是使用insertItem [英] Automatically install toolbarbutton to firefox nav-bar, not working with insertItem

查看:116
本文介绍了自动将toolbarbutton安装到firefox nav-bar,而不是使用insertItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我读了几乎所有可以在MDN,stackoverflow等找到的东西,这一切似乎已经过时和/或不工作。这是问题:

我想在安装时自动将扩展程序的toolbarbutton放置在导航栏上,类似于Chrome。用户在这之后做了什么取决于他们,但是如果你删除了按钮(带有这个特定的扩展名),那么你可能只需删除扩展名,因为没有按钮就没用了。 ANYWAY ...



这样做似乎有两种方法。你可以将它追加到currentSet并使其保持,如下所示:

  var currentset = document.getElementById(nav -bar)currentSet。 
currentset = currentset +,MYBUTTON_ID;
document.getElementById(nav-bar)。setAttribute(currentset,currentset);
document.getElementById(nav-bar)。currentSet = currentset;
document.persist(nav-bar,currentset);

或者,您可以像这样使用insertItem:

  var toolbar = document.getElementById(nav-bar); 
toolbar.insertItem(MYBUTTON_ID,null);
toolbar.setAttribute(currentset,toolbar.currentSet);
document.persist(toolbar.id,currentset);

现在,如果我使用第一种方法,它可以工作,但它会完全擦除导航栏上的所有内容酒吧出于某种原因。 currentSet似乎没有其他的导航栏按钮,当它去覆盖它,所以我只是结束了默认的导航栏加上我的图标。擦掉所有的用户的其他按钮是没有好处的...

第二个选项不会消除其他选项,但无论出于何种原因,insertItem这样做的方式根本不起作用。我的按钮从来没有出现在导航栏,期间。



有什么想法? 好吧,就像我想的那样,我的代码没有问题,问题和我描述的一样。



当扩展名是加载和安装按钮的脚本被执行,这是做得太早。在执行时,currentSet只包含默认按钮。没有其他的扩展按钮被加载。因此,如果您修改currentSet并保存(保留)它,则会清除所有其他按钮。



解决方案(对于我)强制我的安装脚本等待更长的时间。我发现,一旦页面被加载,所有其他按钮有足够的时间出现。所以,我只是这样做:
$ b $ pre $ function installButton(){

var navbar = document.getElementById ( NAV-条);
var newset = navbar.currentSet +,MYBUTTONID;
navbar.currentSet = newset;
navbar.setAttribute(currentset,newset);
document.persist(nav-bar,currentset);


$ b window.addEventListener(load,function(){installButton();},false);


So I've read just about everything I could find on MDN, stackoverflow, etc, and it all seems to be outdated and/or not working. Here is the issue:

I want to automatically put my extension's "toolbarbutton" on the nav-bar when it is installed, similar to Chrome. What the user does after that point is up to them, although if you remove the button (with this particular extension) you might as well just remove the extension as it's useless without the button. ANYWAY...

There seems to be two methods for doing this. You can append it to the "currentSet" and make it persist, like this:

var currentset = document.getElementById("nav-bar").currentSet;
currentset=currentset + ",MYBUTTON_ID";
document.getElementById("nav-bar").setAttribute("currentset",currentset);
document.getElementById("nav-bar").currentSet = currentset;
document.persist("nav-bar","currentset");

Or, you can use "insertItem" like this:

var toolbar = document.getElementById("nav-bar");
toolbar.insertItem("MYBUTTON_ID", null);  
toolbar.setAttribute("currentset", toolbar.currentSet);  
document.persist(toolbar.id, "currentset");

Now, if I use the first method, it works, but it completely erases everything else on the nav bar for some reason. "currentSet" doesn't seem to have the other nav-bar buttons on it when it goes to overwrite it, and so I just end up with the default nav-bar plus my icon. Wiping out all of a user's other buttons is no good...

The second option doesn't wipe out the other options, but for whatever reason, the "insertItem" way of doing it doesn't work at all. My button never appears in the nav-bar, period.

Any ideas?

解决方案

Alright, like I thought, there was nothing wrong with my code, and the problem was exactly as I described.

When the extension is loaded and the script to install the button is executed, it's done too early. At the time of execution, "currentSet" only contains the default buttons. No other extension buttons are loaded yet. As a result, if you modify the currentSet and save (persist) it, you wipe out all other buttons.

The solution (for me) was to force my "install" script to wait longer. I found that once the page had been loaded, all of the other buttons had enough time to appear. So, I simply did this:

function installButton() {

    var navbar = document.getElementById("nav-bar");
    var newset = navbar.currentSet + ",MYBUTTONID";
    navbar.currentSet = newset;
    navbar.setAttribute("currentset", newset );
    document.persist("nav-bar", "currentset");

}

window.addEventListener("load", function () { installButton(); }, false);

这篇关于自动将toolbarbutton安装到firefox nav-bar,而不是使用insertItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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