MediaWiki:Common.js:修订间差异

imported>=海豚=
与萌百同步
fix
 
(未显示4个用户的76个中间版本)
第1行: 第1行:
/* eslint-disable no-var */
/* 这里的任何JavaScript将在全站加载 */
/* eslint-disable no-magic-numbers */
/* global OO, mediaWiki, jQuery */
// <nowiki>
// <nowiki>
/* 这里的任何JavaScript将在全站加载
* 请尊重版权,以下代码复制需要注明原自H萌娘,并且附上URL地址http://www.hmoegirl.com/MediaWiki:Common.js
* 版权协定:知识共享 署名-非商业性使用-相同方式共享 3.0
*  loader模块 写法参见 https://www.mediawiki.org/wiki/ResourceLoader/Modules#mw.loader.load
*/
"use strict";
"use strict";
(function($, mw) { //使用匿名函数防止污染顶级变量
(function($, mw) { //使用匿名函数防止污染顶级变量
    /* 反嵌入反反代 */
    try {
        var substHost;
        try {
            substHost = top.location.host;
        } catch (_) {
            substHost = "";
        }
        var currentHostFlag = !/\.moegirl\.org$/i.test(location.host);
        if (top !== window || currentHostFlag) {
            var reverseProxyhostAlerted = [];
            if (sessionStorage.getItem("reverse proxy alerted") !== null) {
                try {
                    reverseProxyhostAlerted = JSON.parse(sessionStorage.getItem("reverse proxy alerted"));
                    if (!Array.isArray(reverseProxyhostAlerted)) { reverseProxyhostAlerted = []; }
                } catch (e) {
                    reverseProxyhostAlerted = [];
                }
            }
            var detectHost = currentHostFlag ? location.host : substHost;
            if (!reverseProxyhostAlerted.includes(detectHost)) {
                reverseProxyhostAlerted.push(detectHost);
                mw.loader.using(["oojs-ui"]).then(function() {
                    var messageDialog = new OO.ui.MessageDialog();
                    var windowManager = new OO.ui.WindowManager();
                    $("body").append(windowManager.$element);
                    windowManager.addWindows([messageDialog]);
                    messageDialog.title.$label.html("萌娘百科提醒您<br>您当前是在" + (currentHostFlag ? "非萌百域名" : "嵌套窗口") + "访问");
                    messageDialog.message.$label.html("<p>请注意不要在此域名下输入您的用户名或密码,以策安全!</p><p>" + (detectHost ? (currentHostFlag ? "当前" : "顶层窗口") + "域名为 " + location.host + ",<br>" : "") + "萌百域名是以 .moegirl.org 结尾的。</p>");
                    windowManager.openWindow(messageDialog, {
                        actions: [{
                            action: 'accept',
                            label: '我知道了',
                            flags: 'primary'
                        }]
                    });
                });
            }
            sessionStorage.setItem("reverse proxy alerted", JSON.stringify(reverseProxyhostAlerted));
        }
    } catch (e) {
        console.debug(e);
    }
     /* capture支持检测 */
     /* capture支持检测 */
     var captureSupported = false;
     var captureSupported = false;
     try {
     try {
         var options = Object.defineProperty({}, "capture", {
         var options = Object.defineProperty({}, "capture", {
             get: function() {
             get: function () {
                 captureSupported = true;
                 captureSupported = true;
                 return true;
                 return true;
第64行: 第14行:
         window.addEventListener("test", null, options);
         window.addEventListener("test", null, options);
     } catch (err) { /* */ }
     } catch (err) { /* */ }
     /* 函数定义体 */
     /* 检查是否为维护组成员 */
    var wgUserGroups = mw.config.get("wgUserGroups");
    var isMGPMGUser = wgUserGroups.includes("patroller") || wgUserGroups.includes("sysop");
     //滚动公告
     //滚动公告
     function autoScroll(obj) {
     function autoScroll(obj) {
第95行: 第47行:
         });
         });
     }
     }
        autoScroll("#scrollDiv");
     /* MediaViewer#populateStatsFromXhr 错误屏蔽 */
     /* MediaViewer#populateStatsFromXhr 错误屏蔽 */
     (function() {
     (function() {
第102行: 第55行:
         };
         };
     })();
     })();
     //Extension:MultimediaViewer的半透明化修改,用于保持背景文字处于原位,本应修改插件达成的,暂时先用JavaScript应急处理下
     //Extension:MultimediaViewer的半透明化修改。
     function multimediaViewer() {
     function multimediaViewer() {
         var _scrollTo = window.scrollTo;
         var _scrollTo = window.scrollTo;
第130行: 第83行:
             });
             });
             if ($(".mw-mmv-close")[0]) {
             if ($(".mw-mmv-close")[0]) {
                 if (mw.config.get("wgMultimediaViewer") !== "on") {
                 if (mw.config.get("wgMultimediaViewerInjected") !== "on") {
                     mw.config.set("wgMultimediaViewer", "on");
                     mw.config.set("wgMultimediaViewerInjected", "on");
                     $(".mw-mmv-image").off("click")[0].addEventListener("click", function(e) {
                     $(".mw-mmv-image").off("click")[0].addEventListener("click", function(e) {
                         if ($(e.target).is("img")) {
                         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();
                             window.open($(e.target).attr("src").replace(/(www\.hmoegirl\.info\/common\/)thumb\/([a-z\d]+\/[a-z\d]+\/)([^/]+)\/\d+px-\3/i, "$1$2$3"), "_blank").focus();
                             e.preventDefault();
                             e.preventDefault();
                             e.stopPropagation();
                             e.stopPropagation();
第145行: 第98行:
                     } : true);
                     } : true);
                 }
                 }
             } else if (mw.config.get("wgMultimediaViewer") === "on") {
             } else if (mw.config.get("wgMultimediaViewerInjected") === "on") {
                 flag = false;
                 flag = false;
                 mw.config.set("wgMultimediaViewer", "off");
                 mw.config.set("wgMultimediaViewerInjected", "off");
             }
             }
         }, 137);
         }, 137);
     }
     }
        //修复代码编辑器$.ucFirst引用错误
        $.extend({
            ucFirst: function (_s) {
                var s = _s + "";
                return s.charAt(0).toUpperCase() + s.substring(1);
            },
        });
     //子页面页顶链接
     //子页面页顶链接
     function subpage_links() {
     function subpage_links() {
第183行: 第143行:
     //重复文件列表函数
     //重复文件列表函数
     function duplicatedFileList() {
     function duplicatedFileList() {
         if (window.location.href.indexOf("title") === -1) { window.location.replace("http://zh.moegirl.org/index.php?title=Special:ListDuplicatedFiles&limit=500&offset=0"); }
         if (window.location.href.indexOf("title") === -1) { window.location.replace("http://www.hmoegirl.info/index.php?title=Special:ListDuplicatedFiles&limit=500&offset=0"); }
         $("#mw-content-text .mw-spcontent ol").attr("id",
         $("#mw-content-text .mw-spcontent ol").attr("id",
             "picOl");
             "picOl");
第193行: 第153行:
         $(".mw-spcontent > p:nth-child(1), .mw-spcontent > p:nth-child(2), .mw-spcontent > p:nth-child(7)").hide();
         $(".mw-spcontent > p:nth-child(1), .mw-spcontent > p:nth-child(2), .mw-spcontent > p:nth-child(7)").hide();
     }
     }
    //编辑请求
         //关键词警告
    function editRequest() {
        var nsIds = mw.config.get("wgNamespaceIds"),
            nsId = mw.config.get("wgNamespaceNumber"),
            ns = [];
        for (var i in nsIds) {
            if (nsIds[i] === nsId) { ns.push(i); }
        }
        if (!ns[0]) { return; }
        var page = mw.config.get("wgPageName"),
            talkns;
        for (var j = 0, l = ns.length; j < l; j++) {
            if (/^[A-Za-z0-9]+[ _]talk$/i.test(ns[j])) { return; }
            if (/^[a-zA-Z0-9]+$/.test(ns[j])) { talkns = ns[j] + "_talk:"; }
            page = page.replace(new RegExp("^" + ns[j] + ":", "i"), "");
        }
        var talkpage = talkns + page;
        var container = $("<div/>", {
            "class": "editRequest",
        });
        container.append("您虽然无权编辑本页面,但您可以点击右侧按钮在本页的讨论页提出编辑请求,让可以编辑的人代为编辑:");
        $("<span/>").addClass("newComment").text("提出编辑请求").on("click", function() {
            window.open("https://zh.moegirl.org/index.php?action=edit&preload=Template:编辑请求/comment&preloadtitle=编辑请求 - " + mw.config.get("wgUserName") + "&section=new&title=" + talkpage, "_blank");
        }).appendTo(container);
         $("#mw-content-text").children(".wikiEditor-ui:first, textarea[readonly]:first").before("<hr>").before(container);
    }
    //防滥用即将删除模板
     function antiAbuseListener(textarea, event) {
     function antiAbuseListener(textarea, event) {
         var filters = [
         var filters = [
             "{{即将删除",
             "{{即将删除",
            "{{即將刪除",
             "{{挂删",
             "{{挂删",
             "{{模板:即将删除",
             "{{模板:即将删除",
            "{{模板:即將刪除",
             "{{template:即将删除",
             "{{template:即将删除",
            "{{template:即將刪除",
             "[[分类:即将删除的页面]]",
             "[[分类:即将删除的页面",
             "[[category:即将删除的页面]]",
            "[[分類:即將刪除的頁面",
             "开发票",
             "[[category:即将删除的页面",
             "支那",
             "[[category:即將刪除的頁面",
             "zhina",
             "{{文件转移到萌娘共享",
             "{{文件轉移到萌娘共享",
            "{{模板:文件转移到萌娘共享",
            "{{模板:文件轉移到萌娘共享",
            "{{template:文件转移到萌娘共享",
            "{{template:文件轉移到萌娘共享",
            "[[分类:需要删除执行员删除的页面",
            "[[分類:需要刪除執行員刪除的頁面",
            "[[category:需要删除执行员删除的页面",
            "[[category:需要刪除執行員刪除的頁面",
         ];
         ];
         var text1 = textarea.value;
         var text1 = textarea.value;
第260行: 第182行:
             $("body").append(windowManager.$element);
             $("body").append(windowManager.$element);
             windowManager.addWindows([messageDialog]);
             windowManager.addWindows([messageDialog]);
             messageDialog.title.$label.html("H萌娘提醒您<br>您的编辑包含禁止内容!");
             messageDialog.title.$label.html("H萌娘提醒您");
             messageDialog.message.$label.html("<br>您提交的内容包含以下部分,被禁止提交:<br><br><ul>" + results.map(function(result) {
             messageDialog.message.$label.html("<br>您提交的内容包含以下违规内容,被禁止提交:<br><br><ul>" + results.map(function (result) {
                 return "<li>" + result + "</li>";
                 return "<li>" + result + "</li>";
             }).join("") + '</ul><br><br>如果您认为您的编辑无误,请在自行保存编辑内容后到<a target="_blank" rel="nofollow noreferrer noopener" class="external text" href="https://zh.moegirl.org/Talk:%E6%8F%90%E9%97%AE%E6%B1%82%E5%8A%A9%E5%8C%BA">提问求助区</a>提问。');
             }).join("") + '</ul><br>如果您认为您的编辑无误,请在自行保存编辑内容后到讨论版提问!');
             windowManager.openWindow(messageDialog, {
             windowManager.openWindow(messageDialog, {
                 actions: [{
                 actions: [{
第277行: 第199行:
         }
         }
     }
     }
     function antiAbuse(textarea, submitButton, form) {
     function antiAbuse(textarea, submitButton, form) {
         mw.loader.using(["oojs-ui"]).then(function() {
         mw.loader.using(["oojs-ui"]).then(function () {
             if (submitButton) {
             if (submitButton) {
                 submitButton.addEventListener("click", function(e) {
                 submitButton.addEventListener("click", function (e) {
                     antiAbuseListener(textarea, e);
                     antiAbuseListener(textarea, e);
                 }, captureSupported ? {
                 }, captureSupported ? {
第288行: 第209行:
             }
             }
             if (form) {
             if (form) {
                 form.addEventListener("submit", function(e) {
                 form.addEventListener("submit", function (e) {
                     antiAbuseListener(textarea, e);
                     antiAbuseListener(textarea, e);
                 }, captureSupported ? {
                 }, captureSupported ? {
第295行: 第216行:
             }
             }
         });
         });
    }
      //防滥用即将删除
        if (!isMGPMGUser) {
            setInterval(function () {
                if ($(".Wikiplus-InterBox")[0]) {
                    var targetBox = $(".Wikiplus-InterBox");
                    var textarea = $("#Wikiplus-Quickedit")[0];
                    var submitButton = $("#Wikiplus-Quickedit-Submit");
                    if (textarea && targetBox.data("isTrusted") !== true) {
                        targetBox.data("isTrusted", true);
                        targetBox.css("z-index", 100);
                        antiAbuse(textarea, submitButton[0], undefined);
                        var events = $._data(textarea, "events");
                        if (events && events.keydown) {
                            events.keydown.sort(function (a, b) {
                                return a.guid - b.guid;
                            });
                            events.keydown[0].handler = function (e) {
                                if (e.ctrlKey && e.which === 83) {
                                    if (e.shiftKey) {
                                        $("#Wikiplus-Quickedit-MinorEdit").click();
                                    }
                                    if (antiAbuseListener(textarea, e) !== false) {
                                        submitButton.click();
                                        e.preventDefault();
                                        e.stopPropagation();
                                    }
                                }
                            };
                        }
                    }
                }
                if ($("form#editform")[0]) {
                    var targetForm = $("form#editform");
                    if (targetForm.data("isTrusted") !== true) {
                        targetForm.data("isTrusted", true);
                        antiAbuse($("#wpTextbox1")[0], $("#wpSave")[0], targetForm[0]);
                    }
                }
            }, 1000);
        }
    //编辑请求
    function editRequest() {
        var nsIds = mw.config.get("wgNamespaceIds"),
            nsId = mw.config.get("wgNamespaceNumber"),
            ns = [];
        for (var i in nsIds) {
            if (nsIds[i] === nsId) { ns.push(i); }
        }
        if (!ns[0]) { return; }
        var page = mw.config.get("wgPageName"),
            talkns;
        for (var j = 0, l = ns.length; j < l; j++) {
            if (/^[A-Za-z0-9]+[ _]talk$/i.test(ns[j])) { return; }
            if (/^[a-zA-Z0-9]+$/.test(ns[j])) { talkns = ns[j] + "_talk:"; }
            page = page.replace(new RegExp("^" + ns[j] + ":", "i"), "");
        }
        var talkpage = talkns + page;
        var container = $("<div/>", {
            "class": "editRequest",
        });
        container.append("您虽然无权编辑本页面,但您可以点击右侧按钮在本页的讨论页提出编辑请求,让可以编辑的人代为编辑:");
        $("<span/>").addClass("newComment").text("提出编辑请求").on("click", function() {
            window.open("https://www.hmoegirl.info/index.php?action=edit&preload=Template:编辑请求/comment&preloadtitle=编辑请求 - " + mw.config.get("wgUserName") + "&section=new&title=" + talkpage, "_blank");
        }).appendTo(container);
        $("#mw-content-text").children(".wikiEditor-ui:first, textarea[readonly]:first").before("<hr>").before(container);
     }
     }
     /* 函数执行体 */
     /* 函数执行体 */
第306行: 第293行:
             }
             }
         });
         });
        var copyRightsNameSpaces = [ // 以下为允许添加版权声明的名字空间列表
            0, // (主)
            4, // H萌娘
            8, // MediaWiki
            10, // Template
            12, // Help
            614, // R18
        ];
        //滚动公告
        autoScroll("#scrollDiv");
         //Extension:MultimediaViewer的半透明化修改
         //Extension:MultimediaViewer的半透明化修改
         if ($("img[srcset][data-file-width][data-file-height]")[0]) { 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(); }
         if ($(".mw-special-ListDuplicatedFiles").length && $(".mw-spcontent > p").length > 1) { duplicatedFileList(); }
第331行: 第306行:
         });
         });
         //编辑请求
         //编辑请求
         if (["edit", "submit"].indexOf(mw.config.get("wgAction")) !== -1 && !$(".permissions-errors, #wpTextbox1[readonly]")[1] && mw.config.get("wgUserName")) { editRequest(); }
         if (["edit", "submit"].indexOf(mw.config.get("wgAction")) !== -1 && !!$(".permissions-errors, #wpTextbox1[readonly]")[1] && mw.config.get("wgUserName")) { editRequest(); }
         //修复代码编辑器$.ucFirst引用错误
         //修复代码编辑器$.ucFirst引用错误
         $.extend({
         $.extend({
第365行: 第340行:
             });
             });
         }
         }
        //防滥用即将删除
        if (!mw.config.get("wgUserGroups").includes("sysop") && !mw.config.get("wgUserGroups").includes("patroller")) {
            setInterval(function() {
                if ($(".Wikiplus-InterBox")[0]) {
                    var targetBox = $(".Wikiplus-InterBox");
                    if (targetBox.data("isTrusted") !== true) {
                        targetBox.data("isTrusted", true);
                        targetBox.css("z-index", 100);
                        antiAbuse($("#Wikiplus-Quickedit")[0], $("#Wikiplus-Quickedit-Submit")[0], undefined);
                        var events = $._data($("#Wikiplus-Quickedit")[0], "events");
                        if (events && events.keydown) {
                            events.keydown.sort(function(a, b) {
                                return a.guid - b.guid;
                            });
                            events.keydown[0].handler = function(e) {
                                if (e.ctrlKey && e.which === 83) {
                                    if (e.shiftKey) {
                                        $("#Wikiplus-Quickedit-MinorEdit").click();
                                    }
                                    if (antiAbuseListener($("#Wikiplus-Quickedit")[0], e) !== false) {
                                        $("#Wikiplus-Quickedit-Submit").click();
                                        e.preventDefault();
                                        e.stopPropagation();
                                    }
                                }
                            };
                        }
                    }
                }
                if ($("form#editform")[0]) {
                    var targetForm = $("form#editform");
                    if (targetForm.data("isTrusted") !== true) {
                        targetForm.data("isTrusted", true);
                        antiAbuse($("#wpTextbox1")[0], $("#wpSave")[0], targetForm[0]);
                    }
                }
            }, 1000);
        }
    //R18警告
    function r18Warning() {
        var content = $('<div id="x18"><div id="x18Context"><div id="x18Title"><div id="x18Icon"></div></div><div class="x18Button"></div></div></div>');
        if (!window.localStorage.getItem('x18')) { //如果没有x18记录
            $('body').css('overflow', 'hidden'); //禁止滚屏
            $('#mw-content-text').css({
                'opacity': 0
            });
    //禁止偷看
            content.prependTo('body').find('.x18Button').append('<div id="x18Yes">是</div><div id="x18No" class="x18Button">否</div>')
                .end().find('#x18Title').append('你是否已年满十八岁?')
                .end().delay(400).queue(function() {
                    $(this).css({
                        'opacity': 1
                    }); //显示警告信息
                    $(this).dequeue();
                });
            $('#x18Yes').on('click', function() {
                window.localStorage.setItem('x18', true); //记录x18
                $('#mw-content-text').css('opacity', 1).removeAttr('style'); //显示内容
                $("#x18").remove(); //移除警告信息
                $('body').removeAttr('style'); //允许滚屏
            });
            $('#x18No').on('click', function() {
                window.location.replace(window.location.origin + '/' + mw.config.get('wgMainPageTitle')); //不给你看
            });
        } else $("#x18").remove(); //如果有x18记录
    }
    $(function() {
        tabs();
        r18Warning();
     });
     });
})();
})(jQuery, mediaWiki); //立即执行匿名函数并传递原始变量以防止冲突
})(jQuery, mediaWiki); //立即执行匿名函数并传递原始变量以防止冲突
// </nowiki>
// </nowiki>