Imgur: fix HTML escape sequences in the old design

Imgur's "old design" sometimes includes HTML escape sequences in post descriptions, such as having """ and "'" instead of quotation marks. This replaces them with the appropriate characters.

目前為 2023-12-03 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Imgur: fix HTML escape sequences in the old design
// @namespace    http://tampermonkey.net/
// @version      1.1.0
// @description  Imgur's "old design" sometimes includes HTML escape sequences in post descriptions, such as having """ and "'" instead of quotation marks. This replaces them with the appropriate characters.
// @author       Corrodias
// @match        https://imgur.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=imgur.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const mutationObserver = new MutationObserver(mutations => {
        for (const mutation of mutations) {
            processMutation(mutation);
        }
    });

    function processMutation(mutation) {
        for (const node of mutation.addedNodes) {
            if (node.nodeType !== Node.ELEMENT_NODE) continue;
            // Only act if a new post has been loaded.
            if (!node.classList.contains('post-image-container')) return;
            replaceText(node);
        }
    }

    function replaceText(post) {
        // This is empty if the current page is not a post.
        const description = post.getElementsByClassName('post-image-description');
        for (const a of description) {
            a.textContent = a.textContent
                .replaceAll('"', '"')
                .replaceAll(''', "'")
                .replaceAll('>', '>')
                .replaceAll('&lt;', '<')
                .replaceAll('&amp;', '&');
        }
    }

    // Monitor dynamically loaded content, for when the user nagivates to a new post. Observe as little as possible.
    const postContent = document.body.getElementsByClassName('post-images');
    for (const a of postContent) {
        mutationObserver.observe(a, { attributes: false, childList: true, subtree: true });
    }

    // Also run the replacement now in case this is a post.
    replaceText(document.body);
})();