Open multiple links in Chrome at once as new tabs

自古美人都是妖i 提交于 2019-11-27 23:41:08

You can do this in 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>

Here is a more Chrome-specific implementation (if popup blockers are giving you difficulty):

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

Here is some documentation: https://developer.chrome.com/extensions/tabs

Patrick Collins

The reason that the browser extension can do it is because Chrome extensions have access to a special Chrome API, which lets you use:

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

where createData has a tabid value greater than any current tab (and you can find the greatest current tabid using chrome.windows.getAll()).

However, in terms of doing it on your page (or anywhere that's not a Chrome extension), that's not possible, since whether or not a new window opens in a new tab is determined entirely by the user's settings.

The following code will open multiple popUp on the button click.

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

you need to make sure that each window name is different, otherwise the last popup will overwrite it's previous popup. As a result you will end up with a single popup.

Looks like extension uses below code to open those tabs.

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

}

If you want to take a look at the code of the extension for reference you will find the extensions in (for Windows) C:\Documents and Settings\*UserName*\Local Settings\Application Data\Google\Chrome\User Data\Default\Extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!