Jump to content

User:Alex 21/script-plotlength.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.
$(function($) {
	setTimeout(function() {
		$.when( mw.loader.using( ['mediawiki.util']), $.ready ).then(function() {
			var portletlink = mw.util.addPortletLink('p-tv', '#', 'Plot lengths');
			$(portletlink).click( function(e) {
				e.preventDefault();
				
				// Months
				var month = [];
				month[0] = "January"; month[1] = "February"; month[2] = "March"; month[3] = "April"; month[4] = "May"; month[5] = "June"; month[6] = "July"; month[7] = "August"; month[8] = "September"; month[9] = "October"; month[10] = "November"; month[11] = "December";
				
				// Type of infobox; if none exist, assume a list-of-episodes page
				var infoboxtype;
				var infobox = document.getElementsByClassName('infobox')[0];
				if (infobox === undefined) {
					infoboxtype = "TV";
				} else {
					while (infobox.tagName != "TH") infobox = infobox.children[0];
					infoboxtype = ((infobox.style.background || infobox.style.backgroundColor) ? "TV" : "FILM");
				}
				
				// Default parameters
				var plotlength = '<span class="plotlength"></span>';
				var dlength; var summary = ''; var i; var d = new Date(); var plotString;
				var firstSummary = document.getElementsByClassName('summary')[0];
				var inPlot = false; var firstElem;
				var alertText = '';
				
				// Possible headers for prose plot sections (e.g. films, season summaries) - please inform if any are missing
				var IDs = ["Plot", "Premise", "Synopsis", "Summary", "Plot summary", "Overview"];
				
				// Go through and check for the possible headers above
				for (var id = 0; id < IDs.length; id++) {
					var summaryElem = document.getElementById(IDs[id]);
					if (summaryElem === null) continue;
					// Header exists
					var nodes = summaryElem.parentNode.parentNode.children;
					for (i = 0; i < nodes.length; i++) {
						// Check if we are in the plot section via header first
						if (nodes[i] == summaryElem.parentNode) {
							inPlot = true;
						} else if (inPlot === true) {
							// In the plot section via header, now collect the whole plot
							if (nodes[i].tagName == 'H2' || nodes[i].tagName == 'DIV') {
								inPlot = false;
							} else {
								summary += nodes[i].innerHTML+' ';
								if (firstElem === undefined & nodes[i].tagName == "P") firstElem = nodes[i];
							}
						}
					}
					// Remove any previous counts, tags, references
					if (summary.lastIndexOf(plotlength) > 0) summary = summary.substr(summary.lastIndexOf(plotlength)+plotlength.length);
					summary = summary.replace(/(\r\n|\n|\r)/gm,"").replace(/<(table|tbody)(.*)\/(table|tbody)\>/g,'');
					summary = summary.replace(/<\/?([^>]*)>/g,'').trim();
					dlength = summary.split(' ').length+1;
					
					if ((infoboxtype == "FILM" && dlength > 700) || (infoboxtype == "TV" && dlength > 400)) {
						// Length is greater than the limit: too long, tag
						if (firstElem) firstElem.innerHTML = "<span style='color:red'><b>("+dlength+")</b></span> "+plotlength+firstElem.innerHTML;
						plotString = "{{Plot|"+(infoboxtype == "FILM" ? "film" : "episode")+"|date="+month[d.getMonth()]+" "+d.getFullYear()+"}}";
						copyContent(plotString);
						alertText += 'The main plot summary is too long per WP:'+infoboxtype+'PLOT.\nCopied to clipboard: '+plotString+'\n';
					} else {
						// Sufficiently short
						if (firstElem) firstElem.innerHTML = "("+dlength+") "+plotlength+firstElem.innerHTML;
						alertText += 'The main plot summary is sufficiently short per WP:'+infoboxtype+'PLOT.\n';
					}
					// Calculates the length of only one plot summary (only one required), so break.
					break;
				}
				
				// Length of episode summaries, if they exist
				var episodes = '';
				var desc = document.getElementsByClassName('description');
				var trs = document.getElementsByTagName("tr");
				var tenperc = 0;
				for (i = 0; i < desc.length; i++) {
					// Remove any previous counts, tags, references, horizontal breaks, new lines
					summary = desc[i].innerHTML.replace(/(\r\n|\n|\r)/gm,"").replace(/<(table|tbody)(.*)\/(table|tbody)\>/g,'');
					if (summary.lastIndexOf(plotlength) > 0) summary = summary.substr(summary.lastIndexOf(plotlength)+plotlength.length);
					if (summary.indexOf('<hr') >= 0) summary = summary.substr(0,summary.indexOf('<hr'));
					summary = summary.replace(/<\/?([^>]*)>/g,'');
					dlength = summary.split(' ').length+1;
					
					if (dlength > 200) {
						// Length is greater than 200: possible too long
						for (var j = 0; j < trs.length; j++) {
							if (desc[i] == trs[j].children[0] && trs[j-1].cells[0]) {
								// Double episode, summaries below 400 are acceptable
								var doubleep = (trs[j-1].cells[0].innerHTML.match(/(\d*)/g).filter(Number).length > 1);
								if (doubleep && dlength <= 400) {
									desc[i].innerHTML = "("+dlength+") "+plotlength+desc[i].innerHTML;
								} else if ((!doubleep && dlength <= 215) || (doubleep && dlength <= 415)) {
									// Allow up to 15 more than maximum for leeway
									desc[i].innerHTML = "<span style='color:green'>("+dlength+")</span> "+plotlength+desc[i].innerHTML;
									tenperc++;
								} else if ((!doubleep && dlength > 215) || (doubleep && dlength > 415)) {
									// Too long, tag
									episodes += trs[j-1].cells[0].innerHTML.replace(/\n/g,'')+" ("+dlength+")\n";
									desc[i].innerHTML = "<span style='color:red'><b>("+dlength+")</b></span> "+plotlength+desc[i].innerHTML;
								}
							}
						}
					} else {
						// Sufficiently short
						desc[i].innerHTML = "("+dlength+") "+plotlength+desc[i].innerHTML;
					}
				}
				
				// Inform of summaries that are over the limit, if any exist
				if (desc.length > 0) {
					var tenpercS = (tenperc?'('+tenperc+' summar'+(tenperc==1?'y is':'ies are')+' only a few words over the limit)\n':'');
					if (episodes) {
						// Copy too-long tag
						plotString = "{{Plot|episode|date="+month[d.getMonth()]+" "+d.getFullYear()+"}}";
						copyContent(plotString);
						alertText += "Episode summaries exist that are too long per WP:TVPLOT:\n"+episodes+tenpercS+'Copied to clipboard: '+plotString+'\n';
					} else {
						// All short enough
						alertText += 'All episode summaries are sufficiently short per WP:TVPLOT.\n'+tenpercS+'\n';
					}
				}
				
				// Complile all under one alert
				alert(alertText);
				console.log(alertText);
			});
		});
	},400);
});