新标签打开第三方链接,当前标签打开第一方链接。
// ==UserScript==
// @name 新标签打开第三方链接,当前标签打开第一方链接
// @version 1.3
// @author ChatGPT
// @description 新标签打开第三方链接,当前标签打开第一方链接。
// @match *://*/*
// @run-at document-end
// @namespace https://greasyfork.org/users/452911
// ==/UserScript==
function handleLinks() {
const currentDomain = window.location.hostname;
// 处理外部链接
document.querySelectorAll('a').forEach(link => {
if (link.href.startsWith("http") && new URL(link.href).hostname !== currentDomain) {
link.setAttribute('target', '_blank');
} else {
link.setAttribute('target', '_self');
}
});
// 处理内部链接
document.querySelectorAll('a[href^="/"]').forEach(link => {
link.setAttribute('target', '_self');
});
}
// 执行函数
handleLinks();
(function() {
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.addedNodes.length > 0) {
mutation.addedNodes.forEach(addedNode => {
if (addedNode.nodeType === Node.ELEMENT_NODE) {
handleLinks();
}
});
}
});
});
const config = { childList: true, subtree: true };
observer.observe(document.body, config);
})();