Old Reddit Broken Link Fixer

Fixes escaped characters in links on Old Reddit

目前為 2022-07-11 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Old Reddit Broken Link Fixer
// @namespace    https://github.com/whqwert/userscripts
// @version      1.2.5
// @description  Fixes 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
// @run-at       document-start
// ==/UserScript==

(function () {
	'use strict';

	/*
	Function that removes the backslashes from links

	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');
	}

	// For link in comments and post, remove backslashes in link
	function fixAllLinks() {
		document
			.querySelectorAll(
				`#siteTable > .thing > .entry > .expando > form > .usertext-body > .md a[href*="%5C"],
				.commentarea > .sitetable > .thing a[href*="%5C"]`
			)
			.forEach(a => {
				removeBackslashes(a);
			});
	}

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

	function addMoreButtonListener() {
		// 'load more comments' button
		const more = document.querySelector('a[id^="more_"]');
		if (more) {
			more.addEventListener('click', function () {
				observeNewComments();
			});
		}
	}

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

	/*
	Fix links on load

	Used with '@run-at document-start' to fix links as fast as possible
	*/
	new MutationObserver(function () {
		if (document.getElementsByClassName('commentarea').length) {
			runLinkFixer(this);
		}
	}).observe(document, {
		subtree: true,
		childList: true
	});
})();