Jump to content

User:JPxG/Unbreaker.js

From Wikipedia, the free encyclopedia
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.
// All this does is fix "br" tags that mess up the syntax highlighter, and replace them with correct "br /" tags.

function doUnbreak() {
	//alert("This part works");
	tbox = String(document.editform.wpTextbox1.value);
	// Get the text from the edit box and store it as "tbox".
	//alert("This part works too");
	tbox = tbox.replaceAll("<br>", "<br />");
	tbox = tbox.replaceAll("<BR>", "<br />");
	tbox = tbox.replaceAll("<Br>", "<br />");
	tbox = tbox.replaceAll("<bR>", "<br />");
	
	tbox = tbox.replaceAll("<br >", "<br />");
	tbox = tbox.replaceAll("<BR >", "<br />");
	tbox = tbox.replaceAll("<Br >", "<br />");
	tbox = tbox.replaceAll("<bR >", "<br />");
	
	tbox = tbox.replaceAll("<br/>", "<br />");
	tbox = tbox.replaceAll("<BR/>", "<br />");
	tbox = tbox.replaceAll("<Br/>", "<br />");
	tbox = tbox.replaceAll("<bR/>", "<br />");
	
	tbox = tbox.replaceAll("<BR />", "<br />");
	tbox = tbox.replaceAll("<Br />", "<br />");
	tbox = tbox.replaceAll("<bR />", "<br />");
	
	//alert("This part works also also");
	document.editform.wpTextbox1.value = tbox;
	//alert("And this part");
} // function to replace the stuff with the other stuff

function doSyn() {
	tbox = String(document.editform.wpTextbox1.value);
	tbox = tbox.replaceAll(`<code><nowiki>`, `<syntaxhighlight lang="wikitext" inline>`);
	tbox = tbox.replaceAll(`</nowiki></code>`, `</syntaxhighlight>`);
	tbox = tbox.replaceAll(`<syn>`, `<syntaxhighlight lang="wikitext" inline>`);
	tbox = tbox.replaceAll(`</syn>`, `</syntaxhighlight>`);
	document.editform.wpTextbox1.value = tbox;
} // function to replace the stuff with the other stuff

addOnloadHook(function() {
	if (document.editform) {
		mw.util.addPortletLink("p-cactions", "javascript:doUnbreak()", "Unbreak", "ca-unbreak", "Unbreak line breaks", "");
		mw.util.addPortletLink("p-cactions", "javascript:doSyn()", "syn", "ca-syn", "shighlight", "");
	}
}); // onloadhook
// </nowiki>