Jump to content

User:Eejit43/scripts/displaytitle-editor.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
// Note: This script was compiled and minified from TypeScript. For a more readable version, see https://github.com/Eejit43/wikipedia-scripts/blob/main/scripts/displaytitle-editor.ts

"use strict";(()=>{mw.loader.using(["mediawiki.util","oojs-ui-core","oojs-ui.styles.icons-editing-core"],()=>{if(mw.config.get("wgNamespaceNumber")<0||!mw.config.get("wgIsProbablyEditable"))return;mw.util.addCSS(`
#displaytitle-edit-button {
    margin-right: 0;
    margin-left: 3px;
    font-size: 15px;
}

#displaytitle-edit-box {
    display: inline-block;
    ${mw.config.get("skin")==="modern"?"margin-top: 2px;":""}
    margin-bottom: 2px;
    width: 200px;
    max-width: 200px;
    font-size: 15px;
}`),mw.config.get("skin")==="modern"&&mw.util.addCSS(`
#mw_header {
    height: 2.5em;
}

#p-personal {
    top: 2.5em;
}

#mw_main {
    margin-top: 4em;
}`);let i=new OO.ui.ButtonWidget({icon:"edit",framed:!1,id:"displaytitle-edit-button"});i.on("click",async()=>{i.setDisabled(!0),mw.config.get("skin")==="modern"&&mw.util.addCSS(`
#mw_header {
    height: 3em;
}

#p-personal {
    top: 3em;
}

#mw_main {
    margin-top: 4.5em;
}`);let s=mw.config.get("wgPageName").replaceAll("_"," "),e=new OO.ui.TextInputWidget({placeholder:s,id:"displaytitle-edit-box"});e.on("enter",async()=>{e.setDisabled(!0),e.pushPending(),await new mw.Api().edit(mw.config.get("wgPageName"),a=>{let t=a.content.replaceAll(/{{\s*displaytitle\s*:\s*(.*?)\s*}}\n?/gi,"");if(!e.getValue()||e.getValue().replaceAll("_"," ")===s)return{text:t,summary:"Removing DISPLAYTITLE (via [[User:Eejit43/scripts/displaytitle-editor|script]])"};let o=t===a.content;return/{{short description/i.test(t)?{text:t.replace(/{{short description(.*?)}}/i,`{{short description$1}}
{{DISPLAYTITLE:${e.getValue()}}}`),summary:`${o?"Adding DISPLAYTITLE of":"Changing DISPLAYTITLE to"} "${e.getValue()}" (via [[User:Eejit43/scripts/displaytitle-editor|script]])`}:{text:`{{DISPLAYTITLE:${e.getValue()}}}
${t}`,summary:`${o?"Adding DISPLAYTITLE of":"Changing DISPLAYTITLE to"} "${e.getValue()}" (via [[User:Eejit43/scripts/displaytitle-editor|script]])`}}),mw.notify("Successfully updated DISPLAYTITLE, reloading...",{type:"success"}),window.location.reload()}),e.setDisabled(!0),e.pushPending(),i.$element[0].after(e.$element[0]);let n=(await new mw.Api().get({action:"query",formatversion:"2",prop:"revisions",rvprop:"content",rvslots:"main",titles:mw.config.get("wgPageName")})).query.pages[0].revisions[0].slots.main.content.match(/{{\s*displaytitle\s*:\s*(.*?)\s*}}/gi);n&&e.setValue(n.at(-1).replace(/{{\s*displaytitle\s*:\s*(.*?)\s*}}/i,"$1")),e.setDisabled(!1),e.popPending()}),document.querySelector("#firstHeading")?.append(i.$element[0])});})();

// </nowiki>
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc2NyaXB0cy9kaXNwbGF5dGl0bGUtZWRpdG9yLnRzIl0sCiAgInNvdXJjZXNDb250ZW50IjogWyJpbXBvcnQgdHlwZSB7IEFwaVF1ZXJ5UmV2aXNpb25zUGFyYW1zIH0gZnJvbSAndHlwZXMtbWVkaWF3aWtpL2FwaV9wYXJhbXMnO1xuaW1wb3J0IHR5cGUgeyBQYWdlUmV2aXNpb25zUmVzdWx0IH0gZnJvbSAnLi4vZ2xvYmFsLXR5cGVzJztcblxubXcubG9hZGVyLnVzaW5nKFsnbWVkaWF3aWtpLnV0aWwnLCAnb29qcy11aS1jb3JlJywgJ29vanMtdWkuc3R5bGVzLmljb25zLWVkaXRpbmctY29yZSddLCAoKSA9PiB7XG4gICAgaWYgKG13LmNvbmZpZy5n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Gl0Qm94LmdldFZhbHVlKCkucmVwbGFjZUFsbCgnXycsICcgJykgPT09IGFjdHVhbFRpdGxlKVxuICAgICAgICAgICAgICAgICAgICByZXR1cm4geyB0ZXh0LCBzdW1tYXJ5OiAnUmVtb3ZpbmcgRElTUExBWVRJVExFICh2aWEgW1tVc2VyOkVlaml0NDMvc2NyaXB0cy9kaXNwbGF5dGl0bGUtZWRpdG9yfHNjcmlwdF1dKScgfTtcblxuICAgICAgICAgICAgICAgIGNvbnN0IGlzQWRkZWQgPSB0ZXh0ID09PSByZXZpc2lvbi5jb250ZW50O1xuXG4gICAgICAgICAgICAgICAgcmV0dXJuIC97e3Nob3J0IGRlc2NyaXB0aW9uL2kudGVzdCh0ZXh0KVxuICAgICAgICAgICAgICAgICAgICA/IHtcbiAgICAgICAgICAgICAgICAgICAgICAgICAgdGV4dDogdGV4dC5yZXBsYWNlKFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgL3t7c2hvcnQgZGVzY3JpcHRpb24oLio/KX19L2ksXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICBge3tzaG9ydCBkZXNjcmlwdGlvbiQxfX1cXG57e0RJU1BMQVlUSVRMRToke2VkaXRCb3guZ2V0VmFsdWUoKX19fWAsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICksXG4gICAgICAgICAgICAgICAgICAgICAgICAgIHN1bW1hcnk6IGAke2lzQWRkZWQgPyAnQWRkaW5nIERJU1BMQVlUSVRMRSBvZicgOiAnQ2hhbmdpbmcgRElTUExBWVRJVExFIHRvJ30gXCIke2VkaXRCb3guZ2V0VmFsdWUoKX1cIiAodmlhIFtbVXNlcjpFZWppdDQzL3NjcmlwdHMvZGlzcGxheXRpdGxlLWVkaXRvcnxzY3JpcHRdXSlgLFxuICAgICAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICAgICAgOiB7XG4gICAgICAgICAgICAgICAgICAgICAgICAgIHRleHQ6IGB7e0RJU1BMQVlUSVRMRToke2Vk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