Jump to content

User:Alex 21/script-tvepisoderatings.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alex 21 (talk | contribs) at 03:09, 5 March 2016 (Create). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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', '#', 'Fix line colours');
	$(portletlink).click( function(e) {
		e.preventDefault();
		var loc = window.location.href; var i;
		if (loc.indexOf('action=edit') < 0) {
			var tables = document.getElementsByClassName('sortable');
			var alltables = "";
			for (i = 0; i < tables.length; i++) {
				var validtable = false;
				alltables += "{{Television episode ratings\n";
				for (var j = 1; j < tables[i].rows.length; j++) {
					var cells = tables[i].rows[j].cells;
					if (cells[6] === undefined) continue;
					validtable = true; var link;
					
					if (cells[1].children.length > 0) {
						link = cells[1].children[0];
						var elem = document.createElement('textarea');
						elem.innerHTML = link.innerHTML;
						
						title = (link.title?link.title:link.href.substr(link.href.indexOf("#")));
						link = "[["+title+(title!=link.innerHTML?"|"+elem.value:"")+"]]";
					} else {
						link = cells[1].innerHTML.substr(1, cells[1].innerHTML.length-2);
					}
					
					var date = (cells[2].children.length>1?cells[2].children[1]:cells[2]).innerHTML;
					date = date.replace(/ /g,' ').replace(/<span(.*)\/span\>/g,'');
					
					alltables += "| title"+j+"   = "+link+"\n";
					alltables += "| date"+j+"    = "+date+"\n";
					if (cells[3] !== undefined) alltables += "| rs"+j+"      = "+hlcr(cells[3])+"\n";
					if (cells[4] !== undefined) alltables += "| viewers"+j+" = "+hlcr(cells[4])+"\n";
					if (cells[5] !== undefined) alltables += "| dvr"+j+"     = "+hlcr(cells[5])+"\n";
					if (cells[6] !== undefined) alltables += "| dvrv"+j+"    = "+hlcr(cells[6])+"\n";
					if (cells[7] !== undefined) alltables += "| total"+j+"   = "+hlcr(cells[7])+"\n";
					if (cells[8] !== undefined) alltables += "| totalv"+j+"  = "+hlcr(cells[8])+"\n";
					if (j != tables[i].rows.length-1) alltables += "\n";
				}
				alltables += "}}\n\n";
				alltables = alltables.replace(/\|(.*)\= (.*)tbd(.*)\n/g,'');
			}
			copyContent(alltables);
			alert('Tables copied. You will need to paste them before copying the referenes.');
		} else {
			var allrefs = '';
			var wpTextbox1 = document.getElementById('wpTextbox1').value.split("\n");
			for (i = 0; i < wpTextbox1.length; i++) {
				var res1 = /<ref(.*)>(.*)<\/ref>/.exec(wpTextbox1[i]);
				if (res1 && allrefs.indexOf(res1[0]) < 0) allrefs += res1[0]+"\n";
				var res2 = /<ref([^>]*)\/>/.exec(wpTextbox1[i]);
				if (res2 && allrefs.indexOf(res2[0]) < 0) allrefs += res2[0]+"\n";
			}
			copyContent(allrefs);
			alert('References copied.');
		}
		function hlcr(x) { return x.innerHTML.toLowerCase().replace(/<sup(.*)\/sup\>/g,''); } 
		function copyContent(text) {
			var textArea = document.createElement("textarea");
			textArea.innerHTML = text;
			document.body.appendChild(textArea);
			textArea.select();
			document.execCommand('copy');
			document.body.removeChild(textArea);
		}
	});
});