使用 JavaScript 通过单击在单个窗口上打开多个选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7564392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:33:19  来源:igfitidea点击:

open multiple tabs on a single window by a single click using JavaScript

javascriptgoogle-chrometabsgoogle-chrome-extension

提问by Saurabh Saxena

I need to open multiple tabs on a single window, new window instance for a single link will not be a problem but when it comes to 20+ (which is my case) then 20+ new windows are really a problem, so I need to find a solution, the code has to run on chrome only in my case I have 35 links stored in an array. I am reading array using a for loop and opening links in new tabs using window.open()
I can use only JavaScript for this. I am developing a customized chrome extension.

我需要在一个窗口上打开多个选项卡,单个链接的新窗口实例不会有问题,但是当涉及 20+(这是我的情况)时,20+ 新窗口确实是一个问题,所以我需要找到解决方案,代码必须仅在我的情况下在 chrome 上运行我有 35 个链接存储在一个数组中。我正在使用 for 循环读取数组并使用window.open()
我只能使用 JavaScript在新选项卡中打开链接。我正在开发一个定制的 chrome 扩展。

I found out that while using window.open()to open multiple links in different tabs of a same window in Google Chrome, it succeeds in opening only first 24 windows and left out the rest.
I need to find out a way to open all the links at once with a single click.

我发现window.open()在 Google Chrome 中使用在同一窗口的不同选项卡中打开多个链接时,它成功地只打开了前 24 个窗口而忽略了其余的窗口。
我需要找到一种方法,只需单击一下即可打开所有链接。

There are some Google Chrome Extensions available which work like this like LinkClump
This Extension successfully open all selected links in different tabs of a same window. I am trying to modify its working to suit mine.

有一些 Google Chrome 扩展程序可用,它们的工作方式像 LinkClump
此扩展程序成功打开同一窗口不同选项卡中的所有选定链接。我正在尝试修改它的工作以适合我的。

Meanwhile, if anyone can get any solution, he/she is most welcome.

同时,如果有人能得到任何解决方案,他/她是最受欢迎的。

回答by Alasdair

I wasn't sure if you wanted the links to be open in a new window or not so I've included both possibilities;

我不确定您是否希望在新窗口中打开链接,所以我已经包含了两种可能性;

Same Window

同窗

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]
    });
}

chrome.tabs documentation

chrome.tabs 文档

New Window

新窗户

// will open a new window loaded with all your links
chrome.windows.create({
    url: linkArray
});

chrome.windows documentation

chrome.windows 文档

Regardless of which approach you use you will need to declare the tabspermission in your extension's manifest.

无论您使用哪种方法,您都需要在扩展程序的清单中声明tabs权限。

回答by kbbagal

You can use target="_blank"attribute of a link for opening the corresponding page in new tab.

您可以使用target="_blank"链接的属性在新选项卡中打开相应的页面。

回答by cncDAni2

Solotion: Use setTimeout! With more and more time values

解决方法:使用 setTimeout!随着越来越多的时间价值

javascript:
var a=document.location.href;
for (var i=0;i<50;i++){
    setTimeout('window.open(a)',i*200);
}
void(0);

This will clone the current tabs 50 times, even in Chrome! Unfortunately, in new Windows instead of new tabs. Silly Chrome :-)

即使在 Chrome 中,这也会克隆当前标签 50 次!不幸的是,在新窗口而不是新标签中。愚蠢的铬 :-)