简体   繁体   English

Firefox插件按钮代码不起作用-Javascript

[英]Firefox Addon Button Code Not Working - Javascript

I am creating a Firefox extension that is designed to place a button automatically on the toolbar that, when clicked, will open a web page in a new tab. 我正在创建一个Firefox扩展程序,该扩展程序旨在在工具栏上自动放置一个按钮,当单击该按钮时,它将在新选项卡中打开网页。 I have used the code snippets from the Mozilla dev site but, when both are put together, only the button placement works. 我使用了Mozilla开发人员网站上的代码段,但是当将两者放在一起时,只有按钮放置有效。 The button does nothing when clicked. 单击该按钮不执行任何操作。

I don't know too much about javascript, so I have no idea what is going wrong here. 我对javascript不太了解,所以我不知道这里出了什么问题。 The entire extension has passed all Mozilla validation checks with no errors and no warnings. 整个扩展程序都通过了所有Mozilla验证检查,没有错误和警告。

Here is the code. 这是代码。 Any help you could offer would be greatly appreciated. 您能提供的任何帮助将不胜感激。

CustomButton = {
1: function installButton(toolbarId, id, afterId) {
    if (!document.getElementById(id)) {
        var toolbar = document.getElementById(toolbarId);

        // If no afterId is given, then append the item to the toolbar
        var before = null;
        if (afterId) {
            let elem = document.getElementById(afterId);
            if (elem && elem.parentNode == toolbar)
                before = elem.nextElementSibling;
        }

        toolbar.insertItem(id, before);
        toolbar.setAttribute("currentset", toolbar.currentSet);
        document.persist(toolbar.id, "currentset");
    }

if (firstRun) {
    installButton("nav-bar", "my-extension-navbar-button");
}
},

2: function () {

const url = "http://www.mysite.com/"

document
    .getElementById("content")

.webNavigation

.loadURI(url, 0, null, null, null)

}
}

I am not very sharp at this, which is why I am asking the question here instead of searching for other examples, which make no sense to me. 我对此并不十分敏锐,这就是为什么我在这里问这个问题而不是寻找其他对我来说没有意义的例子的原因。 If someone could show me how to modify this specific code to do what I need it to do, I would be grateful. 如果有人可以告诉我如何修改此特定代码以执行我需要做的事情,我将不胜感激。

This will work from FF 29, which will be released on April 2014 It adds an action-Button to the toolbar, once you click it it will load the http://www.example.com site in a new tab; 这将在FF 29(将于2014年4月发布)中生效。它将在工具栏上添加一个action-Button,一旦单击它,它将在新标签页中加载http://www.example.com网站; if you prefer to load a new window use require("sdk/windows") instead. 如果您希望加载新窗口,请使用require(“ sdk / windows”)代替。

Using the addon-sdk 使用addon-sdk

var ui = require("sdk/ui");
var action_button = ui.ActionButton({
    id: "my-button",
    label: "Open example page!",
    icon: "./icon.png",
    onClick: function() {
        require("sdk/tabs").open("http://www.example.com");
    }
});

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

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