MediaWiki:Common.js

来自萌娘共享
AnnAngela讨论 | 贡献2022年2月20日 (日) 11:48的版本
跳转至: 导航搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
$(function() {
    var body = document.body;
    var html = document.documentElement;
    var $body = $(body);
    var $window = $(window);
    /* 浮动滚动条 */
    $window.on("resize", function () {
        var innerWidth = window.innerWidth;
        var scrollbarWidth;
        switch ("scroll") {
            case getComputedStyle(body).overflowY:
                scrollbarWidth = innerWidth - body.clientWidth;
                break;
            case getComputedStyle(html).overflowY:
                scrollbarWidth = innerWidth - html.clientWidth;
                break;
            default:
                var backup = body.style.overflowY;
                body.style.overflowY = "scroll";
                scrollbarWidth = innerWidth - body.clientWidth;
                body.style.overflowY = backup;
        }
        $body[scrollbarWidth === 0 ? "addClass" : "removeClass"]("overlay-scrollbars");
    }).trigger("resize");
    if ($(".mw-special-ListDuplicatedFiles")[0] && $('.mw-spcontent > p')[1]) {
        if (window.location.href.indexOf("title") == -1) window.location.replace(mw.config.get("wgServer") + mw.config.get("wgScriptPath") + "/index.php?title=Special:ListDuplicatedFiles&limit=500&offset=0");
        $('#mw-content-text .mw-spcontent ol').attr("id", "picOl");
        $('#picOl').before('<p id="picOlTitle">图片列表:</p>').after('<p id="MP3OlTitle">MP3列表:</p><ol start="1" id="MP3Ol"></ol>');
        $("#picOlTitle, #MP3OlTitle").hide();
        $("#picOl").find("[href$='.mp3'], [href$='.ogg']").parent().appendTo("#MP3Ol");
        if ($("#picOl").find("li").length) $("#picOlTitle").show();
        if ($("#MP3Ol").find("li").length) $("#MP3OlTitle").show();
        $(".mw-spcontent > p:nth-child(1), .mw-spcontent > p:nth-child(2), .mw-spcontent > p:nth-child(7)").hide();
        if (mw.config.get('wgUserGroups').indexOf('sysop') !== -1) {
            if (mw.config.get('mgDuplicateFilesClean')) mw.config.get('mgDuplicateFilesClean')();
            else mw.config.set('mgDuplicateFilesClean', true);
        }
    }
    var allowedInNamespace = [-1, -2, 6, 14, 8, 10, 12, 828].includes(mw.config.get("wgNamespaceNumber"));
    var allowedGroups = ["sysop", "patroller"];
    var allowedInGroup = mw.config.get("wgUserGroups").filter(function(group) { return allowedGroups.includes(group); }).length > 0;
    // 禁止编辑相关
    function editSet() {
        mw.loader.using(["mediawiki.util"]).then(function() {
            if (/\.(?:js|css)$/.test(mw.util.getParamValue("title") || location.pathname)) {
                return;
            }
            if ([2, 3].includes(mw.config.get("wgNamespaceNumber")) && mw.config.get("wgAction") === "edit" && mw.config.get("wgArticleId") === 0) {
                location.host = location.host.replace("commons.moegirl", "zh.moegirl");
                return;
            }
            if (mw.config.get("wgArticleId") === 0 && !allowedInNamespace && !allowedInGroup) {
                $('a[href*="veaction=edit"]').remove();
                if ($('[class*="ve-init"]').length > 0) {
                    $window.off();
                    location.replace("/" + mw.config.get("wgPageName"));
                }
                $('#wikiEditor-ui-toolbar, #wpSummaryLabel, #wpSummaryWidget, [name="wpSummary"], .editCheckboxes, #wpMinoreditWidget, [name="wpMinoredit"], #wpWatchthisWidget, [name="wpWatchthis"], #editpage-copywarn, #wpSaveWidget, [name="wpSave"], #wpPreviewWidget, [name="wpPreview"], #wpDiffWidget, [name="wpDiff"], [name="wpEditToken"]').remove();
                $(".editOptions").prepend('<p>本站仅供上传图片用,请勿在本站创建页面,如有需要请到<a target="_blank" rel="nofollow noreferrer noopener" class="external text" href="' + mw.config.get("wgServer").replace("commons.moegirl", "zh.moegirl") + mw.config.get("wgScriptPath") + '">主站</a>。</p>');
                $('#editform, [name="editform"]').each(function(_, ele) {
                    $(ele).removeAttr("action");
                    ele.addEventListener("submit", function(e) {
                        e.preventDefault();
                    }, {
                        capture: true
                    });
                });
            }
        });
    }
    editSet();
    $window.on('load', function() {
        editSet();
        if (new mw.Uri(document.referrer).toString().startsWith(mw.config.get("wgServer").replace("commons.moegirl", "zh.moegirl") + mw.config.get("wgScriptPath") + "/")) $("#back").removeAttr('style').on("click", function() {
            if (history.length > 1) history.go(-1);
            else location.href = document.referrer;
        });
        if ($(".fileInfo")[0])
            $(".mw-ui-button-group.mw-mmv-filepage-buttons:first").prepend($("<a/>", {
                class: "mw-mmv-view-expanded mw-ui-button mw-ui-icon",
                text: "在萌娘百科上查看文件说明页"
            }).on("click", function() {
                window.open(mw.config.get("wgServer").replace("commons.moegirl", "zh.moegirl") + mw.config.get("wgScriptPath") + location.pathname + location.search, "_blank");
            }));
        $('#mw-content-text a').each(function() {
            try {
                var link = new mw.Uri(this.href);
                if (link.toString().startsWith(mw.config.get("wgServer") + mw.config.get("wgScriptPath") + "/")) return;
                if (/^\/api\.php/i.test(link.path)) return;
                if (!link.query.title && /\.php$/i.test(link.path)) return;
                if (link.query && (link.query.action || link.query.diff)) return;
                var href = ((link.query.title || decodeURI(link.path.substring(1))).match(/^user(?:[ _]talk)?:[^\/]+/i) || [null])[0];
                if (href) $(this).after('<sub>[<a target="_blank" title="主站上的用户 ' + href.replace(/user(_talk)?\:/i, '') + '" href="' + mw.config.get("wgServer").replace("commons.moegirl", "zh.moegirl") + mw.config.get("wgScriptPath") + '/' + href + '">主</a>]</sub>');
            } catch (e) {
                return;
            }
        });
    });
    //修复代码编辑器$.ucFirst引用错误
    jQuery.extend({
        ucFirst: function(s) {
            s += '';
            return s.charAt(0).toUpperCase() + s.substring(1);
        }
    });
    if (mw.config.get("wgCanonicalSpecialPageName") === "Upload") {
        var wpLicense = $("#wpLicense");
        wpLicense.val("Copyright").change();
        $("#mw-upload-form").on("submit", function() {
            if (wpLicense.val() === "Authorized") {
                localStorage.setItem("AnnTools-license-authorized-" + encodeURIComponent("File:" + $("#wpDestFile").val().replace(/ /g, "_")), "true");
            }
        });
    } else if (mw.config.get("wgAction") === "edit" && mw.util.getParamValue("authorized") === "1") {
        var wpTextbox1 = $("#wpTextbox1");
        wpTextbox1.val(wpTextbox1.val().replace("{{Authorized}}", "{{Authorized\n|作者名=<!--适用于作者授权百科长期使用其作品的情形,此时直接填入作者名即可-->\n|授权证明=<!--适用于作者仅授权单张图片的情形,可以在此填写授权证明的文件链接、或是相关请求授权的讨论链接等-->\n}}"));
        setTimeout(function() { alert("请在编辑框内填入相关授权信息!"); }, 0);
    } else {
        var wgPageName = encodeURIComponent((mw.config.get("wgPageName") || "").replace(/ /g, "_"));
        if (localStorage.getItem("AnnTools-license-authorized-" + wgPageName) === "true") {
            localStorage.removeItem("AnnTools-license-authorized-" + wgPageName);
            location.replace(mw.config.get("wgServer") + mw.config.get("wgScriptPath") + "/index.php?title=" + wgPageName + "&action=edit&authorized=1");
        }
    }
    if (mw.config.get("wgNamespaceNumber") === 6 && (mw.config.get("wgArticleId") || -1) > 0 && mw.config.get("wgAction") === "view" && (mw.config.get("wgRevisionId") || -1) === (mw.config.get("wgCurRevisionId") || -2) && !document.querySelector(".license-info")) {
        if (document.querySelector(".common-box") && mw.config.get("wgUserId", -1) > 0) {
            mw.loader.using("mw.Api").then(function() {
                return new mw.Api().post({
                    action: "purge",
                    pageids: mw.config.get("wgArticleId", -1)
                });
            });
        } else {
            var editbutton = document.querySelector("#ca-edit a");
            var loginbutton = document.querySelector("#pt-login a");
            $("#mw-imagepage-content, #mw-imagepage-content .mw-parser-output").last().append('<table class="common-box license-info" style="margin: 0.5em 10%; width:80%; background: #FBFBFB; border-left: 10px solid Salmon;"><tbody><tr><td style="padding: 2px 0 2px 0.5em"><img alt="Red copyright.svg" src="https://img.moegirl.org.cn/common/1/1d/Red_copyright.svg" width="50" height="50"></td><td style="padding: 0.25em 0.5em"><div><span style="font-weight: bold;">由于上传者未填写授权协议</span>,本作品默认仅以介绍为目的在此百科中以非盈利性方式合理使用。</div>' + ((loginbutton || editbutton) ? '<div>您知道该文件的作者是如何授权的吗?如果您知道的话欢迎<span style="font-weight: bold;">' + (editbutton ? '<a href="' + editbutton.href + '">编辑该页面</a>' : '<a href="' + mw.config.get("wgServer") + mw.config.get("wgScriptPath") + '/index.php?title=Special:%E7%94%A8%E6%88%B7%E7%99%BB%E5%BD%95&returntoquery=action%3Dedit&returnto=' + encodeURIComponent(mw.config.get("wgPageName")) + '">登录后编辑该页面</a>') + "</span>填写授权协议~~</div>" : "") + '</td></tr></tbody></table>');
        }
    }
});