User:Alex 21/script-seriesoverview.js

Source: 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.
// <nowiki>
$(function($) {
	setTimeout(function() {
		$.when( mw.loader.using( ['mediawiki.util']), $.ready ).then(function() {
			var portletlink = mw.util.addPortletLink('p-tv', '#', 'Series overview');
			$(portletlink).click( function(e) {
				e.preventDefault();
				// Find overview table
				var overview = document.getElementById('seriesoverview');
				if (overview === null) {
					var k = 0;
					var alltables = document.getElementById('mw-content-text').getElementsByTagName('table');
					overview = alltables[k];
					var className = overview.className;
					while (so_tableIndexOf(className,"ambox") || so_tableIndexOf(className,"infobox") || so_tableIndexOf(className,"mw-collapsible")) { 
						k++;
						overview = alltables[k];
						className = overview.className;
					}
				}
				
				// Define parameters
				var df = 0; var _i = 1;
				var endExist = false; var episodesCol;
				var SpecialSplit = [' ',' ']; // 0: Special; 1: Split
				
				var headerBase = 3; var network_double = 0;
				var network_single = 0; var infoheaderonly = 0; var auxheaders = 0;
				
				var dateReg = /(([^\s]*)\s)?(([^\s,]*))?.*(\d{4})/;
				
				// Begin template
				var _template_head = "{{Series overview\n";
				var _template = _template_head;
				
				// Aux variables - see where episodes column is located
				for (var a = 1; a < overview.rows[0].cells.length; a++) {
					if (overview.rows[0].cells[a].innerHTML.indexOf('Episodes') != -1) {
						episodesCol = a;
						break;
					}
				}
				auxheaders = episodesCol-1;
				headerBase += auxheaders;
				
				if (auxheaders > 0) {
					for (a = 1; a < episodesCol; a++) {
						_template += "| aux"+(String.fromCharCode(64+a))+"         = "+overview.rows[0].cells[a].innerHTML+"\n";
					}
					_template += "\n";
				}
				
				// Find existance of info and network headers/columns
				if (overview.rows[1].cells[2+auxheaders] !== undefined && overview.rows[1+auxheaders].cells[2+auxheaders].innerHTML.toLowerCase() == "network") {
					network_single = 1;
				}
				if (overview.rows[0].cells[3+auxheaders] !== undefined) {
					if (overview.rows[0].cells[3+auxheaders].innerHTML.toLowerCase() == "network") {
						network_double = 1;
						headerBase++;
					} else if (overview.rows[0].cells[3+auxheaders].getAttribute('rowspan') > 1) {
						infoheaderonly = 1;
					}
				}
				var network = network_single || network_double;
				headerBase -= infoheaderonly;
				
				// Check if overview uses simple one-row header
				var onerowheader = (overview.rows[1].cells[1].tagName == "TD" ? 1 : 0);
				headerBase += onerowheader;
				
				// Add infoheader and info headers
				var extraHeadRows = overview.rows[0].cells.length-headerBase;
				var infoRows = overview.rows[1].cells.length-(headerBase-(network_single?0:1))+infoheaderonly;
				
				if (extraHeadRows > 0) {
					var infoheader = overview.rows[0].cells[headerBase+auxheaders].children[0];
					if (infoheader === undefined || infoheader.tagName == "BR") infoheader = overview.rows[0].cells[headerBase+auxheaders].innerHTML;
					if (typeof infoheader != "object") infoheader = infoheader.replace(/\n/g,'');
					else infoheader.innerHTML = infoheader.innerHTML.replace(/\n/g,'');
					
					_template += "| infoheader   = "+so_link(infoheader)+"\n";
				}
				if (infoRows > 0 && !onerowheader && !infoheaderonly) {
					for (a = 0; a < infoRows; a++) {
						_template += "| info"+(String.fromCharCode(65+a))+"        = "+overview.rows[1].cells[a+(headerBase-1)+auxheaders].innerHTML.replace("\n","")+"\n";
					}
				}
				
				// Extra links
				if (extraHeadRows > 0 || infoRows > 0 && !onerowheader) _template += "\n";
				_template = _template.replace(_template_head+"\n",_template_head);
				
				// Begin iteratin through season rows
				for (var i = 2-onerowheader; i < overview.rows.length; i++) {
					// Define parameters
					var startdate, enddate;
					var RC = overview.rows[i].cells;
					SpecialSplit[0] = ' '; var CellAdj = 0;
					
					// 0: Colour; 1: Link, 2: Episodes
					var CLE = ['','',''];
					
					if (SpecialSplit[1] == 'A') {
						SpecialSplit[1] = 'B'; // Was on first half, now on second half
					} else if (RC[0].getAttribute('rowspan') || RC[1].getAttribute('rowspan')) {
						SpecialSplit[1] = 'A'; // Now on first half
					} else {
						SpecialSplit[1] = ' '; // Not a split season
					}
					
					// Code to add after parameter name
					var ThisKey = so_ThisKey(_i,SpecialSplit);
					
					if (SpecialSplit[1] != 'B') {
						// First half or regular season, link and colour added
						// Link
						var linkCell = (RC[1].firstChild.href !== undefined ? RC[1].firstChild : RC[1].firstChild.firstChild);
						if (linkCell && linkCell.tagName != "A") {
							if (typeof linkCell == "object") linkCell = linkCell.textContent ? linkCell.textContent : linkCell.innerText;
							var aT = document.createElement('a');
							aT.innerHTML = linkCell;
							aT.href = '#Season '+linkCell;
							linkCell = aT;
						}
						
						var linkMain;
						var linkCellH = linkCell.href;
						if (!linkCell) {
							linkMain = '';
						} else if (linkCellH.indexOf('index.php') >= 0) {
							linkMain = linkCellH.substr(linkCellH.indexOf('title=')+6, linkCellH.indexOf('&')-(linkCellH.indexOf('title=')+6))+
								(linkCellH.indexOf('#') >= 0 ? linkCellH.substr(linkCellH.indexOf('#')) : '');
						} else {
							linkMain = linkCellH.substr(linkCellH.lastIndexOf('wiki/')+5);
						}
						linkMain = decodeURIComponent(linkMain.replace(/_/g, ' ').replace(/\.([A-Z0-9]{2})/g, '%$1')).split('#');
						
						// Special row
						if (linkCell.innerHTML != _i) {
							_i--;
							SpecialSplit[0] = 'S';
							CellAdj = Math.max(0,RC[1].getAttribute('colspan')-2);
							ThisKey = so_ThisKey(_i,SpecialSplit);
						}
						
						CLE[0] += "| color"+ThisKey+"     = "+(RC[0].bgColor?RC[0].bgColor:RGBSTRtoHEX(RC[0].style.background?RC[0].style.background:RC[0].style.backgroundColor))+"\n";
						CLE[1] += "| link"+_i+SpecialSplit[0]+"       = <includeonly>"+linkMain[0]+"</includeonly>"+(linkMain[1]?"#"+linkMain[1]:"")+"\n";
						if (isNaN(parseInt(so_wiki(linkCell.innerHTML),10))) {
							CLE[1] += "| linkT"+ThisKey+"     = "+so_wiki(linkCell.innerHTML)+"\n";
						}
					} else {
						// Second half, only colour required
						CLE[0] += "| color"+ThisKey+"     = "+(RC[0].bgColor?RC[0].bgColor:RGBSTRtoHEX(RC[0].style.background?RC[0].style.background:RC[0].style.backgroundColor))+"\n";
					}
					
					// Aux variables
					var aux = '';
					if (episodesCol != 1 && SpecialSplit[1] != 'B') {
						for (a = 1; a < episodesCol; a++) {
							if (SpecialSplit[1] == ' ' || (SpecialSplit[1] != ' ' && RC[1+a].getAttribute('rowspan') > 1))
								aux += "| aux"+String.fromCharCode(64+a)+_i+SpecialSplit[0]+"       = "+so_wiki(RC[1+a].innerHTML)+"\n";
							CellAdj--;
						}
					} else {
						CellAdj -= episodesCol-1;
					}
					
					// Total episodes
					if (SpecialSplit[1] != 'B' && so_tba(RC[2-CellAdj].innerHTML) && !isNaN(parseInt(so_ref(RC[2-CellAdj],false),10))) {
						CLE[2] += "| episodes"+_i+SpecialSplit[0]+"   = "+so_ref(RC[2-CellAdj],false)+"\n";
					}
					
					// Colour, Link, Episodes
					_template += ( SpecialSplit[1] != ' ' ? CLE[1]+aux+CLE[2]+CLE[0] : CLE[0]+CLE[1]+aux+CLE[2] );
					
					// Aux variables split season
					if (episodesCol != 1 && SpecialSplit[1] != ' ') {
						for (a = 1; a < episodesCol; a++) {
							var cell = RC[SpecialSplit[1] == 'B' ? a-CellAdj-1 : a-CellAdj];
							_template += "| aux"+String.fromCharCode(65+a)+ThisKey+"      = "+so_wiki(cell.innerHTML)+"\n";
						}
					}
					
					// Split season episodes
					if (SpecialSplit[1] != ' ') {
						CellAdj += (SpecialSplit[1] == 'B' ? 2 : 0);
						if (so_tba(RC[3-CellAdj].innerHTML) && !isNaN(parseInt(so_ref(RC[3-CellAdj],false),10))) {
							_template += "| episodes"+ThisKey+"  = "+so_ref(RC[3-CellAdj],false)+"\n";
						}
						CellAdj--;
					}
					
					// Start and end dates
					var temp;
					var reS = dateReg.exec(so_dateref(RC[3-CellAdj]));
					if (reS) {
						var reS_x = [reS[4],reS[2],reS[5]];
						if (reS_x[0] && isNaN(parseInt(reS_x[0],10))) {
							temp = reS_x[0]; reS_x[0] = reS_x[1]; reS_x[1] = temp; df = 1;
						}
						startdate = so_startend(reS_x,"Start",df);
					}
					
					var reE = '';
					if (RC[4-CellAdj] !== undefined && RC[3-CellAdj].getAttribute('colspan') <= 1) {
						reE = dateReg.exec(so_dateref(RC[4-CellAdj]));
						if (reE) {
							var reE_x = [reE[4],reE[2],reE[5]];
							if (reE_x[0] && isNaN(parseInt(reE_x[0],10))) {
								temp = reE_x[0]; reE_x[0] = reE_x[1]; reE_x[1] = temp; df = 1;
							}
							enddate = so_startend(reE_x,"End",df);
							endExist = true;
						}
					}
					if (RC[3-CellAdj].getAttribute('colspan') > 1) { CellAdj++; }
					
					if (reS) _template += "| start"+ThisKey+"     = "+startdate+so_ref(RC[3-CellAdj],true)+"\n";
					if (reE) _template += "| end"+ThisKey+"       = "+enddate+so_ref(RC[4-CellAdj],true)+"\n";
					else if (SpecialSplit[0] != 'S' && RC[3-CellAdj].getAttribute('colspan') > 1) _template += "| end"+ThisKey+"       = start\n";
					
					// Network
					if (network && RC[5-CellAdj] !== undefined && RC[5-CellAdj].getAttribute('rowspan')) {
						_template += "| network"+ThisKey+"   = "+so_link(RC[5-CellAdj].children[0])+"\n";
						CellAdj++;
					}
					
					// Info cells
					if (infoRows > 0) {
						for (var j = 0; j < 26; j++) {
							var infoCell = RC[5+j-CellAdj];
							if (infoCell && so_tba(so_ref(infoCell,false))) {
								_template += "| info"+String.fromCharCode(65+j)+ThisKey+"     = "+so_ref(infoCell,false)+"\n";
							}
						}
					}
					
					// New lines and updating split season variables
					if (i < overview.rows.length-1 && SpecialSplit[1] != 'A') _template += "\n";
					
					if (SpecialSplit[1] == 'B') SpecialSplit[1] = ' ';
					else if (SpecialSplit[1] == 'A') _i--;
					
					_i++;
				}
				
				// Released parameters
				var released = '';
				for (a = 1; a < overview.rows[0].cells.length; a++) {
					if (overview.rows[0].cells[a].innerHTML.indexOf('release') != -1) {
						released += "| released     = y\n";
						break;
					}
				}
				if (!endExist) {
					released += "| allreleased  = y\n";
				}
				if (released) _template = _template.replace(_template_head, _template_head+released+"\n");
				
				// Finished
				_template = _template.replace('<small><span style="color: #2C2C2C" title="To be announced">TBA</span></small>','{{TableTBA}}');
				_template += "}}";
				
				copyContent(_template);
				alert("Copied! Now go to the editing page and paste over the raw code.");
				
				// Function: Remove reference
				function so_ref(cell,comment) {
					if (comment) {
						if (cell.innerHTML.indexOf("<sup") < 0) return '';
						else return "<!--Be sure to restore the reference that existed here!-->";
					} else {
						if (cell.innerHTML.indexOf("<sup") < 0) return cell.innerHTML;
						else return cell.innerHTML.replace(/<sup(.*)\/sup\>/g,"<!--Be sure to restore the reference that existed here!-->");
					}
				}
				
				// Function: Raw link to wiki link
				function so_link(link) {
					if (link === undefined || link.href === undefined) return link;
					var nwlink = link.href.replace("https://en.wikipedia.org/wiki/","").replace("_", " ");
					return "[["+(link.innerHTML==nwlink?"":nwlink+"|")+link.innerHTML+"]]";
				}
				
				// Function: Remove reference from data
				function so_dateref(cell) {
					return (cell.innerHTML.indexOf('<')>=0 ? cell.innerHTML.substr(0, cell.innerHTML.indexOf('<')) : cell.innerHTML).split('&nbsp;').join(' ');
				}
				
				// Function: Bold and italic text to wiki
				function so_wiki(text) {
					return text.replace(/<i>(.*)<\/i>/g, "''$1''").replace(/<b>(.*)<\/b>/g, "'''$1'''");
				}
				
				// Function: Dates to wiki templates
				function so_startend(cell,se,df) {
					var monthNumber = new Date(Date.parse(cell[1]+" 1, 2005")).getMonth()+1;
					return "{{"+se+" date|"+cell[2]+
							(!isNaN(monthNumber) && cell[1] !== undefined ? "|"+monthNumber : "")+
							(!isNaN(cell[0]) && cell[0] !== undefined ? "|"+cell[0] : "")+
							(df ? "|df=y" : "")+"}}";
				}
				
				// Function: Chceck if classname contains single class
				function so_tableIndexOf(className,content) {
					return (className.indexOf(content) >= 0);
				}
				
				// Function: Check if text contains TBA, TBD, N/A
				function so_tba(text) {
					text = text.toLowerCase();
					return text.indexOf("tba") < 0 &&  text.indexOf("tbd") < 0 &&  text.indexOf("n/a") < 0;
				}
				
				// Function: Get value of ThisKey
				function so_ThisKey(_i,SpecialSplit) {
					var ThisKey = _i+SpecialSplit[1]+SpecialSplit[0];
					if (ThisKey.indexOf(' ') != -1) {
						var spaces = ThisKey.length-ThisKey.replace(/\s/g,'').length;
						ThisKey = ThisKey.replace(/\s/g,'');
						for (a = 0; a < spaces; a++) ThisKey += ' ';
					}
					return ThisKey;
				}
			});
		});
	},600);
});
// </nowiki>