Jump to content

MediaWiki:Common.js: Difference between revisions

From NFF
No edit summary
Tag: Manual revert
Wiki redesign: Play now button, footer, search results; upload tool loads from MediaWiki:MultiUpload.js
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using("mediawiki.util").then(function(){var e="https://final-fight.com/";mw.util.addPortletLink("p-navigation",e,"Play Final Fight","n-play-now","Go to the Final Fight website");var i=document.querySelector(".vector-header-end");if(i&&!document.querySelector(".ffw-play")){var t=document.createElement("a");t.className="ffw-play",t.href=e,t.title="Go to the Final Fight website",t.innerHTML='<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M7 4.5v15l12-7.5z"/></svg>Play now',i.insertBefore(t,i.firstChild)}var n=document.getElementById("footer");if(n&&!document.querySelector(".ffw-footer")){var o=[["Play now",e],["Discord","https://discord.gg/YZ7f5tJ"],["YouTube","https://www.youtube.com/@finalfight4099"],["Twitch","https://www.twitch.tv/final_fight_streamer"]],a=document.createElement("div");a.className="ffw-footer",a.innerHTML='<a class="ffw-footer-brand" href="'+e+'">Final Fight</a><nav>'+o.map(function(r){return'<a href="'+r[1]+'">'+r[0]+"</a>"}).join("")+"</nav>",n.appendChild(a)}}),mw.config.get("wgCanonicalSpecialPageName")==="Search"&&document.querySelectorAll(".searchresult").forEach(function(e){var i="";e.childNodes.forEach(function(n){i+=n.nodeType===1?""+n.textContent+"":n.nodeValue}),i=i.replace(/\n\.\.\.\s*$/," \u2026").replace(/\{\{[^}]*(\}\}|$)|\}\}|<\/?[a-z][^>]*>|\[\[(?:[^|\]]*\|)?|\]\]|'{2,}|={2,}|__[A-Z]+__/gi," ").replace(/\|\s*[\w \u0001\u0002]+?\s*=\s*/g,`
 
`).replace(/(^|\n)\s*[*#:]+\s*/g,"$1").replace(/\s*\n[\s\n]*/g," \xB7 ").replace(/ {2,}/g," ").replace(/^[\s\u00b7]+|[\s\u00b7]+$/g,""),e.textContent="";var t=null;i.split(/([\u0001\u0002])/).forEach(function(n){n===""?t||(t=e.appendChild(Object.assign(document.createElement("span"),{className:"searchmatch"}))):n===""?t=null:n&&(t||e).appendChild(document.createTextNode(n))})}),mw.config.get("wgCanonicalSpecialPageName")==="Upload"&&mw.loader.load(mw.config.get("wgScript")+"?title=MediaWiki:MultiUpload.js&action=raw&ctype=text/javascript");var discordOnline=document.querySelector(".ffw-discord-online");discordOnline&&fetch("https://discord.com/api/guilds/715225351055474719/widget.json").then(function(e){return e.json()}).then(function(e){if(!e||typeof e.presence_count!="number")throw new Error("no count");discordOnline.textContent=e.presence_count.toLocaleString("en-US")}).catch(function(){discordOnline.parentNode.remove()}),mw.config.get("wgCanonicalSpecialPageName")==="Search"&&mw.loader.using("mediawiki.api").then(function(){var e={};document.querySelectorAll(".mw-search-result").forEach(function(t){var n=t.querySelector(".mw-search-result-heading a");if(!(!n||t.querySelector(".ffw-sr-icon"))){var o=document.createElement("div");for(o.className="ffw-sr-body";t.firstChild;)o.appendChild(t.firstChild);var a=document.createElement("span");a.className="ffw-sr-icon",t.appendChild(a),t.appendChild(o),(e[n.title]=e[n.title]||[]).push(a)}});var i=Object.keys(e);i.length&&new mw.Api().get({action:"query",prop:"pageimages",piprop:"thumbnail",pithumbsize:96,pilimit:50,titles:i.join("|"),formatversion:2}).then(function(t){(t.query.pages||[]).forEach(function(n){n.thumbnail&&(e[n.title]||[]).forEach(function(o){var a=document.createElement("img");a.src=n.thumbnail.source,a.alt="",o.appendChild(a),o.classList.add("ffw-sr-has-icon")})})})});
mw.config.set('UMFBypassLicenseCheck', true);
 
mw.loader.using(["site", "mediawiki.util"]).then(function() {
    if (mw.config.get("wgCanonicalSpecialPageName") !== "Upload")
        return;
    var l10nFactory = l10nFactory || function($lang, $data) {
        return function ($key) {
            // (null == undefined) is true, (null === undefined) is false
            return ($data[$lang] && $data[$lang][$key] != null) ? $data[$lang][$key] : $data.en[$key];
        };
    };
    const l10n = l10nFactory(mw.config.get("wgUserLanguage"), {
        en: {
            multiupload: "Upload multiple files:",
            yes: "Yes",
            no: "No",
            sourcefiles: "Source files:",
            categoryname: "Category name:",
            categorynamehint: "Page name of the category the files will automatically be added to, e.g. Item images",
            categorynamespace: "Category",
            uploadfiles: "Upload files",
            nofiles: "Please select some files first.",
            nolicense: "Please select a valid license first.",
            summary: "Summary",
            license: "Licensing",
            categories: "Categories",
            uploading: "Uploading files...",
            uploaded: "Uploaded:",
            failed: "Failed:",
            done: "Done."
        }
    });
    const getUploadDescription = function() {
        var sections = [];
        var summary = $("#wpUploadDescription").val();
        var licenseDisplayName = $("#wpLicense option:selected").val();
        var categoryName = $("#multiFileCategory").val();
        if (summary !== "")
            sections.push("== " + l10n("summary") + " ==\n" + summary);
        if (licenseDisplayName !== "") {
            var licenseTemplateText = $("#wpLicense option:selected").prop("title");
            sections.push("== " + l10n("license") + " ==\n" + licenseTemplateText);
        }
        if (categoryName !== "")
            sections.push("[[" + l10n("categorynamespace") + ":" + categoryName + "]]");
        return sections.join("\n\n");
    };
    const getWatchlistPreference = function() {
        if ($("#wpWatchthis").is(":checked")) {
            return "watch";
        } else {
            return "nochange";
        }
    };
   
    $(function(){
        $("#wpUploadFile").parent().parent().addClass("regularFileSelect");
        $("tr.regularFileSelect").before('<tr><td class="mw-label">'+ l10n("multiupload") + '</td><td class="mw-input"><label><input type="radio" name="multipleFiles" value="'+ l10n("yes") + '" /> '+ l10n("yes") + '</label> &nbsp; <label><input type="radio" name="multipleFiles" value="'+ l10n("no") + '" checked="" /> '+ l10n("no") + '</label></td></tr>');
        $("tr.regularFileSelect").after('<tr class="multipleFileSelect" style="display:none;"><td class="mw-label">' + l10n("sourcefiles") + '</td><td class="mw-input"><input type="file" id="multiupload" multiple /></td></tr>');
        $("input[name='wpUpload']").addClass("regularFileSelect");
        $("#wpDestFile").parent().parent().addClass("regularFileSelect");
        $("#wpIgnoreWarning").parent().parent().addClass("regularFileSelect");
        // Append "category name" textbox to multiupload section
        $("#mw-upload-form #mw-htmlform-description tbody").append('<tr class="mw-htmlform-field-HTMLTextField multipleFileSelect" style="display:none;"><td class="mw-label"><label for="multiFileCategory"><abbr title="' + l10n("categorynamehint") + '">' + l10n("categoryname") + '</abbr></label></td><td class="mw-input"><input id="multiFileCategory" name="multiFileCategory" size="60"></td></tr>');
        $("input[name='wpUpload']").after('<input type="button" value="' + l10n("uploadfiles") + '" class="multipleFileSelect" style="display:none;" id="multiFileSubmit" />');
        $("input[name='multipleFiles']").change(function() {
            if (this.value === l10n("yes")) {
                $(".regularFileSelect").hide();
                $(".multipleFileSelect").show();
            } else {
                $(".regularFileSelect").show();
                $(".multipleFileSelect").hide();
            }
        });
        $("#multiFileSubmit").click(function() {
            var files = $("#multiupload")[0].files;
            // Cancel upload if no files are selected
            if (files.length === 0) {
                alert(l10n("nofiles"));
                return false;
            }
            // Cancel upload if no license is selected
            if ($("#wpLicense option:selected").val() === "" && !mw.config.get("UMFBypassLicenseCheck")) {
                alert(l10n("nolicense"));
                return false;
            }
            // Description is summary + license + category
            var description = getUploadDescription();
            var watch = getWatchlistPreference();
            $("#firstHeading").text(l10n("uploading"));
            $("#mw-content-text").html("<h3>" + l10n("uploaded") + "</h3><ul></ul><div style='display:none;' id='multiUploadFailed'><h3>" + l10n("failed") + "</h3><ul></ul></div>");
           
            var currentFileIdx = 0;
            var uploadCurrentFile;
            uploadCurrentFile = function() {
                if (currentFileIdx > files.length) {
                    $("#mw-content-text").append("<h3>"+ l10n("done") + "</h3>");
                    return;
                }
                if (files[currentFileIdx] === undefined) {
                    currentFileIdx++;
                    uploadCurrentFile();
                    return;
                }
                fd = new FormData();
                fd.append("action", "upload");
                fd.append("token", mw.user.tokens.get("csrfToken"));
                fd.append("filename", files[currentFileIdx].name);
                fd.append("file", files[currentFileIdx]);
                fd.append("text", description);
                fd.append("watchlist", watch);
                fd.append("ignorewarnings", 1);
                fd.append("format", "json");
               
                $.ajax({
                    url: mw.util.wikiScript("api"),
                    method: "POST",
                    data: fd,
                    cache: false,
                    contentType: false,
                    processData: false,
                    type: "POST"
                }).done(function(d) {
                    if (d.error == undefined) {
                        $("#mw-content-text > ul").append('<li><a href="' + d.upload.imageinfo.descriptionurl + '" target="_blank">' + d.upload.filename + '</a></li>');
                    } else {
                        $("#multiUploadFailed ul").append('<li>' + files[currentFileIdx].name + '</li>');
                        $("#multiUploadFailed").show();
                    }
                    currentFileIdx++;
                    uploadCurrentFile();
                }).fail(function() {
                    $("#multiUploadFailed ul").append('<li>' + files[currentFileIdx].name + '</li>');
                    $("#multiUploadFailed").show();
                    currentFileIdx++;
                    uploadCurrentFile();
                });
            };
            uploadCurrentFile();
        });
    });
});

Revision as of 02:55, 3 October 2026

mw.loader.using("mediawiki.util").then(function(){var e="https://final-fight.com/";mw.util.addPortletLink("p-navigation",e,"Play Final Fight","n-play-now","Go to the Final Fight website");var i=document.querySelector(".vector-header-end");if(i&&!document.querySelector(".ffw-play")){var t=document.createElement("a");t.className="ffw-play",t.href=e,t.title="Go to the Final Fight website",t.innerHTML='<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M7 4.5v15l12-7.5z"/></svg>Play now',i.insertBefore(t,i.firstChild)}var n=document.getElementById("footer");if(n&&!document.querySelector(".ffw-footer")){var o=[["Play now",e],["Discord","https://discord.gg/YZ7f5tJ"],["YouTube","https://www.youtube.com/@finalfight4099"],["Twitch","https://www.twitch.tv/final_fight_streamer"]],a=document.createElement("div");a.className="ffw-footer",a.innerHTML='<a class="ffw-footer-brand" href="'+e+'">Final Fight</a><nav>'+o.map(function(r){return'<a href="'+r[1]+'">'+r[0]+"</a>"}).join("")+"</nav>",n.appendChild(a)}}),mw.config.get("wgCanonicalSpecialPageName")==="Search"&&document.querySelectorAll(".searchresult").forEach(function(e){var i="";e.childNodes.forEach(function(n){i+=n.nodeType===1?""+n.textContent+"":n.nodeValue}),i=i.replace(/\n\.\.\.\s*$/," \u2026").replace(/\{\{[^}]*(\}\}|$)|\}\}|<\/?[a-z][^>]*>|\[\[(?:[^|\]]*\|)?|\]\]|'{2,}|={2,}|__[A-Z]+__/gi," ").replace(/\|\s*[\w \u0001\u0002]+?\s*=\s*/g,`
`).replace(/(^|\n)\s*[*#:]+\s*/g,"$1").replace(/\s*\n[\s\n]*/g," \xB7 ").replace(/ {2,}/g," ").replace(/^[\s\u00b7]+|[\s\u00b7]+$/g,""),e.textContent="";var t=null;i.split(/([\u0001\u0002])/).forEach(function(n){n===""?t||(t=e.appendChild(Object.assign(document.createElement("span"),{className:"searchmatch"}))):n===""?t=null:n&&(t||e).appendChild(document.createTextNode(n))})}),mw.config.get("wgCanonicalSpecialPageName")==="Upload"&&mw.loader.load(mw.config.get("wgScript")+"?title=MediaWiki:MultiUpload.js&action=raw&ctype=text/javascript");var discordOnline=document.querySelector(".ffw-discord-online");discordOnline&&fetch("https://discord.com/api/guilds/715225351055474719/widget.json").then(function(e){return e.json()}).then(function(e){if(!e||typeof e.presence_count!="number")throw new Error("no count");discordOnline.textContent=e.presence_count.toLocaleString("en-US")}).catch(function(){discordOnline.parentNode.remove()}),mw.config.get("wgCanonicalSpecialPageName")==="Search"&&mw.loader.using("mediawiki.api").then(function(){var e={};document.querySelectorAll(".mw-search-result").forEach(function(t){var n=t.querySelector(".mw-search-result-heading a");if(!(!n||t.querySelector(".ffw-sr-icon"))){var o=document.createElement("div");for(o.className="ffw-sr-body";t.firstChild;)o.appendChild(t.firstChild);var a=document.createElement("span");a.className="ffw-sr-icon",t.appendChild(a),t.appendChild(o),(e[n.title]=e[n.title]||[]).push(a)}});var i=Object.keys(e);i.length&&new mw.Api().get({action:"query",prop:"pageimages",piprop:"thumbnail",pithumbsize:96,pilimit:50,titles:i.join("|"),formatversion:2}).then(function(t){(t.query.pages||[]).forEach(function(n){n.thumbnail&&(e[n.title]||[]).forEach(function(o){var a=document.createElement("img");a.src=n.thumbnail.source,a.alt="",o.appendChild(a),o.classList.add("ffw-sr-has-icon")})})})});