简体   繁体   English

使用 Firefox 插件 SDK 进行面板定位

[英]Panel positioning with the Firefox Add-on SDK

I am trying to make some headway positioning my panel in an Add-on make with the new SDK.我正在尝试使用新的 SDK 在附加组件中取得一些进展。

I see that the documentation shows only one way to control the position of a panel, and that is by passing an anchor when you show() it:我看到文档只显示了一种控制面板的 position 的方法,那就是在show()时传递一个锚点:

show(anchor)节目(主播)

Displays the panel.显示面板。 [ anchor: handle ] [锚:手柄]

A handle to a DOM node in a page to which the panel should appear to be anchored.页面中一个 DOM 节点的句柄,面板应该锚定到该节点。 If not given, the panel is centered inside the most recent browser window.如果没有给出,面板在最新的浏览器 window 中居中。 Note that it is not currently possible to anchor panels in this way using only the high level APIs.请注意,目前无法仅使用高级 API 以这种方式锚定面板。

Ideally, I'd like the anchor to be the widget that the panel appears anchored to when that widget is clicked, but the widget is not a DOM node in a page so I guess not...理想情况下,我希望锚点成为单击该小部件时面板显示为锚定的小部件,但该小部件不是DOM node in a page所以我猜不是......

I can probably work around this, but I can't even find a working example of how to anchor the panel to a DOM node.我可能可以解决这个问题,但我什至找不到如何将面板锚定到 DOM 节点的工作示例。 When I pass back a DOM node in a contentScript through port like this:当我通过这样的port传回contentScript中的 DOM 节点时:

lib/main.js lib/main.js

  var scraper = pageMod.PageMod({
    include: ['*'],
    contentScriptWhen: 'ready',
    contentScriptFile: [data.url('jquery-1.6.2.min.js'), data.url('scraper.js')],
    onAttach: function(worker){
      worker.port.on('pageLoaded', function(page){
        widget.panel.show(page.anchor);
      }); 
    } 

data/scraper.js数据/scraper.js

$('body').append('
  <div id="anchor-to-me" style="position:fixed; bottom: 0; right: 0;">.</div>
');

var anchor = $('#anchor-to-me').get();
self.port.emit('pageLoaded', { anchor : anchor  }); 

I get the following console error:我收到以下控制台错误:

error: An exception occurred.
Traceback (most recent call last):
  File "resource://jid1-wuvxefqtmptsnw-at-jetpack-addon-kit-lib/panel.js", line 147, in show
    let document = getWindow(anchor).document;
  File "resource://jid1-wuvxefqtmptsnw-at-jetpack-addon-kit-lib/panel.js", line 345, in getWindow
    let anchorWindow = anchor.ownerDocument.defaultView.top;
TypeError: anchor.ownerDocument is undefined

Any info to help me successfully anchor to a DOM element, or find some other way to position the panel would be great.任何可以帮助我成功锚定到 DOM 元素的信息,或者找到 position 面板的其他方式都会很棒。

Thanks.谢谢。

const panel = require("panel").Panel({
  width: 240,
  height: 320,
  contentURL: data.url("foo.html"),
  contentScriptFile: [data.url("jquery-1.4.4.min.js"),
                      data.url("panel.js")]
});

const widget = widgets.Widget({
  id: "some-id",
  label: "Some label",
  contentURL: data.url("icon.png"),
  //panel: panel,
  onClick: function(view) { /* cool stuff */ 
     view.panel = panel;
  }
});

Instead of using mainPanel.show() , use view.panel = mainPanel;而不是使用mainPanel.show() ,使用view.panel = mainPanel; . . It will solve the problem bcz I did the same.它将解决问题,因为我做了同样的事情。

When you define your widget, you can add a panel property that is the instance of your panel:当您定义您的小部件时,您可以添加一个面板属性,它是您的面板实例:

[ panel: Panel ] [面板:面板]

An optional panel to open when the user clicks on the widget.当用户单击小部件时打开的可选面板。 Note: If you also register a "click" listener, it will be called instead of the panel being opened.注意:如果您还注册了“点击”侦听器,它将被调用而不是打开面板。 However, you can show the panel from the listener by calling this.panel.show().但是,您可以通过调用 this.panel.show() 从侦听器中显示面板。

Here's an example ( unfortunately there isn't an example of this in the docs ):这是一个示例(不幸的是,文档中没有这样的示例):

const panel = require("panel").Panel({
  width: 240,
  height: 320,
  contentURL: data.url("foo.html"),
  contentScriptFile: [data.url("jquery-1.4.4.min.js"),
                      data.url("panel.js")]
});

const widget = widgets.Widget({
  id: "some-id",
  label: "Some label",
  contentURL: data.url("icon.png"),
  panel: panel,
  onClick: function() { /* cool stuff */ }
});

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

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