User:Aaron Liu/NoPiper.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Aaron Liu/NoPiper. |
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;
const 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)
api.get({
titles: match[3], redirects: true,
action: 'query', format: 'json', formatversion: 2
}).then(pipe => {
console.log(JSON.stringify(pipe))
pipe=pipe.query.redirects;
if(typeof pipe!=="undefined"){
pipe=pipe[0];
api.get({
titles: match[1], redirects: true,
action: 'query', format: 'json', formatversion: 2
}).then(link=> {
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]+"]]");
}
});
}
}); // there has got to be a better way to extract data from promises
}
api.get({titles: "Main Page"}).done(() => {return str;});
}