User:Gadget-T265571 fixer.js

/** * Simple replace-on-load for fixing T265771 while an upstream fix is pending * * Replaces a predefined set of HTML entity codes with the literal character, * only in the Page: namespace * * Remove this gadget when T265571 is fixed and deployed * * 2020-10-16: Version 1.0: Initial version *            Version 1.1: Add check to only apply on newly created pages *                         to avoid changing deliberate HTML codes */

(function($, mw) {

"use strict";

var gadget_name = "t265571_fixer"; var gadget_v = "1.1";

var T265571State = { enabled: true, namespaces: ["Page"], entity_replacements: [ [/&#9;/g, '\t'], [/&#10;/g, '\n'], [/&#32;/g, ' '], [/&#34;/g, '"'],       [/&#35;/g, '#'],        [/&#38;/g, '&'],        [/&#39;/g, "'"],        [/&#42;/g, "*"],        [/&#45;/g, "-"],        [/&#58;/g, ":"],        [/&#59;/g, ";"],        [/&#60;/g, "<"],        [/&#61;/g, "="],        [/&#62;/g, ">"],        [/&#61;/g, "["],        [/&#93;/g, "]"],        [/&#95;/g, "_"],        [/&#123;/g, "{"],        [/&#124;/g, "|"],        [/&#125;/g, "}"],    ], };

/* * Run the list of replacements */ function t265571_replacements(editbox) {

console.log(gadget_name + " " + gadget_v + ": running replacements");

var content = editbox.value;

for (var i = 0; i < T265571State.entity_replacements.length; i++) {

content = content.replace(           T265571State.entity_replacements[i][0], T265571State.entity_replacements[i][1]); }

editbox.value = content; }

/* * Run setup, apply any load functions */ function t265571_on_load {

var editbox = document.getElementById("wpTextbox1");

if (editbox) { t265571_replacements(editbox); } }

function t265571_setup {

if (!T265571State.enabled || !T265571State.namespaces.includes( mw.config.get("wgCanonicalNamespace"))           || mw.config.get("wgCurRevisionId") !== 0) { return; }

if ($.inArray(mw.config.get("wgAction"), ["edit", "submit"]) !== -1) { mw.loader.using("ext.proofreadpage.page", function {            // mimic code in the extension, there is a conditionnal deps on ext.wikiEditor.            if (mw.user.options.get("usebetatoolbar") && $.inArray("ext.wikiEditor", mw.loader.getModuleNames) > -1) {               var load_deps = ["ext.wikiEditor"];                if ( mw.user.options.get( "codemirror-syntax-highlight" ) == 1 ) {                    load_deps.push("ext.CodeMirror.lib");                }                mw.loader.using(load_deps, function { t265571_on_load; });           } else {                t265571_on_load;            }        }); } } $(t265571_setup);

}(jQuery, mediaWiki));