简体   繁体   English

在弹出窗口中打开Chrome扩展程序链接

[英]Open Chrome extension link within popup

I am trying to create sub menus in a very simple Chrome extension within popup.html . 我正在尝试在popup.html中的一个非常简单的Chrome扩展程序中创建子菜单。 I've been successful at creating links to open pages in a new tab using html. 我已经成功创建了使用html在新标签页中打开页面的链接。 For instance: 例如:

<a class="two" href="https://chrome.google.com/webstore">
  <img width="16" height="16" src="webstore.png" />Chrome Web Store
</a>

I added the following event listener to popup.js to get this to work: 我在popup.js添加了以下事件监听器,以使其正常工作:

document.addEventListener('DOMContentLoaded', function () {
  var links = document.getElementsByTagName("a");
  for (var i = 0; i < links.length; i++) {
    (function () {
      var ln = links[i];
      var location = ln.href;
      ln.onclick = function () {
        chrome.tabs.create({active: true, url: location});
      };
    })();
  }
});

In order to minimize the number of links in popup.html , I would like to create a submenu system, where a user clicks on a link to open a new html document in the popup. 为了最大程度地减少popup.html的链接popup.html ,我想创建一个子菜单系统,用户在其中单击链接以在弹出窗口中打开新的html文档。

<a class="two" href="submenu.html">
  <img width="16" height="16" src="submenu.png" />Submenu
</a>

The popup will contain more links like the one to the Chrome Webstore in the first example. 在第一个示例中,弹出窗口将包含更多链接,例如指向Chrome Webstore的链接。 Of course, this opens submenu.html in a new tab, not within the Chrome extension popup. 当然,这submenu.html在新标签页中打开submenu.html ,而不是在Chrome扩展程序弹出窗口中打开。 Is there an easy way to open a link within the popup, rather than in a new tab? 有没有一种简单的方法可以在弹出窗口中而不是在新标签页中打开链接?

Thank you. 谢谢。

I think I answered my own question: 我想我回答了自己的问题:

  1. First add apply EventListener only when name="newtab" 首先仅在name="newtab"时添加apply EventListener

     document.addEventListener('DOMContentLoaded', function () { var links = document.getElementsByName("newtab"); for (var i = 0; i < links.length; i++) { (function () { var ln = links[i]; var location = ln.href; ln.onclick = function () { chrome.tabs.create({active: true, url: location}); }; })(); } });` 
  2. Add name="newtab" to each <a> where you want to open in a new tab. name="newtab"添加到要在新选项卡中打开的每个<a>中。

  3. For all other links, proceed normally; 对于所有其他链接,请正常进行。 they will open in the popup. 它们将在弹出窗口中打开。

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

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