all links in new tab

open all links in new tab

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         all links in new tab
// @namespace    https://github.com/x94fujo6rpg/SomeTampermonkeyScripts
// @version      0.04
// @description  open all links in new tab
// @author       x94fujo6
// @match        *://*/*
// @grant        GM_openInTab
// @run-at       document-body
// ==/UserScript==
/* jshint esversion: 9 */

(function () {
	'use strict';
	const
		/**
		 * active: decides whether the new tab should be focused, (是否在新分頁打開後跳到該分頁)
		 * insert: that inserts the new tab after the current one, (是否將新分頁排在目前分頁後面)
		 * setParent: makes the browser re-focus the current tab on close (是否在該分頁關閉時回到目前分頁)
		 * incognito: makes the tab being opened inside a incognito mode/private mode window. (是否在隱密模式下打開分頁)
		 */
		newTabConfig = {
			active: false,
			insert: false,
			setParent: false,
			incognito: false,
		},
		linkReg = /^(https|http):\/\/.+/,
		log = (...any) => console.log(`%c[open all link in new tab]%c`, "color:OrangeRed;", "", ...any),
		asyncTimeout = (fun, time) => {
			return new Promise(resolve => {
				setTimeout(() => {
					fun();
					resolve();
				}, time);
			});
		},
		setOnclick = () => {
			let links = document.querySelectorAll("[href]");
			if (links) {
				links.forEach(link => {
					if (!link.href.match(linkReg)) {
						log(`this is not a http/https link, skip`, link);
						return;
					}
					if (link.getAttribute("setNewTab") == "true") {
						log(`this link has been set, skip`, link);
						return;
					}
					link.onclick = function (event) {
						GM_openInTab(link.href, newTabConfig);
						event.preventDefault();
					};
					link.setAttribute("setNewTab", true);
				});
				return true;
			} else {
				setTimeout(setOnclick, 500);
				return false;
			}
		},
		setQueue = async (max = 3) => {
			let done = false;
			for (let i = 0; i < max; i++) {
				await asyncTimeout(() => done = setOnclick(), 1000 * i);
				if (!done) continue;
				log("all links are set");
				break;
			}
		},
		ob = new MutationObserver(setQueue);

	//--------------------------------
	log("script start");
	setQueue();
	ob.observe(document.body, { childList: true, });

})();