Skip to content

Commit 716d51e

Browse files
committed
新增 针对动态加载内容的链接修改;优化 代码
1 parent 17c4040 commit 716d51e

1 file changed

Lines changed: 44 additions & 13 deletions

File tree

TargetBlank.user.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 新标签页打开链接
3-
// @version 1.0.3
3+
// @version 1.0.4
44
// @author X.I.U
55
// @description 将网页中所有链接改为新标签页打开~
66
// @match *://*/*
@@ -15,23 +15,54 @@
1515

1616
(function() {
1717
'use strict';
18-
targetBlank(); // 修改为新标签页打开
18+
targetBlank(); // 修改为新标签页打开
19+
targetDiscuz(); // 针对 Discuz! 论坛的帖子
20+
aObserver(); // 针对动态加载内容中的 a 标签
1921

20-
// 针对 Discuz! 论坛的帖子
21-
if (document.querySelector('meta[name="author"][content*="Discuz!"], meta[name="generator"][content*="Discuz!"]') || document.getElementById('ft') && document.getElementById('ft').textContent.indexOf('Discuz!') > -1) {
22-
let atarget = document.getElementById('atarget');
23-
if (atarget && atarget.className.indexOf('atarget_1') === -1) { // 强制勾选 [新窗]
24-
atarget.click();
25-
}
26-
}
2722

28-
// 修改为新标签页打开
23+
// 修改为新标签页打开
2924
function targetBlank() {
30-
document.head.appendChild(document.createElement('base')).target = '_blank';
25+
document.head.appendChild(document.createElement('base')).target = '_blank'; // 让所有链接默认以新标签页打开
3126
Array.from(document.links).forEach(function (_this) {
32-
if (_this.href && _this.href.slice(0,4) === 'http') {
33-
_this.target = '_blank'
27+
if (_this.href && _this.href.slice(0,4) != 'http') {
28+
_this.target = '_self'
3429
}
3530
})
3631
}
32+
33+
34+
// 针对 Discuz! 论坛的帖子
35+
function targetDiscuz() {
36+
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)) {
37+
let atarget = document.getElementById('atarget');
38+
if (atarget && atarget.className.indexOf('atarget_1') === -1) { // 强制勾选 [新窗]
39+
atarget.click();
40+
}
41+
}
42+
}
43+
44+
45+
// 针对动态加载内容中的 a 标签
46+
function aObserver() {
47+
const callback = (mutationsList, observer) => {
48+
for (const mutation of mutationsList) {
49+
for (const target of mutation.addedNodes) {
50+
if (target.nodeType != 1) return
51+
if (target.tagName === 'A') {
52+
if (target.href && target.href.slice(0,4) != 'http') {
53+
target.target = '_self'
54+
}
55+
} else {
56+
document.querySelectorAll('a').forEach(function (_this) {
57+
if (_this.href && _this.href.slice(0,4) != 'http') {
58+
_this.target = '_self'
59+
}
60+
});
61+
}
62+
}
63+
}
64+
};
65+
const observer = new MutationObserver(callback);
66+
observer.observe(document, { childList: true, subtree: true });
67+
}
3768
})();

0 commit comments

Comments
 (0)