// ==UserScript== // @name GitHub + Google 链接在新标签页打开 // @namespace https://github.com/djkyc/scriptcat-merged-newtab // @version 1.0.0 // @description GitHub 全站链接与 Google 搜索结果在新标签页打开,保留原页面;统一用 target=_blank,不劫持 href。 // @author xiaolaji (GitHub 原脚本) + Black-Cyan (Google 原脚本),合并整理 // @match https://github.com/* // @match https://www.google.com/search* // @match https://google.com/search* // @match https://www.google.com.hk/search* // @match https://google.com.hk/search* // @match https://www.google.co.jp/search* // @match https://google.co.jp/search* // @match https://www.google.co.uk/search* // @match https://google.co.uk/search* // @grant none // @run-at document-end // @license MIT // ==/UserScript== // // 合并来源: // 1) ScriptCat 498《github的链接在新标签页打开》v0.1,作者 xiaolaji // https://scriptcat.org/zh-CN/script-show-page/498 // 2) ScriptCat 8233《Google 搜索结果在新标签页打开》v1.0.0,作者 Black-Cyan (MIT) // https://scriptcat.org/zh-CN/script-show-page/8233 // // 合并时修掉的原脚本问题(功能不变,只改实现): // - 498 只在 2 秒后跑一次、且把所有 a 的 href 改成 void 伪链接 // 再用 onclick 新窗口打开。这会破坏中键/ Ctrl+点击 / 右键 // “在新标签页中打开” / 复制链接 / 悬停看地址,还会覆盖链接原有 // 的 onclick,且 GitHub 动态加载(Turbo/翻页)的新链接完全不管。 // 改为和 8233 一样用 target="_blank" + rel="noopener noreferrer"。 // - 8233 只匹配了带 www 的 4 个 Google 域名,漏了不带 www 的同域名。 // 合并版把 4 个域名各补一条不带 www 的 @match。 // - 两脚本各用一套监听/跑法,合并为同一套:立即跑一次 + // 一个 MutationObserver(只监听 childList/subtree,不监听 // attributes,所以改 target/rel 本身不会触发回环),用 // data-newtab-done 做幂等标记,动态加进来的链接也能跟上。 // - Google 只改搜索结果链接(div#search a:has(h3)),不改页眉、 // 设置等其他链接;GitHub 改全站可导航链接,但跳过 # 锚点、 // javascript:/mailto:/tel:/data:/blob: 这类非导航链接。 (function () { 'use strict'; var DONE_ATTR = 'data-newtab-done'; function markNewTab(link) { link.setAttribute('target', '_blank'); link.setAttribute('rel', 'noopener noreferrer'); link.setAttribute(DONE_ATTR, '1'); } function isSkippable(rawHref) { if (!rawHref) return true; var h = rawHref.trim().toLowerCase(); return h === '' || h.charAt(0) === '#' || h.indexOf('javascript:') === 0 || h.indexOf('mailto:') === 0 || h.indexOf('tel:') === 0 || h.indexOf('data:') === 0 || h.indexOf('blob:') === 0; } function isDone(link) { return link.getAttribute(DONE_ATTR) === '1'; } // GitHub:全站所有可导航的 ,动态加载的也处理 function processGitHub() { var links = document.querySelectorAll('a'); for (var i = 0; i < links.length; i++) { var link = links[i]; if (isDone(link)) continue; if (isSkippable(link.getAttribute('href'))) continue; markNewTab(link); } } // Google:只处理搜索结果(带 h3 标题的链接),不碰页眉/设置等 function processGoogle() { var links; try { links = document.querySelectorAll('div#search a:has(h3), div#rso a:has(h3)'); } catch (e) { // 极旧浏览器不支持 :has() 时退化为手动筛 h3 var all = document.querySelectorAll('div#search a'); var filtered = []; for (var i = 0; i < all.length; i++) { if (all[i].querySelector('h3')) filtered.push(all[i]); } links = filtered; } for (var j = 0; j < links.length; j++) { var link = links[j]; if (isDone(link)) continue; if (isSkippable(link.getAttribute('href'))) continue; markNewTab(link); } } function processAll() { var host = location.hostname; if (host === 'github.com') { processGitHub(); } else if ( host === 'google.com' || host === 'google.com.hk' || host === 'google.co.jp' || host === 'google.co.uk' || host.endsWith('.google.com') || host.endsWith('.google.com.hk') || host.endsWith('.google.co.jp') || host.endsWith('.google.co.uk') ) { processGoogle(); } } // 把一连串 DOM 变动合并成一次处理,避免每次插入都全量扫一遍 var scheduled = false; function scheduleProcess() { if (scheduled) return; scheduled = true; setTimeout(function () { scheduled = false; processAll(); }, 0); } function start() { processAll(); if (typeof MutationObserver === 'undefined') return; var target = document.body || document.documentElement; if (!target) return; var observer = new MutationObserver(scheduleProcess); observer.observe(target, { childList: true, subtree: true }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', start); } else { start(); } })();