User:Novem Linguae/Scripts/NoPiper.js

async function noPipe(str){ if(typeof noPipeAll !== "undefined"){ if(noPipeAll===false){ console.log("why you noPipeAll = false to me"); if(mw.config.get('wgNamespaceNumber') !== 0) return str; }	}else if(mw.config.get('wgNamespaceNumber') !== 0) // set noPipeAll = true to use this script outside mainspace return str; let api = new mw.Api({		ajax: { headers: { 'Api-User-Agent': 'NoPiper/1.0' } },		parameters: { redirects: true, action: 'query',					format:'json', formatversion: 2}	}); // Possible optimization: remove duplicates and do replaceall? for(const match of str.matchAll(/\[\[(?!Category:):?([^\]#]+)(#[^\]#]+)?\|([^\[\]\n\uE000]+)\]\]/g)){ console.log(match) let pipe = await api.get({			titles: match[3], redirects: true,			action: 'query', format: 'json', formatversion: 2		});

console.log(JSON.stringify(pipe)) pipe=pipe.query.redirects; if(typeof pipe!=="undefined"){ pipe=pipe[0]; let link = await api.get({				titles: match[1], redirects: true,				action: 'query', format: 'json', formatversion: 2			}); console.log(JSON.stringify(link)) link=link.query; if(typeof link.redirects!=="undefined"){ if(typeof match[2]!=="undefined"){ link.redirects[0].tofragment=match[2].replace("_", " "); }				if(JSON.stringify(link.redirects[0])===JSON.stringify(pipe)){ str.replace(match[0], ""+match[3]+""); //technically one could cause a bug by putting a # in here but who'd be crazy enough to do that? }			}else if(link.pages[0].title===pipe.to){ if(typeof match[2]!=="undefined") //if has target section we need to go to, evaluate if target section and our redirect section are equal if(typeof pipe.tofragment!=="undefined") // null safety... why can't we just compare undefineds if(pipe.tofragment===match[2]){ str.replace(match[0], ""+match[3]+""); }				else // if any of the above are false, don't replace; otherwise, we'd continue after already replacing str.replace(match[0], ""+match[3]+""); }		}	}	let str2 = await api.get({titles: "Main Page"}); return str2; }