Chrome extension; open a link from popup.html in a new tab

前端 未结 9 606
挽巷
挽巷 2020-12-04 16:23

I\'m doing a Chrome extension and I got helped in this post here.

My problem now is how to open a new tab of chrome that has as URL the link I clicked in the

相关标签:
9条回答
  • 2020-12-04 17:20

    I had the same problem. Looked like Konrad's solution would worked, but it opened multiple tabs at once. This happened only after first extension install. So I changed it to

    if (e.target.classList.contains("a-link")) {
        chrome.tabs.create({url: $(e.target).attr('href')});
        return false;
    }
    

    and all is working as expected.

    0 讨论(0)
  • 2020-12-04 17:21

    Send tab url to share blog in new tab:

    // popup.js
    chrome.tabs.query({ active: true, currentWindow: true }, function(tabs){        
        var url = tabs[0].url;
        var title = tabs[0].title;
        document.getElementById('linkQZone').onclick = function () {
            var url1 = 'https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + url + '&title=' + title + '&desc=&summary=&site=';
            chrome.tabs.create({active: true, url: url1});
        };
    
        document.getElementById('linkQQ').onclick = function () {
            var url1 = 'https://connect.qq.com/widget/shareqq/index.html?url=' + url + '&title=' + title + '&desc=&summary=&site=';
            chrome.tabs.create({active: true, url: url1});
        };
    });
    
    0 讨论(0)
  • 2020-12-04 17:22

    See my comment https://stackoverflow.com/a/17732609/1340178


    I had the same issue and this was my approach:

    1. Create the popup.html with link (and the links are not working when clicked as Chrome block them).
    2. Create popup.js and link it in the page: <script src="popup.js" ></script>
    3. Add the following code to 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});
                  };
              })();
          }
      });
      

    That's all, links should work after that.

    0 讨论(0)
提交回复
热议问题