Auto width widget

2019-09-17 16:46发布

问题:

I'm writing a small add-on to Firefofx, in which I use widgets. I wonder how to set auto-width which adapts to content. For example I want my widget to display numbers, which will have a different length. I can set my widget width but how do I know how much my number takes pixels. Is there a way to get the width, choosing automatically? Here is some of my code: My widget (my add-on uses other widgets),

//ROBOTS META
var robots_meta = widgets.Widget({
 id: "robots_meta",
 label: "Displays information about robots meta",
 content: "Meta-robots n/a",
 width: 100
});

Here is function that update content of my widget:

function meta_robots_checker(tab) {
  var meta_robots_w = robots_meta.getView(tab.window);
  meta_robots_w.content = 'Meta-robots n/a';
  worker = tab.attach({
    contentScriptFile: data.url("js/meta-checker.js")
   });
worker.port.emit("check_noindex");
worker.port.on('content', function(message) {    
        message = message.toLowerCase();
        message = message.replace('nofollow', '<span style="color:red">nofollow</span>');
        message = message.replace('noindex', '<span style="color:red">noindex</span>');
        meta_robots_w.content = message;
       //HERE MY WIDGET WIDTH SHOULD BE CHANGED
    });
}

回答1:

you have to dynamically control the width, the widget is a panel. not a dynamically change width element like a label.

to get the widget panel element use "Element Selector" addon with "Dom Inspector".

See this topic here: Avoid panel to autoHide in Firefox extension