MediaWiki:Common.js

    叶子和他的Vtuber们

    Note: After publishing, you may have to bypass your browser's cache to see the changes.

    • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
    • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
    • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
    • Opera: Press Ctrl-F5.
    /* Any JavaScript here will be loaded for all users on every page load. */
    /* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
    "use strict";
    (function ($, mw) { //使用匿名函数防止污染顶级变量
    	/* Scroll to top */
    	$(document.body).append('<div id="goTopBtn"><span>▲</span></div>');
    	$(window).scroll(function() {
    	    var sc = $(window).scrollTop();
    	    var rwidth = $(window).width()
    	    if (sc > 0) {
    	        $("#goTopBtn").css("display", "block");
    	        $("#goTopBtn").css("left", (rwidth - 36) + "px")
    	    } else {
    	        $("#goTopBtn").css("display", "none");
    	    }
    	})
    	$("#goTopBtn").click(function() {
    	    var sc = $(window).scrollTop();
    	    $('body,html').animate({
    	        scrollTop: 0
    	    }, 500);
    	})
        /* capture支持检测 */
        var captureSupported = false;
        try {
            var options = Object.defineProperty({}, "capture", {
                get: function () {
                    captureSupported = true;
                    return true;
                },
            });
            window.addEventListener("test", null, options);
        } catch (err) { /* */ }
        /* 函数定义体 */
        //Extension:MultimediaViewer的半透明化修改,用于保持背景文字处于原位,本应修改插件达成的,暂时先用JavaScript应急处理下
        function multimediaViewer() {
            var _scrollTo = window.scrollTo;
            var flag = location.hash !== "";
            window.scrollTo = function scrollTo(x_option, y) {
                if (flag) {
                    console.info("Prevent multimediaViewer called");
                } else {
                    if (y === undefined) { _scrollTo(x_option); }
                    else { _scrollTo(x_option, y); }
                }
            };
            setInterval(function () {
                $("a.image img[data-file-width], a.image img[data-file-height], .mw-mmv-filepage-buttons a.mw-mmv-view-expanded").not(".multimediaViewerScrollSet").each(function () {
                    this.addEventListener("click", function (e) {
                        if ($(e.target).closest(".TabLabelText")[0] || $(e.target).closest("a").closest(".mw-customtoggle")[0]) {
                            e.preventDefault();
                            e.stopPropagation();
                            e.stopImmediatePropagation();
                            return false;
                        }
                        flag = true;
                    }, captureSupported ? {
                        capture: true,
                    } : true);
                    $(this).addClass("multimediaViewerScrollSet");
                });
                if ($(".mw-mmv-close")[0]) {
                    if (mw.config.get("wgMultimediaViewer") !== "on") {
                        mw.config.set("wgMultimediaViewer", "on");
                        $(".mw-mmv-image").off("click")[0].addEventListener("click", function (e) {
                            if ($(e.target).is("img")) {
                                window.open($(e.target).attr("src").replace(/(img\.moegirl\.org\/common\/)thumb\/([a-z\d]+\/[a-z\d]+\/)([^/]+)\/\d+px-\3/i, "$1$2$3"), "_blank").focus();
                                e.preventDefault();
                                e.stopPropagation();
                                e.stopImmediatePropagation();
                                return false;
                            } else if ($(e.target).closest(".error-box")[0]) { return; }
                            $(".mw-mmv-close").click();
                        }, captureSupported ? {
                            capture: true,
                        } : true);
                    }
                } else if (mw.config.get("wgMultimediaViewer") === "on") {
                    flag = false;
                    mw.config.set("wgMultimediaViewer", "off");
                }
            }, 137);
        }
        //Tabs
        function tabs() {
            var defaultStyle = {
                purple: {
                    labelColor: " ", //anti check
                    labelBackgroundColor: "#9070c0",
                    labelBorderColor: "#b090e0 #7050a0 #9070c0 #b090e0",
                    labelPadding: ".2em .3em .2em .3em",
                    textBorderColor: "#9070c0",
                    textBackgroundColor: "#f0edf5",
                    textPadding: "1em",
                },
                green: {
                    labelColor: " ",
                    labelBackgroundColor: "#75c045",
                    labelBorderColor: "#90d060 #60b030 #75c045 #90d060",
                    labelPadding: ".2em .3em .2em .3em",
                    textBorderColor: "#75c045 #60b030 #60b030 #75c045",
                    textBackgroundColor: "#f5fffa",
                    textPadding: "1em",
                },
                red: {
                    labelColor: " ",
                    labelBackgroundColor: "#FF0000",
                    labelBorderColor: "#FF8888 #CC0000 #FF0000 #FF8888",
                    labelPadding: ".2em .3em .2em .3em",
                    textBorderColor: "#FF0000 #CC0000 #CC0000 #FF0000",
                    textBackgroundColor: "#fffafa",
                    textPadding: "1em",
                },
                blue: {
                    labelColor: " ",
                    labelBackgroundColor: "#5b8dd6",
                    labelBorderColor: "#88abde #3379de #5b8dd6 #88abde",
                    labelPadding: ".2em .3em .2em .3em",
                    textBackgroundColor: "#f0f8ff",
                    textBorderColor: "#5b8dd6 #3379de #3379de #5b8dd6",
                    textPadding: "1em",
                },
                yellow: {
                    labelColor: " ",
                    labelBackgroundColor: "#ffe147",
                    labelBorderColor: "#ffe977 #ffd813 #ffe147 #ffe977",
                    labelPadding: ".2em .3em .2em .3em",
                    textBackgroundColor: "#fffce8",
                    textBorderColor: "#ffe147 #ffd813 #ffd813 #ffe147",
                    textPadding: "1em",
                },
                orange: {
                    labelColor: " ",
                    labelBackgroundColor: "#ff9d42",
                    labelBorderColor: "#ffac5d #ff820e #ff9d42 #ffac5d",
                    labelPadding: ".2em .3em .2em .3em",
                    textBackgroundColor: "#ffeedd",
                    textBorderColor: "#ff9d42 #ff820e #ff820e #ff9d42",
                    textPadding: "1em",
                },
                black: {
                    labelColor: " ",
                    labelBackgroundColor: "#7f7f7f",
                    labelBorderColor: "#999999 #4c4c4c #7f7f7f #999999",
                    labelPadding: ".2em .3em .2em .3em",
                    textBackgroundColor: "#e5e5e5",
                    textBorderColor: "#7f7f7f #4c4c4c #4c4c4c #7f7f7f",
                    textPadding: "1em",
                },
            };
            $("body").addClass("tab");
            // A Class
            function StyleSheet() { }
            StyleSheet.prototype.getOwnPropertyNamesLength = function getOwnPropertyNamesLength() {
                return Object.getOwnPropertyNames(this).length;
            };
            String.prototype.toLowerFirstCase = function toLowerFirstCase() {
                return this[0].toLowerCase() + this.substring(1);
            };
            $(".Tabs").each(function () {
                if ($(this).children(".TabLabel")[0]) { return true; }
                var self = $(this),
                    classList = Array.from(this.classList).filter(function (n) { return n in defaultStyle; }),
                    data = $.extend({
                        labelPadding: null,
                        labelBorderColor: null,
                        labelColor: null,
                        labelBackgroundColor: $("#content").css("background-color"),
                        textPadding: null,
                        textBorderColor: null,
                        textBackgroundColor: null,
                        defaultTab: 1,
                    }, classList[0] ? defaultStyle[classList[0]] || {} : {}, this.dataset || {}),
                    tabLabel = self.append('<div class="TabLabel"></div>').children(".TabLabel"),
                    tabContent = self.append('<div class="TabContent"></div>').children(".TabContent"),
                    labelPadding = data.labelPadding,
                    labelColor = data.labelColor,
                    styleSheet = {
                        label: new StyleSheet(),
                        text: new StyleSheet(),
                    },
                    defaultTab = parseInt(data.defaultTab);
                self.children(".Tab").each(function () {
                    if ($(this).children(".TabLabelText").text().replace(/\s/g, "").length || $(this).children(".TabLabelText").children().length) {
                        $(this).children(".TabLabelText").appendTo(tabLabel);
                        $(this).children(".TabContentText").appendTo(self.children(".TabContent"));
                    }
                    $(this).remove();
                });
                if (isNaN(defaultTab) || defaultTab <= 0 || defaultTab > tabLabel.children(".TabLabelText").length) { defaultTab = 1; }
                tabLabel.children(".TabLabelText").on("click", function () {
                    var label = $(this);
                    label.addClass("selected").siblings().removeClass("selected").css({
                        "border-color": "#aaa",
                        "background-color": "inherit",
                    });
                    tabContent.children(".TabContentText").eq(tabLabel.children(".TabLabelText").index(label)).addClass("selected").siblings().removeClass("selected").removeAttr("style");
                    if (styleSheet.label.getOwnPropertyNamesLength()) { label.css(styleSheet.label); }
                    if (label.is(":visible")) { tabLabel.height(label.outerHeight(true)); }
                    else { tabLabel.removeAttr("style"); }
                }).eq(defaultTab - 1).click();
                if (labelPadding) { tabLabel.children(".TabLabelText").css("padding", labelPadding); }
                ["labelBorderColor", "labelBackgroundColor", "textPadding", "textBorderColor", "textBackgroundColor"].forEach(function (n) {
                    var target = /^label/.test(n) ? "label" : "text",
                        key = n.replace(target, "").toLowerFirstCase();
                    styleSheet[target][key] = data[n];
                });
                if (labelColor) { styleSheet.label.borderTopColor = labelColor; }
                else if (styleSheet.label.borderColor) { styleSheet.label.borderTopColor = "green"; }
                tabLabel.find(".selected").click();
                if (styleSheet.text.getOwnPropertyNamesLength()) { tabContent.css(styleSheet.text); }
                if (data.autoWidth === "yes") { self.css("display", "inline-block"); }
            });
        }
        //子页面页顶链接
        function subpage_links() {
            var links = mw.config.get("wgPageName").split("/"),
                link = "",
                subpages = $(".subpages")[0] ? $(".subpages").remove().empty().text("<") : $('<span class="subpages"/>').text("<"),
                length = links.length - 1,
                index;
            if (mw.config.get("wgPageName").indexOf("Special:移动页面") !== -1) { links.splice(0, 1); }
            for (index = 0; index < length; index++) {
                link += "/" + links[index];
                subpages.append($("<a/>", {
                    text: " " + links[index],
                    href: link,
                }));
                if (length - index > 1) { subpages.append(" |"); }
            }
            if (subpages.find("a")[0]) { $("#contentSub").prepend(subpages); }
        }
        /* 函数执行体 */
        $(function () {
            //Template:heimu在safari上的暴力workaround
            mw.loader.using("jquery.client", function () {
                if ($.client.profile().name === "safari") {
                    $(".heimu").on("click", function () {
                        $(this).toggleClass("off");
                    });
                }
            });
            var copyRightsNameSpaces = [ // 以下为允许添加版权声明的名字空间列表
                0, // (主)
                4, // 萌娘百科(感谢老冰,是你启发我让我开始了解网站运维,虽然你离开了萌娘,但是你永远在我心里)
                8, // MediaWiki
                10, // Template
                12, // Help
                614, // R18
            ];
            //Extension:MultimediaViewer的半透明化修改
            if ($("img[srcset][data-file-width][data-file-height]")[0]) { multimediaViewer(); }
            // Add "mainpage" class to the body element
            if (mw.config.get("wgMainPageTitle") === mw.config.get("wgPageName") && mw.config.get("wgAction") === "view") { $("body").addClass("mainpage"); }
            //重复文件列表区分图片和音频
            if ($(".mw-special-ListDuplicatedFiles").length && $(".mw-spcontent > p").length > 1) { duplicatedFileList(); }
            //需要时载入对应的 scripts
            if (mw.config.get("wgAction") === "edit" || mw.config.get("wgAction") === "submit" || mw.config.get("wgCanonicalSpecialPageName") === "Search") { mw.loader.load("/index.php?title=MediaWiki:Common.js/edit.js&action=raw&ctype=text/javascript"); }
            // Tabs执行
            if ($(".Tabs")[0]) { tabs(); }
            //子页面页顶链接
            if (mw.config.get("wgPageName").indexOf("/") !== -1 && (mw.config.get("wgNamespaceIds").special !== mw.config.get("wgNamespaceNumber") || mw.config.get("wgPageName").indexOf("Special:移动页面") !== -1) && !window._unsubpage) { subpage_links(); }
            //修复代码编辑器$.ucFirst引用错误
            jQuery.extend({
                ucFirst: function (_s) {
                    var s = _s + "";
                    return s.charAt(0).toUpperCase() + s.substring(1);
                },
            });
            //注释内列表
            $(".reference-text > ul,.reference-text > ol").each(function () {
                if (this.parentElement.childNodes[0] === this) { $(this).addClass("listInRef"); }
            });
            //列表侧边距
            setInterval(function () {
                $(".mw-parser-output ul, .mw-parser-output ol").not(".margin-left-set").each(function () {
                    if (/none.+none/i.test($(this).css("list-style")) || $(this).is(".gallery")) {
                        if ($(this).parent().is("li") && $(this).parent().parent().is("ul,ol")) { $(this).css("margin-left", "1.2em"); }
                        else { $(this).css("margin-left", "0.2em"); }
                    } else if ($(this).is("ol")) {
                        var l = $(this).children("li").length;
                        if ($(this).attr("start")) { l += parseInt($(this).attr("start")); }
                        $(this).css("margin-left", (l + "").length * 0.5 + 1.2 + "em");
                    } else { $(this).css("margin-left", "1.2em"); }
                    $(this).addClass("margin-left-set");
                });
            }, 200);
            //修正hash跳转错误
            if ($(".mw-collapsible")[0]) {
                mw.loader.using("jquery.makeCollapsible", function () {
                    $(".mw-collapsible").makeCollapsible();
                    var hash = location.hash;
                    location.hash = "";
                    location.hash = hash;
                });
            }
            //以下代码必须在全部内容加载完成后才能正常工作
            $(window).on("load", function () {
                //语言对应
                $(".mw-helplink").each(function () {
                    var linkHref = this.href;
                    if (linkHref.indexOf("/zh") !== linkHref.length - 3) { this.href += "/zh"; }
                });
            });
        });
    })(jQuery, mediaWiki); //立即执行匿名函数并传递原始变量以防止冲突
    
    function scrollToTop(){
      window.scrollTo({top: 0, behavior: 'smooth'});
    }