[英]How can I make a link in firefox extension panel, to open in browser window / tab?
So I am developing my first firefox addon and I have a simple panel which contains some content and a link. 因此,我正在开发我的第一个firefox插件,我有一个简单的面板,其中包含一些内容和链接。 When I click on the link, link is opened in the panel itself. 当我单击链接时,链接会在面板本身中打开。 I want to be able to open this link in firefox tab or window. 我希望能够在Firefox标签或窗口中打开此链接。 I tried searching mozdev documentation but didn't find any solution. 我尝试搜索mozdev文档,但未找到任何解决方案。
You can either add a target
attribute to your links (as _blank
if you want to open a new tab every time); 您可以将target
属性添加到链接中(如果您想每次打开一个新选项卡,则为_blank
); or intercept any click
you do in the panel's document, and then send a message to your add-on code, to open a tab. 或拦截您在面板文档中所做的任何click
,然后向您的附加代码发送一条消息,以打开一个选项卡。 Something like: 就像是:
document.documentElement.addEventListener("click", event => {
let a = event.target.closest("a");
if (a && a.href) {
// replace `self` with `addon` if it's a trusted document and
// it's not a `contentScriptFile`
self.port.emit("open-link", a.href);
}
});
Then in your index.js
or main.js
, you'll have something like: 然后在index.js
或main.js
,您将看到类似以下内容的内容:
const tabs = require("sdk/tabs");
let panel = Panel({ /* ... your panel ... */ });
panel.port.on("open-link", uri => tabs.open(uri));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.