From 716d51e0a5ca027589236b982c6990803fcd028c Mon Sep 17 00:00:00 2001 From: xiu2 <54703944+XIU2@users.noreply.github.com> Date: Fri, 8 Oct 2021 09:35:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E9=92=88=E5=AF=B9?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=8A=A0=E8=BD=BD=E5=86=85=E5=AE=B9=E7=9A=84?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E4=BF=AE=E6=94=B9;=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TargetBlank.user.js | 57 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/TargetBlank.user.js b/TargetBlank.user.js index 386ace8..562eb1e 100644 --- a/TargetBlank.user.js +++ b/TargetBlank.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name 新标签页打开链接 -// @version 1.0.3 +// @version 1.0.4 // @author X.I.U // @description 将网页中所有链接改为新标签页打开~ // @match *://*/* @@ -15,23 +15,54 @@ (function() { 'use strict'; - targetBlank(); // 修改为新标签页打开 + targetBlank(); // 修改为新标签页打开 + targetDiscuz(); // 针对 Discuz! 论坛的帖子 + aObserver(); // 针对动态加载内容中的 a 标签 - // 针对 Discuz! 论坛的帖子 - if (document.querySelector('meta[name="author"][content*="Discuz!"], meta[name="generator"][content*="Discuz!"]') || document.getElementById('ft') && document.getElementById('ft').textContent.indexOf('Discuz!') > -1) { - let atarget = document.getElementById('atarget'); - if (atarget && atarget.className.indexOf('atarget_1') === -1) { // 强制勾选 [新窗] - atarget.click(); - } - } - // 修改为新标签页打开 + // 修改为新标签页打开 function targetBlank() { - document.head.appendChild(document.createElement('base')).target = '_blank'; + document.head.appendChild(document.createElement('base')).target = '_blank'; // 让所有链接默认以新标签页打开 Array.from(document.links).forEach(function (_this) { - if (_this.href && _this.href.slice(0,4) === 'http') { - _this.target = '_blank' + if (_this.href && _this.href.slice(0,4) != 'http') { + _this.target = '_self' } }) } + + + // 针对 Discuz! 论坛的帖子 + function targetDiscuz() { + if (document.querySelector('meta[name="author"][content*="Discuz!"], meta[name="generator"][content*="Discuz!"]') || document.querySelector('body[id="nv_forum"][class^="pg_"][onkeydown*="27"]') || document.querySelector('body[id="nv_search"][onkeydown*="27"]') || (document.querySelector('a[href*="www.discuz.net"]') && document.querySelector('a[href*="www.discuz.net"]').textContent.indexOf('Discuz!') > -1) || (document.getElementById('ft') && document.getElementById('ft').textContent.indexOf('Discuz!') > -1)) { + let atarget = document.getElementById('atarget'); + if (atarget && atarget.className.indexOf('atarget_1') === -1) { // 强制勾选 [新窗] + atarget.click(); + } + } + } + + + // 针对动态加载内容中的 a 标签 + function aObserver() { + const callback = (mutationsList, observer) => { + for (const mutation of mutationsList) { + for (const target of mutation.addedNodes) { + if (target.nodeType != 1) return + if (target.tagName === 'A') { + if (target.href && target.href.slice(0,4) != 'http') { + target.target = '_self' + } + } else { + document.querySelectorAll('a').forEach(function (_this) { + if (_this.href && _this.href.slice(0,4) != 'http') { + _this.target = '_self' + } + }); + } + } + } + }; + const observer = new MutationObserver(callback); + observer.observe(document, { childList: true, subtree: true }); + } })(); \ No newline at end of file