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 07:14, 6 April 2016. 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]*)>/g;
		var desc = document.getElementsByClassName('description');
		var trs = document.getElementsByTagName("tr");
		for (var i = 0; i < desc.length; i++) {
			summary = desc[i].innerHTML.replace(/(\r\n|\n|\r)/gm,"").replace(/<table(.*)\/table\>/g,'');
			while (summary.match(regex)) summary = summary.replace(regex,'$3');
			dlength = summary.split(' ').length;
			if (dlength > 200) {
				for (var j = 0; j < trs.length; j++) {
					if (desc[i] == trs[j].children[0]) {
						episodes += trs[j-1].cells[0].innerHTML+" ("+dlength+")\n";
						desc[i].innerHTML = "<span style='color:red'><b>("+dlength+") ***</b></span> "+desc[i].innerHTML;
					}
				}
			} else {
				desc[i].innerHTML = "("+dlength+") "+desc[i].innerHTML;
			}
		}
		if (episodes) {
			alert(episodes);
			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.');
		}
	});
});