User:Eejit43/scripts/sync-redirect.js

// // Note: This script was compiled and minified from TypeScript. For a more readable version, see https://github.com/Eejit43/wikipedia-scripts/blob/main/scripts/sync-redirect.ts

"use strict";mw.loader.using(["mediawiki.util"],async=>{if(!mw.Title.isTalkNamespace(mw.config.get("wgNamespaceNumber")))return;const r=await new mw.Api.get({action:"query",formatversion:"2",prop:["info","revisions"],rvprop:"content",rvslots:"main",titles:`${mw.config.get("wgFormattedNamespaces")[mw.config.get("wgNamespaceNumber")-1]}:${mw.config.get("wgTitle")}`});if(!r.query.pages[0].redirect)return;mw.util.addPortletLink(mw.config.get("skin")==="minerva"?"p-tb":"p-cactions","#","Sync with main page redirect","sync-redirect").addEventListener("click",async g=>{g.preventDefault,mw.notify("Editing...",{tag:"sync-redirect-notification"});const n=r.query.pages[0].revisions[0].slots.main.content,a=/#redirect:? *\[\[(.+)]]/i.exec(n)?.[1].replaceAll("_"," ").split("|")[0].split("#")[0].trim;if(!a)return mw.notify("Failed to parse redirect target!",{type:"error",tag:"sync-redirect-notification"});const o=new DOMParser.parseFromString(a,"text/html").documentElement.textContent;if(!o)return mw.notify("Failed to parse redirect target!",{type:"error",tag:"sync-redirect-notification"});const i=mw.Title.newFromText(o);if(!i)return mw.notify("Failed to parse redirect target!",{type:"error",tag:"sync-redirect-notification"});const e=i.getMainText,c=//i.test(n),t=mw.config.get("wgFormattedNamespaces")[i.getNamespaceId+1];await new mw.Api.edit(mw.config.get("wgPageName"),=>({text:`#REDIRECT ${t}:${e}${c?`

`:""}`,summary:`Sync redirect with main page, to ${t}:${e} (via script)`,minor:!0})).catch(async(m,d)=>{m==="nocreate-missing"?await new mw.Api.create(mw.config.get("wgPageName"),{summary:`Create redirect matching main page, to ${t}:${e} (via script)`},`#REDIRECT ${t}:${e}${c?`

`:""}`).catch((p,w)=>{mw.notify(`Failed to redirect page: ${w?.error.info??"Unknown error"} (${p})`,{type:"error",tag:"sync-redirect-notification"})}):mw.notify(`Failed to redirect page: ${d?.error.info??"Unknown error"} (${m})`,{type:"error",tag:"sync-redirect-notification"})}),mw.notify("Successfully redirected page, reloading...",{type:"success",tag:"sync-redirect-notification"});const s=new URL(window.location.href);s.searchParams.set("redirect","no"),window.location.href=s.href})});

// //# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc2NyaXB0cy9zeW5jLXJlZGlyZWN0LnRzIl0sCiAgInNvdXJjZXNDb250ZW50IjogWyJpbXBvcnQgeyBBcGlRdWVyeUluZm9QYXJhbXMsIEFwaVF1ZXJ5UmV2aXNpb25zUGFyYW1zIH0gZnJvbSAndHlwZXMtbWVkaWF3aWtpL2FwaV9wYXJhbXMnO1xuaW1wb3J0IHsgTWVkaWFXaWtpRGF0YUVycm9yLCBQYWdlUmV2aXNpb25zUmVzdWx0IH0gZnJvbSAnLi4vZ2xvYmFsLXR5cGVzJztcblxubXcubG9hZGVyLnVzaW5nKFsnbWVkaWF3aWtpLnV0aWwnXSwgYXN5bmMgKCkgPT4ge1xuICAgIGlmICghbXcuVGl0bGUuaXNUYWxrTmFtZXNwYWNlKG13LmNvbmZpZy5nZXQoJ3dnTmFtZXNwYWNlTnVtYmVyJykpKSByZXR1cm47XG4gICAgY29uc3QgbWFpblBhZ2VJbmZvUmV2aXNpb25zID0gKGF3YWl0IG5ldyBtdy5BcGkoKS5nZXQoe1xuICAgICAgICBhY3Rpb246ICdxdWVyeScsXG4gICAgICAgIGZvcm1hdHZlcnNpb246ICcyJyxcbiAgICAgICAgcHJvcDogWydpbmZvJywgJ3JldmlzaW9ucyddLFxuICAgICAgICBydnByb3A6ICdjb250ZW50JyxcbiAgICAgICAgcnZzbG90czogJ21haW4nLFxuICAgICAgICB0aXRsZXM6IGAke213LmNvbmZpZy5nZXQoJ3dnRm9ybWF0dGVkTmFtZXNwYWNlcycpW213LmNvbmZpZy5nZXQoJ3dnTmFtZXNwYWNlTnVtYmVyJykgLSAxXX06JHttdy5jb25maWcuZ2V0KCd3Z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