Disable blank target

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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});
})();