开发者

How to make popup.html links open in tab?

开发者 https://www.devze.com 2023-02-01 06:42 出处:网络
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 thi开发者_JS百科s possible?Add target=&qu

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 thi开发者_JS百科s possible?


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>


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.


Re: is there another way

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消