Old Reddit Broken Link Fixer

Fixes incorrectly escaped characters in links on Old Reddit

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Old Reddit Broken Link Fixer
// @namespace    https://github.com/whqwert/userscripts
// @version      1.2.8
// @description  Fixes incorrectly escaped characters in links on Old Reddit
// @author       whqwert
// @match        https://*.reddit.com/*/*/comments/*
// @icon         https://www.reddit.com/favicon.ico
// @supportURL   https://github.com/whqwert/userscripts/issues
// @license      MIT
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function () {
	'use strict';

	// Fix links on load
	new MutationObserver((_, observer) => {
		// Wait for comments to exist
		if (!document.getElementsByClassName('commentarea').length) return;

		runLinkFixer(observer);
		addMoreButtonListener();
	}).observe(document, {
		childList: true,
		subtree: true
	});

	function runLinkFixer(observer) {
		observer.disconnect();
		fixAllLinks();
	}

	// For link with backslash, remove backslashes in link
	function fixAllLinks() {
		for (const link of document.querySelectorAll('.thing a[href*="%5C"]')) {
			removeBackslashes(link);
		}
	}

	// Not sure if quotes or dashes are incorrectly escaped anymore,
	// but I've seen a few old posts/comments where they are
	function removeBackslashes(a) {
		a.href = a.href.replace(/%5C([-_"])/g, '$1');
		a.innerText = a.innerText.replace(/\\([-_"])/g, '$1');
	}

	// Hijack 'load more comments' button onclick to run fixer
	function addMoreButtonListener() {
		window.morechildren = (function () {
			const _morechildren = window.morechildren;
			return function () {
				const result = _morechildren.apply(this, arguments);
				observeNewComments();
				return result;
			};
		})();
	}

	// When the 'load more comments' button is pressed,
	// it waits for new comments to load and then runs the link fixer
	const commentObserver = new MutationObserver((_, observer) => {
		runLinkFixer(observer);
	});
	function observeNewComments() {
		commentObserver.observe(document.querySelector('.commentarea > .sitetable'), {
			childList: true,
			subtree: true
		});
	}
})();