打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Gadget-interfaceVariantConverter.js

MediaWiki界面页面

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
/**
 * -------------------------------------------------------------------------
 * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
 * !!!     Repository URL: https://github.com/SAPedia/InterfaceCodes     !!!
 * -------------------------------------------------------------------------
 */

/* <nowiki> */

"use strict";(()=>{var J=Object.defineProperty,Q=Object.defineProperties;var Y=Object.getOwnPropertyDescriptors;var S=Object.getOwnPropertySymbols;var U=Object.prototype.hasOwnProperty,W=Object.prototype.propertyIsEnumerable;var _=o=>{throw TypeError(o)};var I=(o,c,l)=>c in o?J(o,c,{enumerable:!0,configurable:!0,writable:!0,value:l}):o[c]=l,b=(o,c)=>{for(var l in c||(c={}))U.call(c,l)&&I(o,l,c[l]);if(S)for(var l of S(c))W.call(c,l)&&I(o,l,c[l]);return o},M=(o,c)=>Q(o,Y(c));var j=(o,c)=>{var l={};for(var w in o)U.call(o,w)&&c.indexOf(w)<0&&(l[w]=o[w]);if(o!=null&&S)for(var w of S(o))c.indexOf(w)<0&&W.call(o,w)&&(l[w]=o[w]);return l};var N=(o,c,l)=>I(o,typeof c!="symbol"?c+"":c,l),ee=(o,c,l)=>c.has(o)||_("Cannot "+l);var D=(o,c,l)=>c.has(o)?_("Cannot add the same private member more than once"):c instanceof WeakSet?c.add(o):c.set(o,l);var u=(o,c,l)=>(ee(o,c,"access private method"),l);$(()=>(async()=>{var r,T,k,L,q,H,F,R,B,G;let{wgPageName:o,wgUserName:c,wgArticleId:l}=mw.config.get(["wgPageName","wgUserName","wgArticleId"]),w=o.replace(/\/.*?$/,""),P=new mw.Api,z=b({main:["zh-cn","zh-tw","zh-hk"],dependent:{"(main)":"zh-cn","zh-hans":"zh-cn","zh-hant":"zh-tw"},noteTA:{G1:"MediaWiki"},autoPopulate:!0,useOpenCC:!0,manualAction:{"zh-hk":d=>d.replaceAll("户","戶"),"zh-tw":d=>d.replaceAll("名稱空間","命名空間").replaceAll("記憶體","內存")},watchlist:"nochange"},window.lr_aivc),A="";if(z.autoPopulate)try{A=(await P.get({action:"parse",assertuser:c,pageid:l,prop:"wikitext"})).parse.wikitext["*"]}catch(d){}let K=d=>Object.entries(d).map(([g,t])=>`${g}=${t}`).join("|"),y=d=>d==="(main)"?w:`${w}/${d}`,h={lcMarker:/-{([\s\S]*?)}-/g,lcMarkerEsc:/-\\{([\s\S]*?)}\\-/g,nowiki:/<nowiki>([\s\S]*?)<\/nowiki>/g,link:/\[\[([\s\S]*?)(?:#([\s\S]*?))?(\|[\s\S]*?)?\]\]/g,extLink:/([^[])\[([^[]+?)( [\s\S]+?)?\]([^\]])/g,template:/\{\{([\s\S]*?)\}\}/g,htmlEntity:/&([a-zA-Z0-9#]+);/g,noOCC:/<!--noOCC-->([\s\S]*?)<!--\/noOCC-->/gi},X=d=>{let g=[],t=[],s=e=>t.push(e)-1,i=d;return i=i.replace(h.lcMarker,(e,n)=>`-\\{${n}}\\-`),i=i.replace(h.nowiki,(e,n)=>`<nowiki>${g.push(n)-1}</nowiki>`),i=i.replace(h.link,(e,n,a,p)=>a||p?`[[${p?s(n):n}${a?`#${s(a)}`:""}${p!=null?p:""}]]`:e),i=i.replace(h.extLink,(e,n,a,p,f)=>`${n}[${s(a)}${p!=null?p:""}]${f}`),i=i.replace(h.template,(e,n)=>{let[a,...p]=n.split("|");return s(a),`${p.reduce((O,m)=>{let C=m.indexOf("=");return C===-1?`${O}|${m}`:`${O}|${s(m.slice(0,C))}=${m.slice(C+1)}`},`{{${t.length-1}`)}}}`}),i=i.replace(h.lcMarkerEsc,(e,n)=>`-\\{${s(n!=null?n:"")}}\\-`),i=i.replace(h.htmlEntity,(e,n)=>`&${s(n)};`),i=i.replace(h.nowiki,(e,n)=>`<nowiki><nowiki>${g[n]}</nowiki></nowiki>`),{replaced:i,mappings:t}},Z=(d,g)=>{let t=[],s=d;return s=s.replace(h.nowiki,(i,e)=>`<nowiki>${t.push(e)-1}</nowiki>`),s=s.replace(h.htmlEntity,(i,e)=>`&${g[e]};`),s=s.replace(h.template,(i,e)=>{let[n,...a]=e.split("|");return`${a.reduce((f,O)=>{let m=O.indexOf("=");return m===-1?`${f}|${O}`:`${f}|${g[O.slice(0,m)]}=${O.slice(m+1)}`},`{{${g[n]}`)}}}`}),s=s.replace(h.extLink,(i,e,n,a,p)=>`${e}[${g[n]}${a!=null?a:""}]${p}`),s=s.replace(h.link,(i,e,n,a)=>n||a?`[[${a?g[e]:e}${g[n]?`#${g[n]}`:""}${a!=null?a:""}]]`:i),s=s.replace(h.nowiki,(i,e)=>`<nowiki>${t[e]}</nowiki>`),s=s.replace(h.lcMarkerEsc,(i,e)=>{var n;return`-{${(n=g[e])!=null?n:e}}-`}),s};class x extends OO.ui.ProcessDialog{constructor(i){var e=i,{data:t}=e,s=j(e,["data"]);super(b({data:t},s));D(this,r);this.config=t.config,this.prepopContent=t.prepopContent}initialize(){super.initialize(),this.configPanel=u(this,r,T).call(this),this.confirmPanel=u(this,r,T).call(this),this.ogText=new OO.ui.MultilineTextInputWidget({value:this.prepopContent,autosize:!0}),this.mainVariants=new OO.ui.TextInputWidget({value:this.config.main.join(";")}),this.depVariants=new OO.ui.TextInputWidget({value:Object.entries(this.config.dependent).map(([t,s])=>`${t}:${s}`).join(";")}),this.noteTAParams=new OO.ui.TextInputWidget({value:K(this.config.noteTA)}),this.occCheckbox=new OO.ui.CheckboxInputWidget({selected:this.config.useOpenCC}),this.configPanel.$element.append(u(this,r,k).call(this,this.ogText,wgULS("原始内容","原始內容"),"top"),u(this,r,k).call(this,this.mainVariants,wgULS("主要变体","主要變体"),"top"),u(this,r,k).call(this,this.depVariants,wgULS("依赖变体","依賴變体"),"top"),u(this,r,k).call(this,this.noteTAParams,wgULS("NoteTA参数","NoteTA參數"),"top"),u(this,r,k).call(this,this.occCheckbox,"使用OpenCC","inline")),this.stackLayout=new OO.ui.StackLayout({items:[this.configPanel,this.confirmPanel]}),this.ogText.connect(this,{resize:"updateSize"}),this.$body.append(this.stackLayout.$element)}getBodyHeight(){return this.stackLayout.getCurrentItem().$element.outerHeight(!0)}getSetupProcess(t){return super.getSetupProcess(t).next(()=>{u(this,r,L).call(this,"config",this.configPanel)},this)}getReadyProcess(t){return super.getReadyProcess(t).next(()=>{this.ogText.focus()},this)}getActionProcess(t){let i={cancel:()=>this.close({action:t}),continue:async()=>{if(u(this,r,q).call(this),this.config.main.includes("(main)"))throw new OO.ui.Error(wgULS("主页面不得作为主要变体","主頁面不得作為主要變体"));u(this,r,H).call(this),this.textInputs={},this.confirmPanel.$element.empty(),await u(this,r,R).call(this,this.ogText.getValue()),u(this,r,L).call(this,"confirm",this.confirmPanel)},back:()=>{u(this,r,L).call(this,"config",this.configPanel)},submit:async()=>{await u(this,r,G).call(this),this.close({action:t}),mw.notify("保存成功!",{title:wgULS("自动繁简转换工具","自動繁簡轉換工具"),type:"success",tag:"lr-aivc"}),setTimeout(()=>location.reload(),730)}}[t];return i?new OO.ui.Process((async()=>{try{await i()}catch(e){throw e instanceof OO.ui.Error?e:(console.error("[VariantConverter] Error:",e),new OO.ui.Error(String(e)))}})(),this):super.getActionProcess(t)}}r=new WeakSet,T=function(){return new OO.ui.PanelLayout({scrollable:!1,expanded:!1,padded:!0})},k=function(t,s,i){return new OO.ui.FieldLayout(t,{label:s,align:i}).$element},L=function(t,s){this.actions.setMode(t),this.stackLayout.setItem(s),this.updateSize()},q=function(){Object.assign(this.config,{main:this.mainVariants.getValue().split(";"),dependent:Object.fromEntries(this.depVariants.getValue().split(";").map(t=>t.split(":"))),noteTAStr:this.noteTAParams.getValue(),useOpenCC:this.occCheckbox.isSelected(),dependentInv:{}})},H=function(){for(let t of this.config.main)this.config.dependentInv[t]=[t];try{for(let[t,s]of Object.entries(this.config.dependent))this.config.dependentInv[s].push(t)}catch(t){throw console.error("[VariantConverter] Error: Key not found in dependentInv. Config dump:",this.config),new OO.ui.Error(wgULS("依赖变体格式错误,请检查控制台","依賴變体格式錯誤,請檢查控制臺"))}},F=function(t,s){var e;let i=new OO.ui.MultilineTextInputWidget({value:s,autosize:!0});if(this.textInputs[t]=i,i.connect(this,{resize:"updateSize"}),this.confirmPanel.$element.append(u(this,r,k).call(this,i,t,"top")),(e=window==null?void 0:window.InPageEdit)!=null&&e.quickDiff){let n=this.config.dependentInv[t].map(a=>new OO.ui.ButtonWidget({label:`${wgULS("对比","對比")}${a==="(main)"?wgULS("主页面","主頁面"):a}`}).on("click",()=>window.InPageEdit.quickDiff({fromtitle:y(a),totext:i.getValue(),pageName:y(a),isPreview:!0})));this.confirmPanel.$element.append(new OO.ui.FieldLayout(new OO.ui.Widget({content:[new OO.ui.HorizontalLayout({items:n})]})).$element)}},R=async function(t){this.confirmPanel.$element.append(`<p>${wgULS("请确认以下转换是否正确","請確認以下轉換是否正確")}:</p>`),this.config.useOpenCC&&!window.OpenCC&&await libCachedCode.injectCachedCode("https://fastly.jsdelivr.net/npm/opencc-js@1.4.1","script");let{replaced:s,mappings:i}=X(t);await Promise.all(this.config.main.filter(e=>e!=="(main)").map(async e=>{var m,C,E;let n=`{{NoteTA|${this.config.noteTAStr}}}<pre id="converted">-{}-${s}</pre>`,a=await P.post({action:"parse",assertuser:c,text:n,contentmodel:"wikitext",prop:"text",uselang:e,disablelimitreport:!0,pst:!0}),f=$($.parseHTML(a.parse.text["*"])).find("#converted").text();this.config.useOpenCC&&(f=u(this,r,B).call(this,f,e));let O=(E=(C=(m=this.config.manualAction)[e])==null?void 0:C.call(m,f))!=null?E:f;u(this,r,F).call(this,e,Z(O,i))}))},B=function(t,s){let i=[],e=t.replace(h.noOCC,(a,p)=>`<!--noOCC-->${i.push(p)-1}<!--/noOCC-->`),n=s.replace("hans","cn").replace(/zh-(?:han)?/,"").replace("tw","twp");return e=OpenCC.Converter({from:n,to:n})(e),e.replace(h.noOCC,(a,p)=>`<!--noOCC-->${i[p]}<!--/noOCC-->`)},G=async function(){let t=(n,a,p)=>({action:"edit",assertuser:c,title:n,text:a,summary:p,tags:"VariantConverter|Automation tool",watchlist:this.config.watchlist}),s=this.config.main.map(n=>P.postWithToken("csrf",t(y(n),this.textInputs[n].getValue(),`自动转换自[[${o}]]`))),i=Object.entries(this.config.dependent).map(([n,a])=>P.postWithToken("csrf",t(y(n),this.textInputs[a].getValue(),`自动转换自[[${o}]](同步${a})`))),e=await Promise.allSettled([...s,...i]);console.log("[VariantConverter] Saved changes",e)},N(x,"static",M(b({},OO.ui.ProcessDialog.static),{tagName:"div",name:"lr-aivc",title:wgULS("自动繁简转换工具","自動繁簡轉換工具"),actions:[{action:"cancel",label:"取消",flags:["safe","close","destructive"],modes:"config"},{action:"continue",label:wgULS("继续","繼續"),flags:["primary","progressive"],modes:"config"},{action:"back",label:"返回",flags:["safe","back"],modes:"confirm"},{action:"submit",label:wgULS("确认","確認"),flags:["primary","progressive"],modes:"confirm"}]}));let v=new OO.ui.WindowManager;$(document.body).append(v.$element);let V=new x({size:"large",data:{config:z,prepopContent:A}});v.addWindows([V]),$(mw.util.addPortletLink("p-cactions","#",wgULS("自动繁简转换","自動繁簡轉換"),"ca-VariantConverter",wgULS("自动同步界面消息的繁简版本","自動同步介面消息的繁簡版本"))).on("click",d=>{d.preventDefault(),$("#mw-notification-area").appendTo(document.body),v.openWindow(V)})})());})();

/* </nowiki> */