[英]When click name in popup open link in new tab?
I have a chrome extension that gets friend requests from Facebook in the popup.我有一个 chrome 扩展程序,它在弹出窗口中从 Facebook 获取朋友请求。 I have received all friend requests in the popup.我已收到弹出窗口中的所有好友请求。 When I click the friend request's name, I want to open his/her profile in a new tab.当我点击好友请求的名字时,我想在新标签页中打开他/她的个人资料。 I have got all links to their profiles.我有他们个人资料的所有链接。 I save them in localStorage and get them in the popup.我将它们保存在 localStorage 中并在弹出窗口中获取它们。 But when I click the name, it does not open his/her profile.但是当我点击名字时,它并没有打开他/她的个人资料。 Can someone help me to open their profiles please?有人可以帮我打开他们的个人资料吗?
Here is my code:这是我的代码:
//content.js
let value = $('.bl').html();
chrome.storage.local.set({friends: value}, function() {
});
let lengths = $('.bl').children();
let num = lengths.length;
console.log(num);
chrome.storage.local.set({leng: num});
$(".bl a.bo").each(function( index ) {
let hrefs = $( this ).attr('href');
chrome.storage.local.set({link: hrefs});
console.log(hrefs)
});
//popup.js
chrome.storage.local.get(['friends','leng','link'],function(result) {
$(".ul_list").html(result.friends);
$('.userNum').html(result.leng);
$('a.bo').attr('onclick',"window.open(result.link)");
});
chrome.storage.onChanged.addListener(changes => {
if (changes.friends) {
}
});
If you inspect popup's devtools console you will probably see an error about inline JavaScript code being blocked.如果您检查弹出窗口的 devtools 控制台,您可能会看到有关内联 JavaScript 代码被阻止的错误。 The popup is a separate window so it has its own devtools which you can open by right-clicking inside the popup to show the context menu, then click "inspect" in the menu.弹出窗口是一个单独的 window,因此它有自己的开发工具,您可以通过在弹出窗口中右键单击来打开它以显示上下文菜单,然后单击菜单中的“检查”。
The solution is to add target=_blank
attribute.解决方案是添加target=_blank
属性。
Replace $('a.bo').attr('onclick',"window.open(result.link)");
替换$('a.bo').attr('onclick',"window.open(result.link)");
with this:有了这个:
$('a.bo').attr({href: result.link, target: '_blank'});
In Chrome extension environment to programmatically open a new tab you will have to use chrome.tabs.create
method.在 Chrome 扩展环境中以编程方式打开一个新选项卡,您必须使用chrome.tabs.create
方法。
const url = "http://www.some-web-site.com";
chrome.tabs.create({ url });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.