Disable blank target

在当前页面打开同域名超链接,而不是跳转新页面,适配所有网站,包括头条和网易。Disable open links in a new window/tab(based on the hostname).

当前为 2025-03-19 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Disable blank target
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  在当前页面打开同域名超链接,而不是跳转新页面,适配所有网站,包括头条和网易。Disable open links in a new window/tab(based on the hostname).
// @author       Logan Wang
// @copyright    Logan Wang
// @homepage     https://github.com/azone
// @license      MIT
// @match        http*://*/*
// @icon         none
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    const neteasyRe = /163\.com$/
    const toutiaoRe = /toutiao\.com$/

    const originalURL = window.location;

    if (toutiaoRe.test(originalURL.host)) {
        const listener = document.addEventListener;
        document.addEventListener = (event, listenerFunc, options) => {
            if (event !== 'click') {
                listener(event, listenerFunc, options);
            }
        };
    }

    function shouldRemove(element) {
        const href = element.href;
        if (!href) {
            return false;
        }

        const url = new URL(href);
        return !url.host || url.host === originalURL.host || neteasyRe.test(url.host);
    }

    function removeBlankTargetIfNecessary(rootElement) {
        const selector = rootElement.querySelectorAll;
        if (!selector) {
            return;
        }

        const elements = rootElement.querySelectorAll('a[target="_blank"]');
        for (const element of elements) {
            if (shouldRemove(element)) {
                element.removeAttribute('target');
            }
        }
    }

    function removeBaseElements() {
        const baseElements = document.querySelectorAll('base[target="_blank"]');
        for (const base of baseElements) {
            base.parentNode.removeChild(base);
        }
    }

    function processElements() {
        removeBlankTargetIfNecessary(document);

        removeBaseElements();
    }

    if (document.readyState === "loading") {
        document.addEventListener('DOMContentLoaded', (e) => {
            processElements();
        });
    } else {
        processElements();
    }

    function observeChanges(records, observer) {
        for (const record of records) {
            if (record.type === 'attributes') {
                if (record.attributeName === 'target' && record.target.getAttribute(record.attributeName) === '_blank') {
                    if (shouldRemove(record.target)) {
                        record.target.removeAttribute(record.attributeName);
                    } else if (record.target.nodeName === 'BASE') {
                        record.target.parentNode.removeChild(record.target);
                    }
                }
            } else if (record.addedNodes.length > 0) {
                for (const node of record.addedNodes) {
                    if (node.nodeName === 'A' && shouldRemove(node)) {
                        node.removeAttribute('target');
                    } else if (node.nodeName === 'BASE' && node.getAttribute('target') === '_blank') {
                        node.parentNode.removeChild(node);
                    } else {
                        removeBlankTargetIfNecessary(node);
                    }
                }
            }
        }
    }

    const observer = new MutationObserver(observeChanges);
    observer.observe(document, {attributes: true, subtree: true, childList: true});
})();