10

I have a Chrome extension that has some links in it. Currently when clicked the links do nothing, i would like to make them open in a new tab when clicked. Is this possible?

user556396
  • 597
  • 3
  • 12
  • 26
  • There are also some good answers here: http://stackoverflow.com/questions/8915845/chrome-extension-open-a-link-from-popup-html-in-a-new-tab – rogerdpack Oct 20 '16 at 19:51

3 Answers3

27

Add target="_blank" to links.

Another way is to attach link opening javascript code to mousedown event on a link.

You can also use base tag to make all links open with target="_blank":

<head>
    <base target="_blank">
</head>
Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
serg
  • 109,619
  • 77
  • 317
  • 330
6

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.

lasantha
  • 825
  • 8
  • 7
  • You've forgotten to close over variable `i` in the closure. Further, `chrome.tabs.create` doesn't require any permissions. – Rob W Jul 18 '13 at 20:05
  • I do not need to close over i, only ln and location are enough. You are right about permission, modified my answer. – lasantha Jul 19 '13 at 04:17
2

Re: is there another way

chrome.tabs.create( { url: "http://www.ajaxian.com"} );

See http://code.google.com/chrome/extensions/tabs.html

Kai
  • 9,038
  • 5
  • 28
  • 28