Techdirt?Fixer

Fix *some* special characters appearing as "?" on older Techdirt articles. As it is a simple script, expect false negatives and false positives.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Techdirt?Fixer
// @namespace    http://wxw.moe/@dia
// @version      0.1.1
// @description  Fix *some* special characters appearing as "?" on older Techdirt articles. As it is a simple script, expect false negatives and false positives.
// @author       Dia
// @match        https://www.techdirt.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @run-at       document-end
// @license      Unlicense
// ==/UserScript==

//manually change these parameters to suit your needs
const USCRIPT_DEBUG_MODE = false; //set to true for useful console logs
const DEBUG_HIGHLIGHT_1 = "<span style=\"color:red\">";
const DEBUG_HIGHLIGHT_2 = "</span>";

const NEG_AUX_VERB = /n\?t(?![a-zA-Z0-9])/g;
const NEG_AUX_VERB_FIXED = "n't";

const LONE_DASH = /(?<=\S\s)\?(?=\s\S)/g;
const LONE_DASH_FIXED = "-";

const APHOS_S = /(?<=[a-zA-Z0-9])\?s(?=[^a-zA-Z0-9])/g;
const APHOS_S_FIXED = "'s";
const IM = /(?<=^|\s)I\?m(?=\s)/g;
const IM_FIXED = "I'm";
const RE_LL_D_VE = /(?<=^|\s)([a-zA-Z0-9]+)\?(re|ll|d|ve)(?=\s)/g;
const RE_LL_D_VE_FIXED = "$1'$2";
const QUOTES = /(?<=^|[\s\(])\?([^\s][^\?]*)\?(?=[\s\)]?)/g;
const QUOTES_FIXED = "\"$1\"";
const QUOTES_IN_LINK = /(<a\shref\=[\"\'][^\"\']*[\"\']>)\?([^\s][^\?]*)\?(<\/a>)/g;
const QUOTES_IN_LINK_FIXED = "$1\"$2\"$3";
const S_APHOS = /s\?(?=\s[a-z])/g; //This cannot catch s' that is immediately followed by a comma or a full stop.
const S_APHOS_FIXED = "s'";

(function() {
    'use strict';
    const postBodyElms = document.getElementsByClassName("postbody");

    let newNegAuxVerb = colorDebug(NEG_AUX_VERB_FIXED);
    let newLoneDash = colorDebug(LONE_DASH_FIXED);
    let newAphosS = colorDebug(APHOS_S_FIXED);
    let newIm = colorDebug(IM_FIXED);
    let newReLlDVe = colorDebug(RE_LL_D_VE_FIXED);
    let newQuotes = colorDebug(QUOTES_FIXED);
    let newQuotesInLink = colorDebug(QUOTES_IN_LINK_FIXED);
    let newSAphos = colorDebug(S_APHOS_FIXED);

    if (USCRIPT_DEBUG_MODE) console.log("Techdirt?Fixer runs in debugging mode");

    for (let i=0; i < postBodyElms.length; i++){
        let postBodyContent = postBodyElms[i].innerHTML;
        postBodyContent = postBodyContent.replace(NEG_AUX_VERB,newNegAuxVerb);
        postBodyContent = postBodyContent.replace(LONE_DASH,newLoneDash);
        postBodyContent = postBodyContent.replace(APHOS_S,newAphosS);
        postBodyContent = postBodyContent.replace(IM,newIm);
        postBodyContent = postBodyContent.replace(RE_LL_D_VE,newReLlDVe);
        postBodyContent = postBodyContent.replace(QUOTES,newQuotes);
        postBodyContent = postBodyContent.replace(QUOTES_IN_LINK,newQuotesInLink);
        postBodyContent = postBodyContent.replace(S_APHOS,newSAphos);
        postBodyElms[i].innerHTML = postBodyContent;
    }

    function colorDebug(message){
        if (USCRIPT_DEBUG_MODE){
            return DEBUG_HIGHLIGHT_1 + message + DEBUG_HIGHLIGHT_2;
        }
        else return message;
    }

})();