// ==UserScript== // @name github的链接在新标签页打开 // @namespace pl816098 // @version 1.0.12 // @description 让github的链接默认是在新标签页中打开而不是当前页打开 // @author pl816098 // @match https://github.com/* // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @grant GM_getValue // @run-at document-idle // @license MIT // @supportURL https://github.com/Paul-16098/vs_code/issues/ // @homepageURL https://github.com/Paul-16098/vs_code/blob/main/js/userjs/README.md // ==/UserScript== /* ==UserConfig== config: host: title: 是否取消對外部域名的處理 description: 是否取消對外部域名的處理 type: checkbox default: true ==/UserConfig== */ (function () { "use strict"; const host = GM_getValue("config.host", true); const not_blank = ['a[data-testid="raw-button"]']; setTimeout(() => { console.log("github的链接在新标签页打开: setimeout start"); let links = document.getElementsByTagName("a"); for (let i = 0; i < links.length; i++) { console.log( "github的链接在新标签页打开: for(1), i: ", i, ", links[i]: ", links[i] ); let found = false; for (let ii = 0; ii < not_blank.length && not_blank.length > 0; ii++) { console.log( "github的链接在新标签页打开: for(2), ii: ", ii, ", not_blank[oi]: ", not_blank[ii] ); let element = document.querySelector(not_blank[ii]); if (element && element.href === links[i].href) { found = true; console.log("github的链接在新标签页打开: not_blank found", found); break; } } if (!found) { let url = links[i].href; let o_url = new URL(url); let patt = /(?:([^:/\\@\s])+\.)*github\.([a-zA-Z]{2,4})$/i; if (!patt.test(o_url.hostname)) { console.log("patt.test(o_url.hostname)", patt.test(o_url.hostname)); if (host) { console.log( "patt.test(o_url.hostname): break", patt.test(o_url.hostname) ); break; } } links[i].href = "javascript:void(0);"; links[i].onclick = function () { window.open(url); }; console.log("github的链接在新标签页打开 run done", links[i]); } } }, 2000); })();