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_Vi];

			

			// 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 = prevnext1+line2;

						nextIncluded = true;

					}

					// If no |prev=, add it, then set include-parameter to true

					if (!prevIncluded) {

						line2 = prevnext0+line2;

						prevIncluded = true;

					}

					

					// Remerge lines, and end infobox

					line = line1 + (line1 ? "\n" : "") + line2;

					infobox = false;

				}

			}



			// Save line

			wpTextbox1_Vi = 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_Vi])) wpTextbox1NewValue += wpTextbox1_Vi+"\n";

			if (wpTextbox1_Vi].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 (noprintk].tagName != "TR") k++;

					var NavContent = noprintk].getElementsByTagName('OL')[0].children;

		

					// Iterate through the elements of season list

					for (var i = 0; i < NavContent.length; i++) {

						// Current article

						if (NavContenti].children0].className.indexOf('selflink') >= 0) {

							console.log(NavContenti].children0]);

							// Previous episode

							if (NavContenti-1]) {

								prevL = NavContenti-1].children0].title;

								prevN = NavContenti-1].children0].innerHTML;

							}

							// Next episode

							if (NavContenti+1]) {

								nextL = NavContenti+1].children0].title;

								nextN = NavContenti+1].children0].innerHTML;

							}

						}

					}

					

					// Data to save

					var prevnext = [];

					prevnext0 = ("| prev = "+(prevL ? ( prevL == prevN ? "[["+prevL+"]]" : "[["+prevL+"|"+prevN+"]]") : "")+"\n").replace(/\[\[\]\]/g, '');

					prevnext1 = ("| 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);

});