משתמש:מושך בשבט/sortcategories.js

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
// See [[User:Alex 21/script-categoriessort.js]], original script by [[User:Alex 21]]
$(function($) {
	/*
	if (
			config.mw.wgNamespaceNumber == 1 || //talk page
			config.mw.wgArticleId === 0 || // Page doesn't exist
	) {
		return;
	} else (
	)
	*/
	mw.loader.using( ['mediawiki.util'] ).then( function () {
		var portletlink = mw.util.addPortletLink('p-tb', '#', 'Sort categories');
		$(portletlink).click( function(e) {
			e.preventDefault();
			// Determine if we need to go to the editing page.
			var loc = window.location.href; 
			if (loc.indexOf('action=edit') < 0 && loc.indexOf('action=submit') < 0) {
				alert("Go to the edit page to use this script.");
			} else {
				// Get textbox value
				var wpTextbox1 = document.getElementById('wpTextbox1');
				var wpTextbox1_V = wpTextbox1.value;
				
				// Categories to check
				var category = "[[Category:";
				var categoryA = category+"A ";
				var categoryAn = category+"An ";
				var categoryThe = category+"The ";
				var categoryEponymous = category+mw.config.get("wgTitle");
				
				// Get the text up to the start of the categories, and then all of the text with the categories.
				var categoriesStart = wpTextbox1_V.indexOf(category);
				var textBeforeCategories = wpTextbox1_V.substr(0, categoriesStart).trim();
				var textWithCategories = wpTextbox1_V.substr(categoriesStart).trim();
				
				// Categories should be on new lines, so split by new line, sort alphabetically with a few checks, then join again with new lines.
				// Checks: eponymous categories are listed first; categories are sorted without preceding "The"
				var splitCategories = textWithCategories.split("\n");
				splitCategories.sort(function(a, b) {
					if (a.substr(0, categoryEponymous.length) == categoryEponymous) return -1e8;
					if (b.substr(0, categoryEponymous.length) == categoryEponymous) return 1e8;
					
					if (a.substr(0, categoryThe.length) == categoryThe) a = a.replace(categoryThe, category);
					if (b.substr(0, categoryThe.length) == categoryThe) b = b.replace(categoryThe, category);
					
					if (a.substr(0, categoryA.length) == categoryA) a = a.replace(categoryA, category);
					if (b.substr(0, categoryA.length) == categoryA) b = b.replace(categoryA, category);
					
					if (a.substr(0, categoryAn.length) == categoryAn) a = a.replace(categoryAn, category);
					if (b.substr(0, categoryAn.length) == categoryAn) b = b.replace(categoryAn, category);
					
					return a.localeCompare(b);
				});
				textWithCategories = splitCategories.join("\n");
				
				// Merge pre-category text back with the sorted and joined category text, place back in textbok and add summary.
				wpTextbox1.value = textBeforeCategories+"\n\n"+textWithCategories+"\n";
				setoptions(minor = 'true');
				setreason('sorted categories alphabetically via [[User:Epicgenius/sortcategories.js|script]]', 'append');
				doaction('diff');
			}
		});
	});
});