简体   繁体   中英

When click name in popup open link in new tab?

I have a chrome extension that gets friend requests from Facebook in the popup. 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. 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. 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.

The solution is to add target=_blank attribute.
Replace $('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.


const url = "http://www.some-web-site.com";

chrome.tabs.create({ url });


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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