Jump to content

User:Alex 21/script-plotlength.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alex 21 (talk | contribs) at 04:39, 16 April 2016 (Double episodes). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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($) {
	var portletlink = mw.util.addPortletLink('p-tb', '#', 'Plot lengths');
	$(portletlink).click( function(e) {
		e.preventDefault();
		var episodes = ''; var dlength; var summary;
		var regex = /<([^\s]*)([^>]*)>([^<]*)<\/([^\s]*)>/;
		var plotlength = '<span class="plotlength"></span>';
		var desc = document.getElementsByClassName('description');
		var trs = document.getElementsByTagName("tr");
		var tenperc = 0;
		for (var i = 0; i < desc.length; i++) {
			summary = desc[i].innerHTML.replace(/(\r\n|\n|\r)/gm,"").replace(/<table(.*)\/table\>/g,'');
			if (summary.lastIndexOf(plotlength) > 0) summary = summary.substr(summary.lastIndexOf(plotlength)+plotlength.length);
			while (summary.match(regex)) summary = summary.replace(regex,'$3');
			dlength = summary.split(' ').length+1;
			if (dlength > 200) {
				for (var j = 0; j < trs.length; j++) {
					if (desc[i] == trs[j].children[0]) {
						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)) {
							desc[i].innerHTML = "<span style='color:green'>("+dlength+")</span> "+plotlength+desc[i].innerHTML;
							tenperc++;
						} else if ((!doubleep && dlength > 215) || (doubleep && dlength > 415)) {
							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 {
				desc[i].innerHTML = "("+dlength+") "+plotlength+desc[i].innerHTML;
			}
		}
		var tenpercS = (tenperc?'('+tenperc+' summar'+(tenperc==1?'y':'ies')+' are only a few words over the limit)':'');
		if (episodes) {
			alert(episodes+tenpercS);
			var d = new Date();
			var plotString = "{{Plot|episode|date="+d.getMonthName()+" "+d.getFullYear()+"}}";
			copyContent(plotString);
			alert('Copied to clipboard: '+plotString);
		} else {
			alert('All plots are sufficiently short.\n'+tenpercS);
		}
	});
});