简体   繁体   English

如何在firefox扩展面板中建立链接以在浏览器窗口/选项卡中打开?

[英]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.jsmain.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.

相关问题 Firefox / Chrome扩展程序:创建新标签后如何打开链接? - Firefox/Chrome extension: how to open link when new tab is created? 如何在Firefox扩展程序的新标签页中打开网址? - How do I open a url in a new tab in a firefox extension? Firefox侧边栏扩展链接已加载到新的浏览器选项卡中。 如何? - Firefox sidebar extension link loaded into a new browser tab. How-To? 如何指示 Applescript 打开带有链接的新 Firefox 窗口? - How do I instruct Applescript to open a new Firefox window with a link? 如果选项卡在某个域中打开,如何使插件/扩展添加图标/按钮到Firefox地址栏? - how make an addon/extension add an icon/button to the Firefox address bar if the tab is open in a certain domain? 如何在 Firefox 中复制打开的标签标题列表? - How can I copy a list of the open tab titles in Firefox? Firefox扩展中的打开窗口事件 - Open window event in Firefox extension 如何打开File:/// URL Firefox扩展开发的标签? - How to open a tab to a file:/// URL Firefox extension developement? 如何在当前选项卡中打开主页(Firefox 扩展) - How to open home page in current tab (Firefox extension) 我的 Firefox 扩展中可以有一个没有阴影的 XUL 面板吗? - Can I have an XUL panel without a shadow in my Firefox extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM