Link add-on SDK panel to toolbar button

前端 未结 3 1451
小鲜肉
小鲜肉 2021-01-28 09:29

The add-on SDK attaches panels to widgets as seen here. I would like to achieve the same effect using the add-on SDK with a toolbar button instead.

The

3条回答
  •  自闭症患者
    2021-01-28 10:04

    Panels don't have an onCommand method see MDN - Panels Article

    You can make your panel stylized, give it type arrow like panel.setAttribute('type', 'arrow') and then to attach to your button. I didn't give it type arrow below.

    Heres the working code. Copy paste to scratchpad and set Environment > Browser then run it.

    var doc = document; //to put this back in sdk do const doc = require('sdk/window/utils').getMostRecentBrowserWindow().document;
    
    var navBar = doc.getElementById('nav-bar')
    var btn = doc.createElement('toolbarbutton');
    btn.setAttribute('id', 'hylytit');
    btn.setAttribute('type', 'menu-button');
    btn.setAttribute('class', 'toolbarbutton-1');
    btn.setAttribute('image', ''); //i made this image blank because i dont have the image and im running from scratchpad
    btn.setAttribute('orient', 'horizontal');
    btn.setAttribute('label', 'Hylyt.it');
    
    var panel = doc.createElement('panel');
    
    btn.addEventListener('command', function(event) { //moved this below var panel = doc.createElement because panel needs to be crated before we write this function
      //if (event.button===0) btnClick();
      //console.log(TAG+'button clicked'); //what is TAG? its undefeined for me
        panel.openPopup(btn);
    }, false);
    
    panel.setAttribute('id', 'search-panel');
    /*
    panel.addEventListener('command', function(event) {
        console.log(TAG+'dropdown clicked'); //what is TAG? its undefeined for me
    }, false);
    */
    var label = doc.createElement('label');
    label.setAttribute('control', 'name');
    label.setAttribute('value', 'Article List');
    var textbox = doc.createElement('textbox');
    textbox.setAttribute('id', 'name');
    panel.appendChild(label);
    panel.appendChild(textbox);
    btn.appendChild(panel);
    navBar.appendChild(btn);
    

提交回复
热议问题