Widget:TalkToc

HetmesAskalana讨论 | 贡献2022年11月26日 (六) 17:10的版本

Only for Special:前缀索引/萌娘百科_talk:讨论版/<script id="AnnTools_TalkToc"> "use strict"; window.RLQ = window.RLQ || []; window.RLQ.push([["ext.gadget.site-lib","jquery","ext.gadget.libOOUIDialog"],function (){

   if (location.hostname.includes("mzh.moegirl.org")) { return; }
   if (!mw.config.get("talkTocLoaded", false)) {
       mw.config.set("talkTocLoaded", true);
       if (!$("#talkTocBox")[0]) {
           if (localStorage.getItem("widgetTalkToc") === "disable") {
               $("body").addClass("widgetTalkTocDisable");
               $(".toctitle").after("<" + 'div style="text-align: center;">[<' + 'a class="enableWidgetTalkToc">' + wgUVS("启用讨论页面整理工具", "啟用討論頁面整理工具") + "<" + "/a>]<" + "/div>");
               $(".enableWidgetTalkToc").on("click", function () {
                   localStorage.setItem("widgetTalkToc", "enable");
                   $(this).text(wgUVS("启用成功!刷新页面中……", "啟用成功!重新整理頁面中……"));
                   setTimeout(location.reload.bind(location), 150);
               });
               return;
           }
           $("body").addClass("widgetTalkTocEnable");
           const toclist = ["tocBox", "talkTocBox", "toc"],
               bgcolor = $("body").css("background-color"),
               $toc = $("#toc");
           /* 预处理 */
           //备份目录
           $toc.after($toc.clone().hide().attr("id", "tocClone"));
           //整理讨论串
           $("#mw-content-text > .mw-parser-output > h2").each(function () {
               const head = $(this);
               const container = $("<" + "div/>");
               container.addClass("discussionContainer");
               head.addClass("discussionHead");
               head.before(container);
               head.nextUntil("h2").not("h2").appendTo(container);
               head.prependTo(container);
               if (container.find(".saveNotice")[0] && head.nextUntil(".saveNotice").length === 0) {
                   $toc.find(`a[href="#${head.find(".mw-headline")[0].id.replace(/"/g, '\\"')}"]`).parent().addClass("savedDiscussion hiddenDiscussion");
                   container.addClass("savedDiscussion");
               } else if(container.find(".movedToNotice")[0] && head.nextUntil(".movedToNotice").length === 0){
                   $toc.find(`a[href="#${head.find(".mw-headline")[0].id.replace(/"/g, '\\"')}"]`).parent().addClass("movedDiscussion hiddenDiscussion");
                   container.addClass("movedDiscussion");
               } else if (window.CSS && CSS.supports && CSS.supports("position", "sticky")) {
                   head.css({
                       position: "sticky",
                       top: 0,
                       "background-color": bgcolor,
                       "z-index": 999999,
                   });
                   container.css("position", "relative");
               }
           });
           // Temporary padding fix for Microsoft Edge
           // TODO: Report this issue to Edge team
           if (/Edge/.test(navigator.userAgent)) {
               $(".discussionContainer").css("padding-top", "10px");
           }
           //整理标题列表
           $toc.append("<" + "ol style='margin-left: 1.7em;'><" + "/ol>").after("<" + 'div id="tocBox"><' + "/div>");
           $toc.children("ul").children().appendTo($toc.find("ol"));
           $toc.find(".toclevel-1 > a > .tocnumber").remove();
           $toc.appendTo("#tocBox");
           /* 定义变量 */
           const $tocBox = $("#tocBox");
           const $level2 = $toc.find(".toclevel-2").hide();
           const $svdDcs = $toc.find(".savedDiscussion");
           const $movedDcs = $toc.find(".movedDiscussion");
           const $title = $toc.find(".toctitle").css("margin-right", "1em").append("<" + 'span id="toctoggle"><' + "/span>");
           const $toggle = $title.append("<" + 'div id="toggle"><' + "/div>").find("#toggle");
           const $tocText = $title.after("<" + 'div id="tocText"><' + "/div>").parents("#toc").find("#tocText");
           const appendToggleButton = function ($Obj, $set, shTx, hiTx, fun) {
               if (!$Obj[0]) { return false; }
               $set.append($("<" + "a/>", {
                   text: function () {
                       if ($Obj.is(":visible")) { return hiTx; }
                       return shTx;
                   },
                   "class": "toggleButton",
                   href: "javascript:void(0);",
                   bind: {
                       click: function () {
                           if ($Obj.is(":visible")) {
                               $Obj.hide();
                               $(this).text(shTx);
                           } else {
                               $Obj.show();
                               $(this).text(hiTx);
                           }
                       },
                   },
               }));
               if (typeof fun === "function") { fun(); }
           };
           /* 业务处理*/
           //添加存档讨论串标题toggle按钮
           if ($svdDcs[0] || $movedDcs[0]) {
               $toggle.append($("<" + "a/>", {
                   text: wgUVS("显示已被存档标题", "顯示已被存檔標題"),
                   "class": "toggleButton",
                   href: "javascript:void(0);",
                   on: {
                       click: function () {
                           if ($svdDcs.hasClass("hiddenDiscussion")) {
                               $svdDcs.removeClass("hiddenDiscussion");
                               $(this).text(wgUVS("隐藏已被存档标题", "隱藏已被存檔標題"));
                           } else {
                               $svdDcs.addClass("hiddenDiscussion");
                               $(this).text(wgUVS("显示已被存档标题", "顯示已被存檔標題"));
                           }
                       },
                   },
               }));
               $tocText.append("<" + "div class='tocText'>(" + wgUVS("有下划线标题是已被存档标题", "有下劃線標題是已被存檔標題") + ")<" + "/div>");
           }
           //添加二三四五级标题toggle按钮和标题数量
           appendToggleButton($level2, $toggle, wgUVS("显示段落", "顯示段落"), wgUVS("隐藏段落", "隱藏段落"), () => {
               $tocText.append("<" + "div class='tocText'>(" + wgUVS("中括号内数字是该大段下小段数量" ,"中括號內數字是該大段下小段數量") + ")<" + "/div>");
               $toc.find(".toclevel-1").each(function () {
                   const subtitleLength = $(this).find(".toctext").length - 1;
                   if (subtitleLength) { $(this).children("a").after(`[${subtitleLength}]`); }
               });
           });
           //添加整体toggle按钮
           appendToggleButton($toc.find("ol"), $title.find("#toctoggle"), wgUVS("显示", "顯示"), wgUVS("隐藏", "隱藏"));
           $("#toc").addClass("noOrigin");
           /*
           $('.saveNotice').each(function() {
               $(this).prev('h2').addClass('savedNotice');
           });*/
           const containers = $(".discussionContainer");
           containers.first().before("<" + 'div id="talkTocBox"><' + "table><" + "/table><" + "/div>");
           const self = $("#talkTocBox table"),
               a = $("<" + "a/>").text(wgUVS("显示已存档讨论串标题", "顯示已存檔討論串標題"));
           self.append("<" + "caption/>").find("caption").append(`${mw.config.get("wgTitle")}${wgUVS("现有讨论串","現有討論串")}`).append(" [").append(a).append("]<" + "br>").append(wgUVS("(有下划线标题是已被存档讨论串标题)", "(有下劃線標題是已被存檔討論串標題)"))
               .end().append("<" + "tbody/>");
           containers.not(".savedDiscussion").not(".movedDiscussion").each(function (i) {
               if (i % 4 === 0) { self.find("tbody:last").append("<" + "tr/>"); }
               const that = $(this);
               /* var content = that.children().not('h2');
               content.find('a').each(function() {
                   try {
                       var href = $(this)[0].href;
                       if (!href) return;
                       var link = new mw.Uri(href);
                       if (link.host !== 'zh.moegirl.org') this.userNameErrorReason = "link.host !== 'zh.moegirl.org'";
                       else if (/^\/api\.php/i.test(link.path)) this.userNameErrorReason = "!!/^\\/api\\.php/i.test(link.path)";
                       else if (!link.query.title && /\.php$/i.test(link.path)) this.userNameErrorReason = "!!(!link.query.title && /\\.php$/i.test(link.path))";
                       else {
                           var t = link.query.title || decodeURIComponent(link.path.substring(1));
                           if (/^user([ _]talk)?:[^\/]+$/i.test(t)) $(this).data('userName', t.replace(/^user([ _]talk)?:/i, )).addClass('userTalkPage');
                           else this.userNameErrorReason = "!!/^user([ _]talk)?:[^\\/]+$/i.test(t)";
                       }
                   } catch (e) {
                       return;
                   }
               });
               content.find('.userTalkPage').each(function() {
                   var parent = $(this).closest(containers).clone();
                   var now = Date.now() + (Math.random() + ).substring(2);
                   parent.find('.userTalkPage').text(now);
                   var regExec = RegExp(now + '(.*)(\\d{4}年([1-9]|1[0-2])月([1-9]|[12]\\d|3[01])日\\s{0,10}\\([一二三四五六日]\\)\\s{0,10}([01]\\d|2[0-3]):[0-5]\\d\\s{0,10}(\\([A-Z]{3}\\))?)').exec(parent.text());
                   if (!regExec || regExec[1].length > 20) {
                       this.userNameErrorReason = 'Too far away from timestamp (' + (regExec ? regExec[1].length : 'null') + ')';
                       this.userNameErrorRegExec = regExec;
                       $(this).removeClass('userTalkPage');
                   } else $(this).data('userTalkTime', regExec[2]);
               });
               var userTalkPage = [];
               content.find('.userTalkPage').each(function() {
                   var timestamp = $(this).data('userTalkTime').replace(/ \([A-Z]{3,4}\)$|\([日一二三四五六]\)/g, ),
                       date = new Date(),
                       time = timestamp.split(/[年月日\s\(\)\:]+/).map(function(n) {
                           return +n;
                       });
                   if (!time || !time[4]) return $(this).removeClass('.userTalkPage')[0].userNameErrorReason = 'Error Timestamp(' + timestamp + ')';
                   date.setFullYear(time[0]);
                   date.setMonth(time[1] - 1);
                   date.setDate(time[2]);
                   date.setHours(time[3]);
                   date.setMinutes(time[4]);
                   userTalkPage.push([$(this).data('userName'), timestamp, date.getTime()]);
               });
               if (!userTalkPage[0]) userTalkPage.push(['Unsigned user/Nonstandard sign', 'Unknown Time']);
               else userTalkPage.sort(function(a, b) {
                   return a[2] - b[2]
               });
               var firstName = userTalkPage[0],
                   lastName = userTalkPage[userTalkPage.length - 1];
               titlereg[that.find('.mw-headline:first').attr('id')] = that;*/
               self.find("tr:last").append("<" + "td><" + "/td>")
                   .find("td:last").append(
                       $("<" + "a/>").attr("href", `#${that.find(".mw-headline:first").attr("id")}`).append(that.find(".mw-headline:first").clone().find("a").replaceWith(function () {
                           return $(this).text();
                       }).end().html()),
                   );
               /* .append(
                   $('<' + 'div/>').addClass('signInfo').append('话题发起:' + (firstName[0] === 'Unsigned user/Nonstandard sign' ? 'Unsigned user/Nonstandard sign' : '<' + 'a href="/User_talk:' + firstName[0] + '" title="' + firstName[0] + '的讨论页" target="_blank">' + firstName[0].replace(/_/g, ' ') + '<' + '/a>'))
                   .append('<' + 'br>最后回复:' + (lastName[0] === 'Unsigned user/Nonstandard sign' ? 'Unsigned user/Nonstandard sign' : '<' + 'a href="/User_talk:' + lastName[0] + '" title="' + lastName[0] + '的讨论页" target="_blank">' + lastName[0].replace(/_/g, ' ') + '<' + '/a>') + '(' + lastName[1] + ')')
               ); */
           });
           /* if ($('.signInfo').filter(function() {
                   return $(this).text().indexOf('Unsigned user/Nonstandard sign') !== -1;
               }).length * 2 > $('.signInfo').length) $('.signInfo').hide(); */
           self.append("<" + 'tbody class="hr"><' + "tr><" + 'td colspan="4"> <' + "/td><" + "/tr><" + "/tbody>").append("<" + "tbody/>").find("tbody").not(":first").hide();
           containers.filter(".savedDiscussion").each(function (i) {
               if (i % 4 === 0) { self.find("tbody:last").append("<" + "tr/>"); }
               self.find("tr:last").append(`${"<" + "td><" + 'a class="savedTitle" href="#'}${$(this).find(".mw-headline:first").attr("id").replace(/"/g, """)}">${$(this).clone().find(".mw-headline:first").children(".mw-headline-number").remove().end().prepend(`${containers.index(this) + 1} `).text()}<` + "/a><" + "/td>");
           });
           self.append("<" + 'tbody class="hr"><' + "tr><" + 'td colspan="4"> <' + "/td><" + "/tr><" + "/tbody>").append("<" + "tbody/>").find("tbody").not(":first").hide();
           containers.filter(".movedDiscussion").each(function (i) {
               if (i % 4 === 0) { self.find("tbody:last").append("<" + "tr/>"); }
               self.find("tr:last").append(`${"<" + "td><" + 'a class="movedTitle" href="#'}${$(this).find(".mw-headline:first").attr("id").replace(/"/g, """)}">${$(this).clone().find(".mw-headline:first").children(".mw-headline-number").remove().end().prepend(`${containers.index(this) + 1} `).text()}<` + "/a><" + "/td>");
           });
           a.on("click", function () {
               self.find("tbody").not(":first").fadeToggle();
               $(this).text($(this).text() === wgUVS("显示已存档讨论串标题", "顯示已存檔討論串標題") ? wgUVS("隐藏已存档讨论串标题", "隱藏已存檔討論串標題") : wgUVS("显示已存档讨论串标题" ,"顯示已存檔討論串標題"));
           });
           self.find("tbody").not(".hr").each(function () {
               const that = $(this).find("tr:last");
               that.append(("<" + "td> <" + "/td>").repeat(4 - that.find("td").length));
           });
           $("#talkTocBox caption").append("<" + "br>[").append($("<" + "a/>").addClass("cbutton")).append("]");
           $("#tocText").append("<" + "br>[").append($("<" + "a/>").addClass("cbutton")).append("]");
           $(".cbutton").on("click", () => {
               if (!localStorage.wgTocStyle || localStorage.wgTocStyle !== "list") {
                   $("#talkTocBox").hide();
                   $("#tocBox").show();
                   localStorage.setItem("wgTocStyle", "list");
                   $(".cbutton").text(wgUVS("点击切换至表格格式", "點選切換至表格格式", null, null, "點擊切換至表格格式"));
                   $(toclist.filter((l) => {
                       return l !== "tocBox";
                   }).map((l) => {
                       return `a[href="#${l}"]`;
                   }).join(", ")).attr("href", "#tocBox");
               } else {
                   $("#talkTocBox").show();
                   $("#tocBox").hide();
                   localStorage.setItem("wgTocStyle", "table");
                   $(".cbutton").text(wgUVS("点击切换至列表格式", "點選切換至列表格式", null, null, "點擊切換至列表格式"));
                   $(toclist.filter((l) => {
                       return l !== "talkTocBox";
                   }).map((l) => {
                       return `a[href="#${l}"]`;
                   }).join(", ")).attr("href", "#talkTocBox");
               }
           });
           if (!localStorage.wgTocStyle || localStorage.wgTocStyle !== "list") {
               $("#talkTocBox").show();
               $(".cbutton").text(wgUVS("点击切换至列表格式", "點選切換至列表格式", null, null, "點擊切換至列表格式"));
               $(toclist.filter((l) => {
                   return l !== "talkTocBox";
               }).map((l) => {
                   return `a[href="#${l}"]`;
               }).join(", ")).attr("href", "#talkTocBox");
           } else {
               $("#tocBox").show();
               $(".cbutton").text(wgUVS("点击切换至表格格式", "點選切換至表格格式", null, null, "點擊切換至表格格式"));
               $(toclist.filter((l) => {
                   return l !== "tocBox";
               }).map((l) => {
                   return `a[href="#${l}"]`;
               }).join(", ")).attr("href", "#tocBox");
           }
           $(".cbutton").parent().append(" [<" + 'a class="disableWidgetTalkToc">' + wgUVS("禁用讨论页面整理工具", "禁用討論頁面整理工具") + "<" + "/a>]");
           $(".disableWidgetTalkToc").on("click", function () {
               localStorage.setItem("widgetTalkToc", "disable");
               $(this).text(wgUVS("禁用成功!刷新页面中……", "禁用成功!重新整理頁面中……"));
               setTimeout(location.reload.bind(location), 150);
           });
           $tocBox.css({
               position: "relative",
               "z-index": 10,
           });
           $(window).on("hashchange", () => {
               try {
                   const hash = location.hash.substring(1);
                   const target = $(`[id="${decodeURIComponent(hash).replace(/"/g, '\\"')}"]`);
                   if (target[0]) {
                       let scrollTop = target.offset().top;
                       if (target.closest(".discussionHead").length === 0) {
                           const parent = target.closest("h1, h2, h3, h4, h5, h6");
                           if (parent.length > 0) {
                               scrollTop -= parent.outerHeight(true) - parent.height();
                               const h = parent.prevAll("h1, h2, h3, h4, h5, h6").filter(function () {
                                   return $(this).css("position") === "sticky";
                               });
                               if (h.length > 0) { scrollTop -= h.outerHeight(); }
                           }
                       } else { scrollTop = target.closest(".discussionContainer").offset().top; }
                       setTimeout(() => {
                           $("html,body").animate({
                               scrollTop: scrollTop,
                           }, 0);
                       }, 1);
                       return false;
                   }
               } catch (e) {
                   return;
               }
           }).trigger("hashchange");
           const g = ["sysop", "patroller"];
           if (mw.config.get("wgUserGroups").filter(n => g.includes(n)).length === 0 && mw.config.get("wgPageName").startsWith("萌娘百科_talk:讨论版/")) {
               mw.loader.using("ext.gadget.libOOUIDialog");

$("#right-navigation, #moe-page-tools-container").addClass("no-need-to-edit-whole-page").find("#p-views > ul").append('

  • <a href="javascript:void(0);">' + wgULS("请勿编辑全文", "請勿編輯全文") + '</a>
  • ').find("#ca-no-need-to-edit-whole-page-notice a").on("click", async () => { await mw.loader.using("ext.gadget.libOOUIDialog"); oouiDialog.alert(wgULS("讨论版属于公共讨论页,一般来说无须编辑全文,故我们隐藏了入口。


    您可以点击下方标题旁的【编辑】链接来编辑对应段落,也可以点击右上方【添加话题】链接来新增话题。", "討論版屬於公共討論頁,一般來說無須編輯全文,故我們隱藏了入口。


    您可以點選下方標題旁的【編輯】連結來編輯對應段落,也可以點選右上方【新增話題】連結來新增話題。", null, null, "討論版屬於公共討論頁,一般來説無須編輯全文,故我們隱藏了入口。


    您可以點擊下方標題旁的【編輯】連結來編輯對應段落,也可以點擊右上方【新增話題】連結來新增話題。"), {

                           title: "萌娘百科提醒您",
                           size: "medium",
                       });
                   });
               }
           }
       }
    

    }]); </script><style>

    1. ca-nstab-project,
    2. p-namespaces ul>li:first-of-type,
    3. back-to-subject {
       display: none;
    

    }

    /*

     */
    .widgetTalkTocEnable #tocBox {
        max-width: 37%;
        min-width: 300px;
        float: left;
    }
    .widgetTalkTocEnable #toc.noOrigin {
        display: table;
    }
    .widgetTalkTocEnable div.toc#toc {
        min-width: 300px;
        box-sizing: border-box;
        margin-right: 37px;
        padding-right: 1em;
        padding-left: 1em;
    }
    .widgetTalkTocEnable div.toc#toc div.toctitle h2 {
        margin-right: 1em;
    }
    .widgetTalkTocEnable div.toc.noOrigin#toc .toctoggle {
        display: none;
    }
    .widgetTalkTocEnable div#toc.toc #toctoggle,
    .widgetTalkTocEnable div#toc.toc #toggle {
        font-size: 94%;
        -webkit-user-select: none;
        -ms-user-select: none;
        -o-user-select: none;
        -moz-user-select: none;
        user-select: none;
    }
    .widgetTalkTocEnable div#toc.toc .toggleButton:before {
        content: "[";
        color: black;
    }
    .widgetTalkTocEnable div#toc.toc .toggleButton:after {
        content: "]";
        color: black;
    }
    .widgetTalkTocEnable #toc .toggleButton:first-child {
        margin-right: 0.5em;
    }
    .widgetTalkTocEnable div#toc.toc .toggleButton:last-child {
        margin-right: 0;
    }
    .widgetTalkTocEnable div#toc.toc .hiddenDiscussion {
        visibility: hidden;
        height: 0;
        margin: 0;
    }
    .widgetTalkTocEnable div.toc#toc li.savedDiscussion a {
        text-decoration: underline;
    }
    .widgetTalkTocEnable div.toc#toc li.movedDiscussion a {
        text-decoration: line-through;
    }
    .widgetTalkTocEnable #toc div#tocText {
        text-align: center;
    }
    .widgetTalkTocEnable #toc div.tocText {
        display: inline-block;
    }
    .widgetTalkTocEnable {
        margin: 0 !important;
        padding: 0 !important;
    }
    #toc,
    #tocBox,
    #talkTocBox {
        display: none;
    }
    .widgetTalkTocDisable #toc {
        display: table;
    }
    .widgetTalkTocEnable #talkTocBox td {
        text-align: center;
        border: 1px solid #a7d7f9;
        width: 25%;
        padding: 0 1em;
    }
    .widgetTalkTocEnable #talkTocBox .hr td {
        width: 100%;
    }
    .widgetTalkTocEnable #talkTocBox table {
        border-collapse: collapse;
        margin-bottom: 1em;
        width: 100%;
    }
    .widgetTalkTocEnable #talkTocBox a {
        cursor: pointer;
    }
    .widgetTalkTocEnable #talkTocBox .savedTitle {
        text-decoration: underline;
    }
    .widgetTalkTocEnable #talkTocBox .movedTitle {
        text-decoration: line-through;
    }
    .widgetTalkTocEnable .cbutton {
        cursor: pointer;
    }
    .signInfo {
        display: none !important;
    }
    #right-navigation.no-need-to-edit-whole-page ul {
        display: flex;
    }
    #right-navigation.no-need-to-edit-whole-page ul>li {
        order: 999;
    }
    #right-navigation.no-need-to-edit-whole-page ul>li#ca-view {
        order: 1;
    }
    #right-navigation.no-need-to-edit-whole-page ul>li#ca-no-need-to-edit-whole-page-notice {
        order: 2;
        font-style: italic;
    }
    #right-navigation.no-need-to-edit-whole-page ul>li#ca-no-need-to-edit-whole-page-notice a,
    #right-navigation.no-need-to-edit-whole-page ul>li#ca-no-need-to-edit-whole-page-notice a:visited {
        color: #222;
        text-decoration: none;
    }
    #right-navigation.no-need-to-edit-whole-page ul>li#Wikiplus-Edit-TopBtn,
    #right-navigation.no-need-to-edit-whole-page ul>li#ca-quick-edit,
    #right-navigation.no-need-to-edit-whole-page ul>li#ca-edit,
    #right-navigation.no-need-to-edit-whole-page ul>li#ca-editTopSection,
    #moe-page-tools-container.no-need-to-edit-whole-page div>a#ca-edit, 
    #moe-page-tools-container.no-need-to-edit-whole-page div>li#Wikiplus-Edit-TopBtn {
        display: none;
    }
    .cloaked-element.transparent-shield {
        z-index: 9999999;
    }
    .navpopup {
        z-index: 1000000;
    }
    /* 

    */

    </style></includeonly>