User:Alex 21/script-plotlength.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Alex 21/script-plotlength. |
$(function($) {
var portletlink = mw.util.addPortletLink('p-tb', '#', 'Plot lengths');
$(portletlink).click( function(e) {
e.preventDefault();
var plotlength = '<span class="plotlength"></span>';
var dlength; var summary = ''; var i; var d = new Date(); var plotString;
var firstSummary = document.getElementsByClassName('summary')[0];
if (firstSummary.className == "summary" && firstSummary.tagName == "TH") {
// Film Plot
var inPlot = false; var firstElem;
var IDs = ["Plot", "Premise", "Synopsis", "Summary", "Plot summary"];
for (var id = 0; id < IDs.length; id++) {
var summaryElem = document.getElementById(IDs[id]);
if (summaryElem === null) continue;
var nodes = summaryElem.parentNode.parentNode.children;
for (i = 0; i < nodes.length; i++) {
if (nodes[i] == summaryElem.parentNode) {
inPlot = true;
} else if (inPlot === true) {
if (nodes[i].tagName == 'H2') {
inPlot = false;
} else {
summary += nodes[i].innerHTML+' ';
if (firstElem === undefined & nodes[i].tagName == "P") firstElem = nodes[i];
}
}
}
if (summary.lastIndexOf(plotlength) > 0) summary = summary.substr(summary.lastIndexOf(plotlength)+plotlength.length);
summary = summary.replace(/(\r\n|\n|\r)/gm,"").replace(/<table(.*)\/table\>/g,'');
summary = summary.replace(/<\/?([^>]*)>/g,'').trim();
dlength = summary.split(' ').length+1;
if (dlength > 700) {
firstElem.innerHTML = "<span style='color:red'><b>("+dlength+")</b></span> "+plotlength+firstElem.innerHTML;
plotString = "{{Plot|film|date="+d.getMonthName()+" "+d.getFullYear()+"}}";
copyContent(plotString);
alert('The plot summary is too long per WP:FILMPLOT. Copied to clipboard: '+plotString);
} else {
firstElem.innerHTML = "("+dlength+") "+plotlength+firstElem.innerHTML;
alert('The plot summary is sufficiently short per WP:FILMPLOT.');
}
break;
}
} else {
// Episodes Plot
var episodes = '';
var desc = document.getElementsByClassName('description');
var trs = document.getElementsByTagName("tr");
var tenperc = 0;
for (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);
summary = summary.replace(/<\/?([^>]*)>/g,'');
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 is':'ies are')+' only a few words over the limit)':'');
if (episodes) {
plotString = "{{Plot|episode|date="+d.getMonthName()+" "+d.getFullYear()+"}}";
copyContent(plotString);
alert("Episode summaries exist that are too long per WP:TVPLOT:\n"+episodes+tenpercS+'Copied to clipboard: '+plotString);
} else {
alert('All plots are sufficiently short per WP:TVPLOT.\n'+tenpercS);
}
}
});
});