User:Phlsph7/AlphabeticToolbox.js

From Wikipedia, the free encyclopedia
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 Wikipedia:Bypass your cache.
// sort the links in the toolbox alphabetically
$(document).ready(function() {
	setTimeout(function(){
		const toolbox = document.getElementById('p-tb');
		const list = toolbox.getElementsByTagName('ul')[0];
		const items = Array.from(list.getElementsByTagName('li'));
		items.sort(function(a, b) {
			var textA = a.textContent.trim().toUpperCase();
			var textB = b.textContent.trim().toUpperCase();
			return textA.localeCompare(textB);
		});
	
		// Remove existing items and append sorted items
		while (list.firstChild) {
			list.removeChild(list.firstChild);
		}
		items.forEach(function(item) {
			list.appendChild(item);
		});
		
		// hide unnecessary items
		const hideItems = `Cite this page, Download QR code, Edit interlanguage links, Expand all, Get shortened URL, Permanent link, Related changes, Special pages, What links here, Wikidata item, Add interlanguage links`.split(', ');
		for(let item of items){
			if(hideItems.includes(item.textContent)){
				item.style.display = 'none';
			}
		}
	}, 500);
});