User:Alex 21/script-plotlength.js

$(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();
				
				function format_length_colour(size, episode) {
					// return [format, number (0 fine, 1 acceptable, 2 oversized), episode tracker]
					if (size < 200) { // Within limit 
						return ["("+size+") "+plotlength, 0, ""];
					} else if (size <= 220) { // Allow up to 20 more than maximum for leeway
						return ["<span style='color:green'>("+size+")</span> "+plotlength, 1, ""];
					} else { // Too long, tag
						return ["<span style='color:red'><b>("+size+")</b></span> "+plotlength, 2, episode.replace(/\n/g,'')+" ("+size+")\n"];
					}
				}
				
				// Months
				var month = "January February March April May June July August September October November December".split(" ");
				
				// 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 {
					infoboxtype = ( (infobox.className.indexOf("ib-tv-episode") >= 0) ? "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, new lines; split by horizontal breaks
					var basesummary = desc[i].innerHTML;
					summary = basesummary.replace(/(\r\n|\n|\r)/gm,"").replace(/<(table|tbody|style)(.*)\/(table|tbody|style)\>/g,'');
					if (summary.lastIndexOf(plotlength) > 0) summary = summary.substr(summary.lastIndexOf(plotlength)+plotlength.length);
					summary = summary.replace(/<hr\s*\/?>/g, '{{hr}}');
					basesummary = basesummary.replace(/<hr\s*\/?>/g, '{{hr}}');
					summary = summary.replace(/<\/?([^>]*)>/g, '');
					
					summary_split_hr = summary.split('{{hr}}');
					basesummary_split_hr = basesummary.split('{{hr}}');
					summary_split_hr_lengths = [];
					var episodeNumberElement = null;
					
					for (h = 0; h < summary_split_hr.length; h++) {
						summary = summary_split_hr[h];
						dlength = summary.split(' ').length+1;
						
						if (summary.match(/^(Note)\s*:?/) == null) { // Ignore sections that are notes
							summary_split_hr_lengths[summary_split_hr_lengths.length] = dlength;
							
							episodeNumberElement = desc[i].parentNode; // description > expand-child class element 
							while (episodeNumberElement?.className.indexOf('module-episode-list-row') == -1 || episodeNumberElement?.children.length == 0) {
								episodeNumberElement = episodeNumberElement.previousElementSibling;
							}
							episodeNumberElement = episodeNumberElement?.children[0].innerHTML;
							console.log(episodeNumberElement);
							[count_format, size_format, episode_format] = format_length_colour(dlength, episodeNumberElement);
							
							if (summary_split_hr.length > 1) {
								basesummary_split_hr[h] = count_format+basesummary_split_hr[h];
							} else {
								basesummary_split_hr[h] = count_format+"<br />"+basesummary_split_hr[h];
								if (size_format == 1) {
									tenperc++;
								} else if (size_format == 2) {
									episodes += episode_format;
								}
							}
						}
					}
					
					if (summary_split_hr_lengths.length > 1) { // Total length of all parts if there is more than one hr 
						var total_length = summary_split_hr_lengths.reduce((accumulator, currentvalue) => accumulator + currentvalue, 0);
						
						[count_format, size_format, episode_format] = format_length_colour(total_length, episodeNumberElement);
						basesummary_split_hr[0] = count_format+"<br />"+basesummary_split_hr[0];
						if (size_format == 1) {
							tenperc++;
						} else if (size_format == 2) {
							episodes += episode_format;
						}
					}
					desc[i].innerHTML = basesummary_split_hr.join('<hr />');
				}
				
				// 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);
});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.