User:Guywan/Scripts/RefCruncher.js: Difference between revisions

Content deleted Content added
mNo edit summary
mNo edit summary
 
(15 intermediate revisions by the same user not shown)
Line 5:
if(mw.config.get("wgAction") !== "edit") return;
const debug = false;
console.log("Running RefCruncher");
if(debug) console.log("Running RefCruncher");
window.rc_refsCrunched = false;
window.rc_refs = [];
window.rc_refsCrunched = false;
document.onkeyup = function(e)
rc_refs = [];
// Add key handler.
window.addEventListener("keydown", e =>
{
if(e.ctrlKey && e.altKey && e.which == 82)
{
run();
if(window.rc_refsCrunched)
{
console.log("Uncrunching refs.");
uncrunchRefs();
window.rc_refsCrunched = false;
}
else
{
console.log("Crunching refs.");
crunchRefs();
window.rc_refsCrunched = true;
}
}
});
function run()
{
if(debug) console.log("Crunching refs.Activate");
if(window.rc_refsCrunched)
{
rc_refsCrunched = uncrunchRefs();
}
else
{
window.rc_refsCrunched = falsecrunchRefs();
}
}
function crunchRefs()
Line 40 ⟶ 42:
var counter = 0;
text = text.replace(/(<ref.*?\/>)|(<ref.*?>(.|\s)*?<\/ref>)/g, function(match) =>
{
if(debug) console.log(match);
window.rc_refs.push(match);
return `{<ref# ${counter++}}/>`;
});
console.log(text);
txtarea.value = text;
return true;
}
Line 60 ⟶ 62:
var text = txtarea.value;
text = text.replace(/\{<ref# (\d+)\}/>/g, function(match, p1) =>
{
if(debug) console.log(match);
return window.rc_refs.pop([Number(p1))];
});
window.rc_refs = [];
txtarea.value = text;
}
return false;
}
});