简体   繁体   English

如何在下方的同一标签中打开Goog​​le Chrome扩展程序弹出窗口中的链接?

[英]How do I have a link from a Google Chrome extension popup open in the same tab underneath?

I want the link to appear in the tab beneath resulting in the popup going away. 我希望链接显示在下面的选项卡中,导致弹出窗口消失。

Currently have this: 目前有这个:

//Open links in tab from popup
if (document.location.search == '?popup')
$('a').attr('target', '_blank');

But the _blanks opens in a new tab. 但_blanks在新标签中打开。 Any help would be greatly appreciated - thanks! 任何帮助将不胜感激 - 谢谢!

You would need to get the current selected tab first via, http://code.google.com/chrome/extensions/tabs.html#method-getSelected 您需要首先通过http://code.google.com/chrome/extensions/tabs.html#method-getSelected获取当前选定的标签

Then you use the tab.id, that the callback has fired, and updating it with a url: http://code.google.com/chrome/extensions/tabs.html#method-update 然后使用tab.id,回调已触发,并使用网址更新它: http//code.google.com/chrome/extensions/tabs.html#method-update

For example: 例如:

chrome.tabs.getSelected({}, function(tab) {
  chrome.tabs.update(tab.id, {url: 'http://google.com'});
});

If you want to let every link in the popup page to update the current tab opened. 如果要让弹出页面中的每个链接更新当前打开的选项卡。 You can do the following (as you mentioned within the comments but with currentTarget): 您可以执行以下操作(正如您在注释中提到的但使用currentTarget):

$('a').live('click', function(e) {
  var href = e.currentTarget.href;
  chrome.tabs.getSelected(null,function(tab) {
    chrome.tabs.update(tab.id, {url: href});
  });
  window.close(); // To close the popup.
});

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

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