如何让Google Chrome扩展程序中的弹出窗口链接在同一标签页下打开?

5
我希望链接出现在选项卡中,在弹出窗口消失的情况下。
目前的代码如下:
//Open links in tab from popup
if (document.location.search == '?popup')
$('a').attr('target', '_blank');

但是,_blanks会在新标签页中打开。如有帮助需要,请随时联系我们 - 谢谢!
1个回答

7
您需要首先通过以下方式获取当前选定的选项卡: http://code.google.com/chrome/extensions/tabs.html#method-getSelected 然后,您可以使用回调触发的tab.id,并将其与一个url进行更新: http://code.google.com/chrome/extensions/tabs.html#method-update 例如:
chrome.tabs.getSelected({}, function(tab) {
  chrome.tabs.update(tab.id, {url: 'http://google.com'});
});

如果您想让弹出页面中的每个链接更新当前打开的选项卡,则可以执行以下操作(如您在评论中提到但使用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.
});

类似这样的代码对我不起作用://在同一标签页下打开弹出窗口中的所有链接 如果(document.location.search == '?popup') $('a').live('click', function(e) { var href = e.targetElement.href; chrome.tabs.getSelected({}, function(tab) { chrome.tabs.update(tab.id, {url: href}); }); }); - jprim
当您右键单击弹出窗口并选择检查时,检查器中是否有任何错误? - Mohamed Mansour
没有。虽然我不能右击弹出窗口,但必须在全屏版本中查看它。 - jprim
谢谢,Mohamed。我刚刚给你发了一封电子邮件! - jprim
1
没问题!我已经更新了上面的答案,以便其他人能够找到它很有用。 - Mohamed Mansour
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接