【问题标题】:Open multiple links in Chrome at once as new tabs在 Chrome 中一次打开多个链接作为新标签页
【发布时间】:2014-08-13 09:16:45
【问题描述】:

我正在尝试在 Google Chrome 的新标签页中一次打开多个链接,但失败了。

问题:

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

使用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 尝试单独打开链接,但没有成功。

【问题讨论】:

  • 这似乎有点不确定,您试图绕过弹出框,即使它们是出于这个特定原因而制作的,以阻止弹出框...:L
  • 我没有做一些可疑的事情.. 看看扩展,开发人员是如何绕过它的?嗯
  • 你想这样做independently of browser settings吗?我不认为这是可能的。

标签: javascript angularjs google-chrome tabs popup


【解决方案1】:

浏览器扩展之所以能做到,是因为Chrome扩展have access to a special Chrome API,可以让你使用:

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

其中createDatatabid 值大于任何当前选项卡(您可以使用chrome.windows.getAll() 找到最大的当前tabid)。

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

【讨论】:

  • 这是正确的。浏览器扩展可以做的比网页更多。尽管两者都使用 javascript,但它们是非常非常不同的东西。
【解决方案2】:

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

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 中找到扩展程序

【讨论】:

  • 虽然查看浏览器扩展的代码可能很有趣,但此代码在网站上不起作用。
  • chrome.tabs.create 是我在回答中讨论的 Chrome API 的一部分——它不适用于网页。
  • 我知道它在网页中不起作用,因为它使用了 chrome api。那是为了回答他关于扩展开发人员如何实现它的问题。
【解决方案3】:

您可以在原生 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

【讨论】:

  • "vanilla JavaScript" 在您的示例中运行良好。但是具有多个 open_win 函数(和相应按钮)的页面不会为每个按钮打开多个选项卡:
  • “vanilla JavaScript”不再在 chrome 中工作(2018 年 1 月),它只打开一个标签,而不是两个..
  • 截至 2018 年 7 月,我认为最好的解决方案是这样的:window.open("https://www.ecosia.org", "_new"); window.open("https://www.duckduckgo.com", "secondWindow"); 诀窍是确保第二个窗口的名称不是_new,否则浏览器会尝试粘贴链接到覆盖第一个 window.open 的同一窗口。我已经在 Chrome 和 IE 中尝试过,它对我有用。
  • 截至 2019 年 11 月,tfantina 的代码仅在移动版 Safari (iOS 13.2) 上打开一个标签页。不过在 Mac Safari (13.0.3) 上运行良好。
  • 老兄,你的第二个解决方案很棒。
【解决方案4】:

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

<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>

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

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

    • 这只会在我的一个标签中打开。
    【解决方案6】:

    打开多个选项卡或窗口的最佳方法是使用 500 毫秒的 setTimeout()

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

    【讨论】:

      【解决方案7】:

      由于现代浏览器(甚至是带有拦截器的旧浏览器)绝对不允许这样做(一个用户操作,一个新标签)。我的解决方案是:

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

      用户可以通过ctrl+点击按钮N次快速连续打开链接。

      【讨论】:

      【解决方案8】:

      我有一个使用 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);
          }
        });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-28
        • 2014-05-05
        • 2020-04-08
        • 1970-01-01
        • 2016-03-28
        • 2014-04-18
        • 2013-11-30
        • 1970-01-01
        相关资源
        最近更新 更多