2023年政策修订增补工作正在进行中,欢迎参与!
User:BearBin/js/OneKeyPurge.js
< User:BearBin | js
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox/Safari:按住“Shift”的同时单击“刷新”,或按“Ctrl-F5”或“Ctrl-R”(Mac为“⌘-R”)
- Google Chrome:按“Ctrl-Shift-R”(Mac为“⌘-Shift-R”)
- Internet Explorer:按住“Ctrl”的同时单击“刷新”,或按“Ctrl-F5”
- Opera:在“工具→首选项”中清除缓存
- 如果您已登录但该页面出现未登录状态,请尝试在地址栏的地址最后添加代码
?_=1
来访问最新页面。 - 添加代码后的本页地址如下:
-{R|https://moegirl.icu/User:BearBin/js/OneKeyPurge.js?_=1}-
var _addText = '{{Documentation|content=* 工具介绍见[[User:BearBin/js#OneKeyPurge]]。\n* 源代码见[https://github.com/BearBin1215/MoegirlPedia/blob/master/src/gadgets/OneKeyPurge GitHub]。}}';
// <nowiki>
(()=>{"use strict";var e={371:e=>{e.exports=function(e){var t=[];return t.toString=function(){return this.map((function(t){var n="",i=void 0!==t[5];return t[4]&&(n+="@supports (".concat(t[4],") {")),t[2]&&(n+="@media ".concat(t[2]," {")),i&&(n+="@layer".concat(t[5].length>0?" ".concat(t[5]):""," {")),n+=e(t),i&&(n+="}"),t[2]&&(n+="}"),t[4]&&(n+="}"),n})).join("")},t.i=function(e,n,i,a,s){"string"==typeof e&&(e=[[null,e,void 0]]);var o={};if(i)for(var r=0;r<this.length;r++){var l=this[r][0];null!=l&&(o[l]=!0)}for(var c=0;c<e.length;c++){var d=[].concat(e[c]);i&&o[d[0]]||(void 0!==s&&(void 0===d[5]||(d[1]="@layer".concat(d[5].length>0?" ".concat(d[5]):""," {").concat(d[1],"}")),d[5]=s),n&&(d[2]?(d[1]="@media ".concat(d[2]," {").concat(d[1],"}"),d[2]=n):d[2]=n),a&&(d[4]?(d[1]="@supports (".concat(d[4],") {").concat(d[1],"}"),d[4]=a):d[4]="".concat(a)),t.push(d))}},t}},474:e=>{e.exports=function(e){return e[1]}},803:(e,t,n)=>{n.d(t,{A:()=>r});var i=n(474),a=n.n(i),s=n(371),o=n.n(s)()(a());o.push([e.id,".snake .snake-head{font-size:1.3em;margin:.8em 0 .5em;text-align:center;text-decoration:underline}.snake .snake-body{display:flex;flex-wrap:wrap;gap:.2em;justify-content:center;max-height:10.5em;overflow-y:auto}.snake .snake-scale{border:1px solid;height:1em;width:1em}.snake .snake-scale[data-scale-state=ongoing],.snake .snake-scale[data-scale-state=ready]{background-color:#eaecf0;border-color:#a2a9b1}.snake .snake-scale[data-scale-state=success]{background-color:#d5fdf4;border-color:#16876e}.snake .snake-scale[data-scale-state=warn]{background-color:#fee7e6;border-color:#d33}.snake .snake-scale[data-scale-state=fail]{background-color:#fef6e7;border-color:#edab00}",""]);const r=o},568:(e,t,n)=>{n.d(t,{A:()=>r});var i=n(474),a=n.n(i),s=n(371),o=n.n(s)()(a());o.push([e.id,"#one-key-purge .okp-button{flex:0 1;padding:6px}#one-key-purge .okp-header{border-bottom:1px solid #bbb;display:flex}#one-key-purge .okp-header .okp-header-text{align-items:center;display:flex;flex:1 0;font-weight:700;justify-content:center}#one-key-purge .okp-footer{border-top:1px solid #bbb}#one-key-purge .okp-note{font-size:1.143em;line-height:1.3;margin-bottom:.8em}#one-key-purge .oo-ui-fieldLayout-header{font-weight:700;min-width:6em;width:20%}#one-key-purge .oo-ui-multiselectWidget-group,#one-key-purge .oo-ui-radioSelectWidget{display:flex;flex-wrap:wrap}#one-key-purge .oo-ui-multiselectWidget-group>label,#one-key-purge .oo-ui-radioSelectWidget>label{flex:1 0 11em;padding:4px 0}#one-key-purge-help{list-style:circle;padding-left:1.2em}",""]);const r=o},960:e=>{var t=[];function n(e){for(var n=-1,i=0;i<t.length;i++)if(t[i].identifier===e){n=i;break}return n}function i(e,i){for(var s={},o=[],r=0;r<e.length;r++){var l=e[r],c=i.base?l[0]+i.base:l[0],d=s[c]||0,u="".concat(c," ").concat(d);s[c]=d+1;var p=n(u),h={css:l[1],media:l[2],sourceMap:l[3],supports:l[4],layer:l[5]};if(-1!==p)t[p].references++,t[p].updater(h);else{var g=a(h,i);i.byIndex=r,t.splice(r,0,{identifier:u,updater:g,references:1})}o.push(u)}return o}function a(e,t){var n=t.domAPI(t);n.update(e);return function(t){if(t){if(t.css===e.css&&t.media===e.media&&t.sourceMap===e.sourceMap&&t.supports===e.supports&&t.layer===e.layer)return;n.update(e=t)}else n.remove()}}e.exports=function(e,a){var s=i(e=e||[],a=a||{});return function(e){e=e||[];for(var o=0;o<s.length;o++){var r=n(s[o]);t[r].references--}for(var l=i(e,a),c=0;c<s.length;c++){var d=n(s[c]);0===t[d].references&&(t[d].updater(),t.splice(d,1))}s=l}}},443:e=>{var t={};e.exports=function(e,n){var i=function(e){if(void 0===t[e]){var n=document.querySelector(e);if(window.HTMLIFrameElement&&n instanceof window.HTMLIFrameElement)try{n=n.contentDocument.head}catch(e){n=null}t[e]=n}return t[e]}(e);if(!i)throw new Error("Couldn't find a style target. This probably means that the value for the 'insert' parameter is invalid.");i.appendChild(n)}},628:e=>{e.exports=function(e){var t=document.createElement("style");return e.setAttributes(t,e.attributes),e.insert(t,e.options),t}},120:(e,t,n)=>{e.exports=function(e){var t=n.nc;t&&e.setAttribute("nonce",t)}},81:e=>{e.exports=function(e){if("undefined"==typeof document)return{update:function(){},remove:function(){}};var t=e.insertStyleElement(e);return{update:function(n){!function(e,t,n){var i="";n.supports&&(i+="@supports (".concat(n.supports,") {")),n.media&&(i+="@media ".concat(n.media," {"));var a=void 0!==n.layer;a&&(i+="@layer".concat(n.layer.length>0?" ".concat(n.layer):""," {")),i+=n.css,a&&(i+="}"),n.media&&(i+="}"),n.supports&&(i+="}");var s=n.sourceMap;s&&"undefined"!=typeof btoa&&(i+="\n/*# sourceMappingURL=data:application/json;base64,".concat(btoa(unescape(encodeURIComponent(JSON.stringify(s))))," */")),t.styleTagTransform(i,e,t.options)}(t,e,n)},remove:function(){!function(e){if(null===e.parentNode)return!1;e.parentNode.removeChild(e)}(t)}}}},849:e=>{e.exports=function(e,t){if(t.styleSheet)t.styleSheet.cssText=e;else{for(;t.firstChild;)t.removeChild(t.firstChild);t.appendChild(document.createTextNode(e))}}}},t={};function n(i){var a=t[i];if(void 0!==a)return a.exports;var s=t[i]={id:i,exports:{}};return e[i](s,s.exports,n),s.exports}n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.nc=void 0,(()=>{const e=e=>{const t=document.createElement("template");return t.innerHTML=e.trim(),t.content.children[0]};var t=n(960),i=n.n(t),a=n(81),s=n.n(a),o=n(443),r=n.n(o),l=n(120),c=n.n(l),d=n(628),u=n.n(d),p=n(849),h=n.n(p),g=n(803),m={};m.styleTagTransform=h(),m.setAttributes=c(),m.insert=r().bind(null,"head"),m.domAPI=s(),m.insertStyleElement=u();i()(g.A,m);g.A&&g.A.locals&&g.A.locals;class f{_length=0;_complete=0;hasHead;hasHref;element;head;body;headComplete;headLength;blocks={};constructor({hasHead:t=!0,hasHref:n=!0,...i}={}){this.hasHead=t,this.hasHref=n,this.element=e('<div class="snake"></div>');for(const e in i)this.element.setAttribute(e,i[e]);this.hasHead&&(this.head=e('<div class="snake-head"></div>'),this.headComplete=e('<span class="snake-head-complete">0</div>'),this.headLength=e('<span class="snake-head-all">0</div>'),this.head.append("已完成:",this.headComplete,"/",this.headLength),this.element.append(this.head)),this.body=e('<div class="snake-body"></div>'),this.element.append(this.body)}set length(e){this._length=e,this.hasHead&&(this.headLength.innerHTML=e)}get length(){return this._length}set complete(e){this._complete=e,this.hasHead&&(this.headComplete.innerHTML=e)}get complete(){return this._complete}addScale(e,t,n){if(this.blocks[e])throw new Error(`Snake: 项目${e}已存在。`);let i;this.hasHref?(i=document.createElement("a"),i.title=t||e,i.href=n||`/${e}`,i.target="_blank"):(i=document.createElement("div"),t&&(i.title=t)),i.classList.add("snake-scale","scale-state-ready"),i.dataset.scaleState="ready",this.length++,this.blocks[e]=i,this.body.append(i)}removeScale(...e){for(const t of e){if(!this.blocks[t])throw new Error(`Snake: 不存在名为${e}的项目。`);this.blocks[t].remove(),this.length--,Reflect.deleteProperty(this.blocks,t)}}crawl(e,t="success"){if(!this.blocks[e])throw new Error(`Snake: 不存在名为${e}的项目。`);"success"===this.blocks[e].dataset.scaleState&&this.complete--,"success"===t&&this.complete++,"ongoing"===this.blocks[e].dataset.scaleState&&this.blocks[e].classList.remove("oo-ui-pendingElement-pending"),"ongoing"===t&&this.blocks[e].classList.add("oo-ui-pendingElement-pending"),this.blocks[e].dataset.scaleState=t,this.blocks[e].scrollIntoView()}clear(){this.complete=0,this.length=0,this.blocks={},this.body.innerHTML=""}}const y=async(e,t)=>{const n=new mw.Api;let i;const a=[],s={action:"query",prop:"linkshere",titles:e,lhlimit:"max"};t&&(s.lhnamespace=t);do{const e=await n.post(s);a.push(...(Object.values(e.query.pages)[0].linkshere||[]).map((({title:e})=>e))),i=e.continue?.lhcontinue,i&&(s.lhcontinue=i)}while(i);return a},w=async(e,t)=>{const n=new mw.Api;let i;const a=[],s={action:"query",prop:"transcludedin",titles:e,tilimit:"max"};t&&(s.tinamespace=t);do{const e=await n.post(s);a.push(...(Object.values(e.query.pages)[0].transcludedin||[]).map((({title:e})=>e))),i=e.continue?.ticontinue,i&&(s.ticontinue=i)}while(i);return a},b=async(e,t=["page","subcat","file"])=>{const n=new mw.Api,i=[];if(mw.config.get("wgUserGroups").some((e=>["bot","flood","patroller","sysop"].includes(e)))){let a="";for(;void 0!==a;){const s=await n.post({action:"query",list:"categorymembers",cmlimit:"max",cmtitle:e,cmtype:t,cmcontinue:a});if(s.query.categorymembers[0])for(const e of s.query.categorymembers)i.push(e.title);a=s.continue?.cmcontinue}}else{const n=async e=>{const a=$(await $.ajax(e)),s=t.map((e=>{switch(e){case"page":return"#mw-pages li a";case"subcat":return"#mw-subcategories li a";case"file":return"#mw-category-media li a.galleryfilename"}})).join(","),o=a.find(s).map(((e,t)=>t.classList.contains("CategoryTreeLabel")?`Category:${$(t).text()}`:t.classList.contains("galleryfilename")?`File:${$(t).text()}`:$(t).text())).get();if(i.push(...o),t.includes("page")){const e=a.find('a[href*="&pagefrom="]');e.length&&await n(e.eq(0).attr("href"))}if(t.includes("subcat")){const e=a.find('a[href*="&subcatfrom="]');e.length&&await n(e.eq(0).attr("href"))}if(t.includes("file")){const e=a.find('a[href*="&filefrom="]');e.length&&await n(e.eq(0).attr("href"))}};await n(`/${e}?action=render`)}return i},k=e=>new Promise((t=>setTimeout(t,e)));var v=n(568),x={};x.styleTagTransform=h(),x.setAttributes=c(),x.insert=r().bind(null,"head"),x.domAPI=s(),x.insertStyleElement=u();i()(v.A,x);v.A&&v.A.locals&&v.A.locals;$((()=>(async()=>{if(mw.config.get("wgNamespaceNumber")<0&&mw.config.get("wgRelevantPageName")===mw.config.get("wgPageName"))return;await mw.loader.using(["mediawiki.api","mediawiki.user","mediawiki.notification","oojs-ui"]);const e=new mw.Api,t=-1===mw.config.get("wgNamespaceNumber")?mw.config.get("wgRelevantPageName"):mw.config.get("wgPageName");let n=[];class i extends OO.ui.Dialog{failList=[];changeList=[];complete=0;progressBar=new f;running=!1;$header=$('<header class="okp-header"></header>');$footer=$('<footer class="okp-footer"></footer>');$body;panelLayout;closeButton;stopButton;actionButton;typeSelectInput;optionRadioSelect;static static={...super.static,tagName:"div",name:"one-key-purge"};initialize(){super.initialize(),this.panelLayout=new OO.ui.PanelLayout({scrollable:!1,expanded:!1,padded:!0}),this.closeButton=new OO.ui.ButtonWidget({label:"取消",flags:["destructive"]}),this.stopButton=new OO.ui.ButtonWidget({label:"终止",flags:["primary","destructive"]}),this.stopButton.$element.hide(),this.actionButton=new OO.ui.ButtonWidget({label:"执行",flags:["primary","progressive"]}),this.typeSelectInput=new OO.ui.CheckboxMultiselectInputWidget({options:[{data:"link",label:"链接"},{data:"include",label:"嵌入"},...14===mw.config.get("wgNamespaceNumber")?[{data:"category",label:"分类成员"}]:[]]});const e=new OO.ui.FieldLayout(this.typeSelectInput,{label:"页面类型"}),i=new OO.ui.ButtonWidget({label:"帮助",icon:"help"}),a=new OO.ui.RadioOptionWidget({data:"purge",label:"清除缓存(Purge)"}),s=new OO.ui.RadioOptionWidget({data:"nulledit",label:"零编辑(Null Edit)"});this.optionRadioSelect=new OO.ui.RadioSelectWidget({items:[a,s]}),this.optionRadioSelect.selectItem(a);const o=new OO.ui.FieldLayout(this.optionRadioSelect,{label:"操作类型"}),r=n.includes("noratelimit")?"<b>提醒</b>:在被大量嵌入/链入的页面此工具将会向服务器发送<b>大量请求</b>,请慎重使用!":"<b>提醒</b>:您未持有<code>noratelimit</code>权限,清除缓存和零编辑的速率将被分别限制为<u>30次/min</u>和<u>10次/min</u>,请耐心等待。";return this.closeButton.on("click",this.close.bind(this)),this.stopButton.on("click",(()=>{this.stopButton.setDisabled(!0),this.running=!1})),i.on("click",(()=>{OO.ui.alert($('<ul id="one-key-purge-help"></ul>').append("<li><b>清除缓存</b>:通常在需要刷新页面内容时使用,例如模板编辑后刷新嵌入了此模板的页面。</li>",`<li><b>零编辑</b>:通常在需要刷新<a href="/Special:链入页面/${t}">链入/嵌入页面列表</a>时使用,例如页面移动后清理链入。</li>`),{title:"帮助",size:"medium"})})),this.actionButton.on("click",this.action.bind(this)),this.$body.append(this.$header.append($('<div class="okp-button okp-cancel-button"/>').append(this.closeButton.$element,this.stopButton.$element),$('<div class="okp-header-text">批量清除页面缓存</div>'),$('<div class="okp-button okp-action-button"/>').append(this.actionButton.$element)),this.panelLayout.$element.append(`<div class="okp-note">${r}</div>`,e.$element,o.$element,this.progressBar.element),this.$footer.append($('<div class="okp-button okp-help-button"/>').append(i.$element))),this}async getList(){const e=[];try{if(this.typeSelectInput.getValue().includes("link")){const n=await y(t);n.length>0?mw.notify(`获取链接到【${t}】的页面列表成功。`):mw.notify(`没有页面链接到【${t}】。`),e.push(...n)}if(this.typeSelectInput.getValue().includes("include")){const n=await w(t);n.length>0?mw.notify(`获取链接到【${t}】的页面列表成功。`):mw.notify(`没有页面链接到【${t}】。`),e.push(...n)}if(this.typeSelectInput.getValue().includes("category")){const n=await b(t,["page"]);n.length>0?mw.notify(`获取【${t}】的分类成员成功。`):mw.notify(`【${t}】内没有成员。`),e.push(...n)}}catch(e){}return[...new Set(e)]}get optionType(){return this.optionRadioSelect.findSelectedItem()?.getData()}progressChange(e,t,n=""){const i="nulledit"===this.optionType?"零编辑":"清除缓存";switch(this.progressBar.crawl(e,t),t.toLowerCase()){case"success":this.complete++;break;case"warn":this.complete++,this.changeList.push(e);break;case"fail":this.failList.push(e),mw.notify(`页面【${e}】${i}失败${n?`:${n}`:""}。`,{type:"warn"})}}async nullEdit(t){for(let i=0;i<t.length&&this.running;i++){const a=t[i];this.progressBar.crawl(a,"ongoing"),e.postWithToken("csrf",{format:"json",action:"edit",appendtext:"",watchlist:"nochange",nocreate:!0,title:a}).done((e=>{this.progressBar.crawl(a,"ongoing"),"Success"===e.edit.result?void 0!==e.edit.nochange?this.progressChange(a,"success"):this.progressChange(a,"warn"):this.progressChange(a,"fail")})).catch((e=>{this.progressChange(a,"fail",e)})),i+1<t.length&&(n.includes("noratelimit")?await k(1e3):await k(6e3))}}async purge(t){for(let i=0;i<t.length&&this.running;i+=5){const a=t.slice(i,i+5);for(const e of a)this.progressBar.crawl(e,"ongoing");e.post({format:"json",action:"purge",titles:a.join("|"),forcelinkupdate:!0}).done((({purge:e})=>{for(const{title:t}of e)this.progressChange(t,"success")})).catch((e=>{for(const t of a)this.progressChange(t,"fail",e)})),i+5<t.length&&(n.includes("noratelimit")?await k(1e3):await k(2e3))}}async action(){this.progressBar.clear(),0===this.typeSelectInput.getValue().length&&mw.notify("请选择页面类型。"),this.failList=[],this.changeList=[],this.complete=0,this.running=!0,this.$header.addClass("oo-ui-pendingElement-pending"),this.actionButton.setDisabled(!0),this.stopButton.setDisabled(!1),this.closeButton.$element.hide(),this.stopButton.$element.show();const e=await this.getList();e.length>0&&mw.notify(`共${e.length}个页面,开始执行${"nulledit"===this.optionType?"零编辑":"清除缓存"}……`);for(const t of e)this.progressBar.addScale(t);this.updateSize(),"nulledit"===this.optionType?await this.nullEdit(e):await this.purge(e),this.failList.length>0&&OO.ui.alert($(`<div>${this.failList.join("、")}<br>可能页面受到保护,或编辑被过滤器拦截,请手动检查。</div>`),{title:"提示",size:"medium"}),this.changeList.length>0&&OO.ui.alert($(`<div>${this.changeList.join("、")}。<br>被意外更改,请手动撤回或回退`),{title:"警告",size:"medium"}),this.$header.removeClass("oo-ui-pendingElement-pending"),this.actionButton.setDisabled(!1),this.closeButton.$element.show(),this.stopButton.$element.hide()}}const a=$(document.body),s=new OO.ui.WindowManager({id:"one-key-purge"});a.append(s.$element);const o=new i({size:"large"});s.addWindows([o]),$(mw.util.addPortletLink("p-cactions","javascript:void(0)","批量清除缓存","ca-okp")).on("click",(async()=>{if(!n.length)try{n=await mw.user.getRights()}catch(e){mw.notify(`获取用户信息出错:${e}`,{type:"error"})}$("#mw-notification-area").appendTo("body"),s.openWindow(o),a.css("overflow","auto")}))})()))})()})();
// </nowiki>