立即在Chrome中打开多个链接作为新标签

时间:2014-06-23 10:54:45

标签: javascript angularjs google-chrome tabs popup

我试图在新版标签中一次性在谷歌浏览器中打开多个链接,但它失败了。

问题:

  1. 被弹出窗口阻止
  2. 在用户允许弹出窗口后,在新窗口中打开而不是选项卡

  3. 使用this,我可以在Firefox中一次打开多个链接:

    <!DOCTYPE html>
    <html ng-app="plunker">
    
    <head>
        <meta charset="utf-8">
        <title>AngularJS Plunker</title>
        <script>document.write('<base href="' + document.location + '" >');</script>
        <link rel="stylesheet" href="style.css">
        <script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js" data-semver="1.2.17"></script>
        <script src="app.js"></script>
    </head>
    
    <body ng-controller="MainCtrl">
        <button ng-click="openLinks()">Open</button>
    </body>
    
    </html>
    

    另外,我遇​​到了一个找到a workaround的人。

    我尝试使用setInterval尝试单独打开链接,但它无法正常工作。

8 个答案:

答案 0 :(得分:25)

您可以在vanilla JavaScript中执行此操作:

<html>
<head>
<script type="text/javascript">
function open_win() {
    window.open("http://www.java2s.com/")
    window.open("http://www.java2s.com/")
}
</script>
</head>

<body>
<form>
<input type=button value="Open Windows" onclick="open_win()">
</form>
</body>

</html>

这是一个更具特定于Chrome的实现(如果弹出窗口阻止程序给你带来困难):

var linkArray = []; // your links
for (var i = 0; i < linkArray.length; i++) {
    // will open each link in the current window
    chrome.tabs.create({
        url: linkArray[i]
    });
}

以下是一些文档:https://developer.chrome.com/extensions/tabs

答案 1 :(得分:4)

浏览器扩展程序可以执行此操作的原因是Chrome扩展程序have access to a special Chrome API,可让您使用:

chrome.windows.create({tabid: n})

其中createData的{​​{1}}值大于任何当前标签(您可以使用tabid找到最大的当前tabid)。

但是,就在您的网页上(或任何不是Chrome扩展程序的地方)进行此操作而言,这是不可能的,因为whether or not a new window opens in a new tab is determined entirely by the user's settings

答案 2 :(得分:1)

用户必须允许弹出窗口,但我最终还是这样做了:

function openMultipleTabs(urlsArray){

    urlsArray.forEach(function(url){
        let link     = document.createElement('a');
        link.href    = url;
        link.target  = '_blank';
        link.click();
    });

}

答案 3 :(得分:0)

以下代码将在单击按钮时打开多个弹出窗口。

<html>
<head>
<title></title>
<script type="text/javascript">
  function open_win() {
 window.open("url","windowName","windowFeatures")
 window.open("url","DifferentWindowName","windowFeatures")// different name for each popup
}
</script>
</head>

<body>
<form>
<input type=button value="Open Windows" onclick="open_win()">
</form>
</body>

您需要确保每个窗口名称都不同,否则最后一个弹出窗口将覆盖它的上一个弹出窗口。结果,您最终会看到一个弹出窗口。

答案 4 :(得分:0)

打开多个标签或窗口的最佳方法是使用500ms的setTimeout()

  window.open("facebook.com","one","windowFeatures")
  setTimeout(function(){   window.open("facebook.com","two","windowFeatures") }, 500);

答案 5 :(得分:0)

由于现代浏览器(甚至是带有阻止程序的旧浏览器)绝对不允许这样做(一个用户操作,一个新选项卡)。我的解决方案是:

    openInfoLinks = () => {
        const urlsArray = [
            `https://...`,
            `https://...`,
            `https://...`,
        ]
        window.open(
            urlsArray[this.linkCounter],
            `_blank_${someIdentifier}_${this.linkCounter}`
        );
        this.linkCounter++;
        setTimeout(() => {
            this.linkCounter = 0;
        }, 500);
    }

用户可以通过连续按住Ctrl键并单击N次单击来快速打开链接。

答案 6 :(得分:0)

我有一个使用 setTimeout 的简单解决方案,请在下面查看

function openMultipleTabs(urlsArray: string[]) {
  urlsArray.forEach((url: string, key: number) => {
    if (key === 0) {
      window.open(url, `_blank_first_${key.toString()}`);
    } else {
      setTimeout(function () {
        console.log("resolved", key);
        window.open(url, `_blank_${key.toString()}`);
      }, 1500 * key);
    }
  });
}

答案 7 :(得分:-1)

看起来扩展程序使用代码下方打开这些标签。

function openTab(urls, delay, window_id, tab_position, close_time) {
    var obj = {
            windowId: window_id,
            url: urls.shift().url,
            selected: false
    }

    if(tab_position != null) {
        obj.index = tab_position
        tab_position++;
    }

    chrome.tabs.create(obj, function(tab) {
        if(close_time > 0) {
            window.setTimeout(function() {
                chrome.tabs.remove(tab.id);
            }, close_time*1000);
        }
    });

    if(urls.length > 0) {
        window.setTimeout(function() {openTab(urls, delay, window_id, tab_position, close_time)}, delay*1000);
    }

}

如果您想查看扩展程序的代码以供参考,您可以在(适用于Windows)C:\Documents and Settings\*UserName*\Local Settings\Application Data\Google\Chrome\User Data\Default\Extensions

中找到扩展程序