去除知乎外部链接拦截

知乎所有外部链接在点击跳转时,会被拦截,并进行二次确认后才能跳转,这个脚本的就是清除所有针对外链的拦截

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         去除知乎外部链接拦截
// @namespace    http://tampermonkey.net/
// @version      v1.2
// @description  知乎所有外部链接在点击跳转时,会被拦截,并进行二次确认后才能跳转,这个脚本的就是清除所有针对外链的拦截
// @author       Buddha
// @match        https://*.zhihu.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zhihu.com
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const prefixUrl = /^https?:\/\/link\.zhihu\.com\/\?target=/;
    const replaceUrl = (node)=>{
        if (prefixUrl.test(node.href)) {
            node.href = decodeURIComponent(node.href.replace(prefixUrl, ''));
        }
    };

    const mutationFun = (node)=>{
        if (!node) return;
		const observer = new MutationObserver(mutations=>{
			mutations.filter(m=>m.type==='childList').forEach(mutation=>{
				Array.from(mutation.addedNodes).forEach(item=>mutationFun(item));
				node.querySelectorAll('a[class]').forEach(m=>replaceUrl(m));
			});
		});
        const option = {childList: true, characterData: true, subtree: true};
		observer.observe(node, option);
    };

    /* 适用于知乎首页、个人主页、问题页 动态加载 */
    mutationFun(document.querySelector('.ListShortcut'));

    /* 针对文章详情页 */
    document.querySelectorAll('.RichText a').forEach(link =>replaceUrl(link));

    /* 针对文章详情页 评论区 延时加载 */
    mutationFun(document.querySelector('.Post-Sub'));
})();