Jump to content

User:Alex 21/script-seasonlist.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.
$(document).ready( function () {
	// If on the edit page, and saved data exists, execute
	if (window.location.href.indexOf('action') >= 0 && localStorage.prevnext) {
		// Textbox value
		var wpTextbox1 = document.getElementById('wpTextbox1');
		var wpTextbox1_V = wpTextbox1.value.split("\n");

		// Variables
		var brackets = 0;
		var infobox = false;
		var prevIncluded = false; var nextIncluded = false;
		var prevnext = JSON.parse(localStorage.prevnext);

		// Iterate through each line
		for (var i = 0; i < wpTextbox1_V.length; i++) {
			var line = wpTextbox1_V[i];
			
			// Check if in infobox or at end of it couting open/closing double curly brackets
			if (line.toLowerCase().indexOf("infobox") >= 0) infobox = true;
			if (line.toLowerCase().indexOf("{{") >= 0) brackets += line.match(/\{\{/g).length;
			if (line.toLowerCase().indexOf("}}") >= 0) brackets -= line.match(/\}\}/g).length;

			if (infobox) {
				// If |prev= or |next= already exist with non-empty entries, take note of this
				if (line.match(/\|\s*prev\s*\=\s*(.+)/)) prevIncluded = true;
				if (line.match(/\|\s*next\s*\=\s*(.+)/)) nextIncluded = true;

				// Remove |season_list= and empty |prev= and |next=
				line = line.replace(/\s*\|\s*season_list\s*=(.*)/g, '');
				if (!prevIncluded) line = line.replace(/\s*\|\s*prev\s*\=(.*)/g, '');
				if (!nextIncluded) line = line.replace(/\s*\|\s*next\s*\=(.*)/g, '');

				// End of infobox
				if (brackets === 0) {
					// Split line into infobox content and }} if }} isn't on a new line
					var line1; var line2;
					if (line.indexOf("}}") >= 0) {
						line1 = line.substr(0, line.indexOf("}}"));
						line2 = line.substr(line.indexOf("}}"));
					} else {
						line1 = "";
						line2 = line;
					}
					
					// If no |next=, add it, then set include-parameter to true
					if (!nextIncluded) {
						line2 = prevnext[1]+line2;
						nextIncluded = true;
					}
					// If no |prev=, add it, then set include-parameter to true
					if (!prevIncluded) {
						line2 = prevnext[0]+line2;
						prevIncluded = true;
					}
					
					// Remerge lines, and end infobox
					line = line1 + (line1 ? "\n" : "") + line2;
					infobox = false;
				}
			}

			// Save line
			wpTextbox1_V[i] = line;
		}

		// Clear saved data
		localStorage.prevnext = '';

		// Update infobox
		var wpTextbox1NewValue = ''; var infobox2 = true;
		for (i = 0; i < wpTextbox1_V.length; i++) {
			if (!infobox2 || (infobox2 && wpTextbox1_V[i])) wpTextbox1NewValue += wpTextbox1_V[i]+"\n";
			if (wpTextbox1_V[i].trim().substr(-3) == "\n}}") infobox2 = false;
		}
		wpTextbox1.value = wpTextbox1NewValue;

		// Add summary and alert
		document.getElementById('wpSummary').value += "Replaced deprecated season_list with prev/next via [[User:Alex 21/script-seasonlist|script]].";
		alert("Completed!");
	}
});
$(function($) {
	setTimeout(function() {
		$.when( mw.loader.using( ['mediawiki.util']), $.ready ).then(function() {
		var portletlink = mw.util.addPortletLink('p-tv', '#', 'Remove season list');
			$(portletlink).click( function(e) {
				e.preventDefault();
				// Not on editing page
				if (window.location.href.indexOf('action') < 0) {
					// Variables
					var prevL = ''; var nextL = '';
					var prevN = ''; var nextN = '';
		
					// Find season list
					var noprint = document.getElementsByClassName('noprint'); var k = 0;
					while (noprint[k].tagName != "TR") k++;
					var NavContent = noprint[k].getElementsByTagName('OL')[0].children;
		
					// Iterate through the elements of season list
					for (var i = 0; i < NavContent.length; i++) {
						// Current article
						if (NavContent[i].children[0].className.indexOf('selflink') >= 0) {
							console.log(NavContent[i].children[0]);
							// Previous episode
							if (NavContent[i-1]) {
								prevL = NavContent[i-1].children[0].title;
								prevN = NavContent[i-1].children[0].innerHTML;
							}
							// Next episode
							if (NavContent[i+1]) {
								nextL = NavContent[i+1].children[0].title;
								nextN = NavContent[i+1].children[0].innerHTML;
							}
						}
					}
					
					// Data to save
					var prevnext = [];
					prevnext[0] = ("| prev = "+(prevL ? ( prevL == prevN ? "[["+prevL+"]]" : "[["+prevL+"|"+prevN+"]]") : "")+"\n").replace(/\[\[\]\]/g, '');
					prevnext[1] = ("| next = "+(nextL ? ( nextL == nextN ? "[["+nextL+"]]" : "[["+nextL+"|"+nextN+"]]") : "")+"\n").replace(/\[\[\]\]/g, '');
		
					localStorage.prevnext = JSON.stringify(prevnext);
		
					// Missing episodes due to previous season finale/next season premiere not being linked
					if (!prevL) alert("Previous link missing! Premiere episode? Manual entry required.");
					if (!nextL) alert("Next link missing! Finale episode? Manual entry required.");
		
					// Direct to editing page to automatically execute edits
					window.location = window.location.href.substr(0, window.location.href.indexOf('#'))+"?action=edit";
				}
			});
		});
	},500);
});