forked from XIU2/UserScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTargetBlank.user.js
37 lines (34 loc) · 1.39 KB
/
TargetBlank.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// ==UserScript==
// @name 新标签页打开链接
// @version 1.0.3
// @author X.I.U
// @description 将网页中所有链接改为新标签页打开~
// @match *://*/*
// @icon https://i.loli.net/2021/03/07/rdijeYm83pznxWq.png
// @grant none
// @license GPL-3.0 License
// @run-at document-end
// @namespace https://github.com/XIU2/UserScript
// @supportURL https://github.com/XIU2/UserScript
// @homepageURL https://github.com/XIU2/UserScript
// ==/UserScript==
(function() {
'use strict';
targetBlank(); // 修改为新标签页打开
// 针对 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';
Array.from(document.links).forEach(function (_this) {
if (_this.href && _this.href.slice(0,4) === 'http') {
_this.target = '_blank'
}
})
}
})();