User:Shahadat/RefCruncher.js
Jump to navigation
Jump to search
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 Justapedia:Bypass your cache.
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", or "iusc", take note that this causes you to dynamically load a remote script, which could be changed by others. If you are unsure whether code you are adding to this page is safe, you can ask at technical help. The code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Shahadat/RefCruncher. |
// [[Category:Justapedia scripts]]
// <nowiki>
$(function()
{
if(mw.config.get("wgAction") !== "edit") return;
const debug = false;
if(debug) console.log("Running");
rc_refsCrunched = false;
rc_refs = [];
// Add key handler.
window.addEventListener("keydown", e =>
{
if(e.ctrlKey && e.altKey && e.which == 82)
{
run();
}
});
function run()
{
if(debug) console.log("Activate");
if(rc_refsCrunched)
{
rc_refsCrunched = uncrunchRefs();
}
else
{
rc_refsCrunched = crunchRefs();
}
}
function crunchRefs()
{
const txtarea = document.getElementById("wpTextbox1");
var text = txtarea.value;
var counter = 0;
text = text.replace(/(<ref.*?\/>)|(<ref.*?>(.|\s)*?<\/ref>)/g, match =>
{
if(debug) console.log(match);
rc_refs.push(match);
return `<ref ${counter++}/>`;
});
txtarea.value = text;
return true;
}
function uncrunchRefs()
{
const txtarea = document.getElementById("wpTextbox1");
var text = txtarea.value;
text = text.replace(/<ref (\d+)\/>/g, (match, p1) =>
{
if(debug) console.log(match);
return rc_refs[Number(p1)];
});
window.rc_refs = [];
txtarea.value = text;
return false;
}
});
// </nowiki>