Skip to content

Commit 85ca805

Browse files
committed
优化 代码(排除 # 链接)
1 parent 3e8cdb5 commit 85ca805

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

TargetBlank.user.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 新标签页打开链接
3-
// @version 1.0.4
3+
// @version 1.0.5
44
// @author X.I.U
55
// @description 将网页中所有链接改为新标签页打开~
66
// @match *://*/*
@@ -24,7 +24,7 @@
2424
function targetBlank() {
2525
document.head.appendChild(document.createElement('base')).target = '_blank'; // 让所有链接默认以新标签页打开
2626
Array.from(document.links).forEach(function (_this) {
27-
if (_this.href && _this.href.slice(0,4) != 'http') {
27+
if (_this.onclick || _this.href.slice(0,4) != 'http' || _this.getAttribute('href').slice(0,1) === '#') {
2828
_this.target = '_self'
2929
}
3030
})
@@ -49,12 +49,12 @@
4949
for (const target of mutation.addedNodes) {
5050
if (target.nodeType != 1) return
5151
if (target.tagName === 'A') {
52-
if (target.href && target.href.slice(0,4) != 'http') {
52+
if (target.onclick || target.href.slice(0,4) != 'http' || target.getAttribute('href').slice(0,1) === '#') {
5353
target.target = '_self'
5454
}
5555
} else {
5656
document.querySelectorAll('a').forEach(function (_this) {
57-
if (_this.href && _this.href.slice(0,4) != 'http') {
57+
if (_this.onclick || _this.href.slice(0,4) != 'http' || _this.getAttribute('href').slice(0,1) === '#') {
5858
_this.target = '_self'
5959
}
6060
});

0 commit comments

Comments
 (0)