Jump to content

User:WikiMasterGhibif/capitalize.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.
var addToolbarButtonsUrl = '//en.wikipedia.org/w/index.php?title='
	+ 'User:V111P/js/addToolbarButtons.js&action=raw&ctype=text/javascript';
// Backlink: [[User:V111P/js/addToolbarButtons.js]]
mw.loader.using('jquery.textSelection'); // it's loaded by default, but just in case
	var textArea = $('#wpTextbox1');

var myToolbarButton = {
	id: 'myButton',
        tooltip:'decapitalize selection',
	callback: function myCallback () 
      {
		var sel = textArea.textSelection('getSelection');
		sel = sel.replace(/\r/g, ''); // IE before 9 doesn't remove the \r's

		// manipulate the text in the sel variable
		sel = sel.replace(/A/g, 'a'); // for example. Replaces space with underscore
		sel = sel.replace(/B/g, 'b');
		sel = sel.replace(/C/g, 'c');
		sel = sel.replace(/D/g, 'd');
		sel = sel.replace(/E/g, 'e');
		sel = sel.replace(/F/g, 'f');
		sel = sel.replace(/G/g, 'g');
		sel = sel.replace(/H/g, 'h');
		sel = sel.replace(/I/g, 'i');
		sel = sel.replace(/J/g, 'j');
		sel = sel.replace(/K/g, 'k');
		sel = sel.replace(/L/g, 'l');
		sel = sel.replace(/M/g, 'm');
		sel = sel.replace(/N/g, 'n');
		sel = sel.replace(/O/g, 'o');
		sel = sel.replace(/P/g, 'p');
		sel = sel.replace(/Q/g, 'q');
		sel = sel.replace(/R/g, 'r');
		sel = sel.replace(/S/g, 's');
		sel = sel.replace(/T/g, 't');
		sel = sel.replace(/U/g, 'u');
		sel = sel.replace(/V/g, 'v');
		sel = sel.replace(/W/g, 'w');
		sel = sel.replace(/X/g, 'x');
		sel = sel.replace(/Y/g, 'y');
		sel = sel.replace(/Z/g, 'z');
		// replace the selected text in the textarea with the new text
		textArea.textSelection('encapsulateSelection', {pre: sel, replace: true}); 
	}
};


if (mw.libs.addToolbarButtons)
	mw.libs.addToolbarButtons(myToolbarButton);
else {
	var tbs = window.toolbarButtonsToAdd = window.toolbarButtonsToAdd || [];
	tbs.push(myToolbarButton);
	mw.loader.load(addToolbarButtonsUrl);
}
//IT WORKS NOW!!!!!