User:Guywan/Scripts/RefCruncher.js

This is an old revision of this page, as edited by Guywan (talk | contribs) at 14:20, 3 December 2019 (Created page with '// Category:Wikipedia scripts // <nowiki> $(function() { if(mw.config.get("wgAction") !== "edit") return; window.rc_refsCrunched = false; window.rc_refs...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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 Wikipedia:Bypass your cache.
// [[Category:Wikipedia scripts]]
// <nowiki>
$(function()
{
	if(mw.config.get("wgAction") !== "edit") return;
	
	window.rc_refsCrunched = false;
	window.rc_refs = [];
	
	document.onkeyup = function(e)
	{
		if(e.ctrlKey && e.altKey && e.which == 82)
		{
			if(window.rc_refsCrunched)
			{
				uncrunchRefs();
				
				window.rc_refsCrunched = false;
			}
			else
			{
				crunchRefs();
				
				window.rc_refsCrunched = true;
			}
		}
	};
	
	function crunchRefs()
	{	
		const txtarea = document.getElementById("wpTextbox1");
		
		var text = txtarea.value;
		var counter = 0;
		
		text.replace(/<ref.*?>.*?<\/ref>/g, function(match)
		{
			window.rc_refs.push(match);
			
			return `{ref#${counter++}}`;
		});
		
		txtarea.value = text;
	}
	
	function uncrunchRefs()
	{
		const txtarea = document.getElementById("wpTextbox1");
		
		var text = txtarea.value;
		
		text.replace(/{ref#(\d+)}/g, function(match, p1)
		{
			return window.rc_refs.pop(Number(p1));
		});
		
		txtarea.value = text;
	}
});
// </nowiki>