Usuário:Chairhandlers/script-resumodesérie.js
Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.
- Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
- Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
- Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5.
$(function($) {
setTimeout(function() {
$.when( mw.loader.using( ['mediawiki.util']), $.ready ).then(function() {
var portletlink = mw.util.addPortletLink('p-tv', '#', 'Resumo de série');
$(portletlink).click( function(e) {
e.preventDefault();
// Encontrar a tabela de resumo
var overview = document.getElementById('seriesoverview');
if (overview === null) {
var k = 0;
var alltables = document.getElementById('mw-content-text').getElementsByTagName('table');
overview = alltables[k];
var className = overview.className;
while (so_tableIndexOf(className,"ambox") || so_tableIndexOf(className,"infobox") || so_tableIndexOf(className,"mw-collapsible")) {
k++;
overview = alltables[k];
className = overview.className;
}
}
// Definir parâmetros
var df = 0; var _i = 1;
var endExist = false; var episodesCol;
var SpecialSplit = [' ',' ']; // 0: Especial; 1: Dividida
var headerBase = 3; var network_double = 0;
var network_single = 0; var infoheaderonly = 0; var auxheaders = 0;
var dateReg = /(([^\s]*)\s)?(([^\s,]*))?.*(\d{4})/;
// Começar predefinição
var _template_head = "{{Resumo de série\n";
var _template = _template_head;
// Variáveis aux - ver aonde a coluna de episódios está localizada
for (var a = 1; a < overview.rows[0].cells.length; a++) {
if (overview.rows[0].cells[a].innerHTML.indexOf('Episodes') != -1) {
episodesCol = a;
break;
}
}
auxheaders = episodesCol-1;
headerBase += auxheaders;
if (auxheaders > 0) {
for (a = 1; a < episodesCol; a++) {
_template += "| aux"+(String.fromCharCode(64+a))+" = "+overview.rows[0].cells[a].innerHTML+"\n";
}
_template += "\n";
}
// Encontrar a existência de informações e cabeçalhos/colunas de emissora
if (overview.rows[1].cells[2+auxheaders] !== undefined && overview.rows[1+auxheaders].cells[2+auxheaders].innerHTML.toLowerCase() == "network") {
network_single = 1;
}
if (overview.rows[0].cells[3+auxheaders] !== undefined) {
if (overview.rows[0].cells[3+auxheaders].innerHTML.toLowerCase() == "network") {
network_double = 1;
headerBase++;
} else if (overview.rows[0].cells[3+auxheaders].getAttribute('rowspan') > 1) {
infoheaderonly = 1;
}
}
var network = network_single || network_double;
headerBase -= infoheaderonly;
// Verificar se o resumo usa um cabeçalho simples de uma linha
var onerowheader = (overview.rows[1].cells[1].tagName == "TD" ? 1 : 0);
headerBase += onerowheader;
// Adicionar cabeçalho de informação e cabeçalhos de informações
var extraHeadRows = overview.rows[0].cells.length-headerBase;
var infoRows = overview.rows[1].cells.length-(headerBase-(network_single?0:1))+infoheaderonly;
if (extraHeadRows > 0) {
var infoheader = overview.rows[0].cells[headerBase+auxheaders].children[0];
if (infoheader === undefined || infoheader.tagName == "BR") infoheader = overview.rows[0].cells[headerBase+auxheaders].innerHTML;
if (typeof infoheader != "object") infoheader = infoheader.replace(/\n/g,'');
else infoheader.innerHTML = infoheader.innerHTML.replace(/\n/g,'');
_template += "| infoheader = "+so_link(infoheader)+"\n";
}
if (infoRows > 0 && !onerowheader && !infoheaderonly) {
for (a = 0; a < infoRows; a++) {
_template += "| info"+(String.fromCharCode(65+a))+" = "+overview.rows[1].cells[a+(headerBase-1)+auxheaders].innerHTML.replace("\n","")+"\n";
}
}
// Ligações extras
if (extraHeadRows > 0 || infoRows > 0 && !onerowheader) _template += "\n";
_template = _template.replace(_template_head+"\n",_template_head);
// Começar a iterar pelas linhas de temporada
for (var i = 2-onerowheader; i < overview.rows.length; i++) {
// Definir parâmetros
var startdate, enddate;
var RC = overview.rows[i].cells;
SpecialSplit[0] = ' '; var CellAdj = 0;
// 0: Cor; 1: Ligações, 2: Episódios
var CLE = ['','',''];
if (SpecialSplit[1] == 'A') {
SpecialSplit[1] = 'B'; // Estava na primeira parte, agora está na segunda parte
} else if (RC[0].getAttribute('rowspan') || RC[1].getAttribute('rowspan')) {
SpecialSplit[1] = 'A'; // Agora está na primeira parte
} else {
SpecialSplit[1] = ' '; // Não é uma temporada dividida
}
// Código para adicionar após o nome do parâmetro
var ThisKey = so_ThisKey(_i,SpecialSplit);
if (SpecialSplit[1] != 'B') {
// Primeira parte ou temporada comum, ligação e cor adicionados
// Ligação
var linkCell = (RC[1].firstChild.href !== undefined ? RC[1].firstChild : RC[1].firstChild.firstChild);
if (linkCell && linkCell.tagName != "A") {
if (typeof linkCell == "object") linkCell = linkCell.textContent ? linkCell.textContent : linkCell.innerText;
var aT = document.createElement('a');
aT.innerHTML = linkCell;
aT.href = '#Season '+linkCell;
linkCell = aT;
}
var linkMain;
var linkCellH = linkCell.href;
if (!linkCell) {
linkMain = '';
} else if (linkCellH.indexOf('index.php') >= 0) {
linkMain = linkCellH.substr(linkCellH.indexOf('title=')+6, linkCellH.indexOf('&')-(linkCellH.indexOf('title=')+6))+
(linkCellH.indexOf('#') >= 0 ? linkCellH.substr(linkCellH.indexOf('#')) : '');
} else {
linkMain = linkCellH.substr(linkCellH.lastIndexOf('wiki/')+5);
}
linkMain = decodeURIComponent(linkMain.replace(/_/g, ' ').replace(/\.([A-Z0-9]{2})/g, '%$1')).split('#');
// Linha especial
if (linkCell.innerHTML != _i) {
_i--;
SpecialSplit[0] = 'S';
CellAdj = Math.max(0,RC[1].getAttribute('colspan')-2);
ThisKey = so_ThisKey(_i,SpecialSplit);
}
CLE[0] += "| color"+ThisKey+" = "+(RC[0].bgColor?RC[0].bgColor:RGBSTRtoHEX(RC[0].style.background?RC[0].style.background:RC[0].style.backgroundColor))+"\n";
CLE[1] += "| link"+_i+SpecialSplit[0]+" = <includeonly>"+linkMain[0]+"</includeonly>"+(linkMain[1]?"#"+linkMain[1]:"")+"\n";
if (isNaN(parseInt(so_wiki(linkCell.innerHTML),10))) {
CLE[1] += "| linkT"+ThisKey+" = "+so_wiki(linkCell.innerHTML)+"\n";
}
} else {
// Segunda parte, apenas a cor é necessária
CLE[0] += "| color"+ThisKey+" = "+(RC[0].bgColor?RC[0].bgColor:RGBSTRtoHEX(RC[0].style.background?RC[0].style.background:RC[0].style.backgroundColor))+"\n";
}
// Variáveis aux
var aux = '';
if (episodesCol != 1 && SpecialSplit[1] != 'B') {
for (a = 1; a < episodesCol; a++) {
if (SpecialSplit[1] == ' ' || (SpecialSplit[1] != ' ' && RC[1+a].getAttribute('rowspan') > 1))
aux += "| aux"+String.fromCharCode(64+a)+_i+SpecialSplit[0]+" = "+so_wiki(RC[1+a].innerHTML)+"\n";
CellAdj--;
}
} else {
CellAdj -= episodesCol-1;
}
// Total de episódios
if (SpecialSplit[1] != 'B' && so_tba(RC[2-CellAdj].innerHTML) && !isNaN(parseInt(so_ref(RC[2-CellAdj],false),10))) {
CLE[2] += "| episodes"+_i+SpecialSplit[0]+" = "+so_ref(RC[2-CellAdj],false)+"\n";
}
// Cor, Ligação, Episódios
_template += ( SpecialSplit[1] != ' ' ? CLE[1]+aux+CLE[2]+CLE[0] : CLE[0]+CLE[1]+aux+CLE[2] );
// Variáveis aux dividem a temporada
if (episodesCol != 1 && SpecialSplit[1] != ' ') {
for (a = 1; a < episodesCol; a++) {
var cell = RC[SpecialSplit[1] == 'B' ? a-CellAdj-1 : a-CellAdj];
_template += "| aux"+String.fromCharCode(65+a)+ThisKey+" = "+so_wiki(cell.innerHTML)+"\n";
}
}
// Dividir episódios da temporada
if (SpecialSplit[1] != ' ') {
CellAdj += (SpecialSplit[1] == 'B' ? 2 : 0);
if (so_tba(RC[3-CellAdj].innerHTML) && !isNaN(parseInt(so_ref(RC[3-CellAdj],false),10))) {
_template += "| episodes"+ThisKey+" = "+so_ref(RC[3-CellAdj],false)+"\n";
}
CellAdj--;
}
// Datas de estreia e final
var temp;
var reS = dateReg.exec(so_dateref(RC[3-CellAdj]));
if (reS) {
var reS_x = [reS[4],reS[2],reS[5]];
if (reS_x[0] && isNaN(parseInt(reS_x[0],10))) {
temp = reS_x[0]; reS_x[0] = reS_x[1]; reS_x[1] = temp; df = 1;
}
startdate = so_startend(reS_x,"Start",df);
}
var reE = '';
if (RC[4-CellAdj] !== undefined && RC[3-CellAdj].getAttribute('colspan') <= 1) {
reE = dateReg.exec(so_dateref(RC[4-CellAdj]));
if (reE) {
var reE_x = [reE[4],reE[2],reE[5]];
if (reE_x[0] && isNaN(parseInt(reE_x[0],10))) {
temp = reE_x[0]; reE_x[0] = reE_x[1]; reE_x[1] = temp; df = 1;
}
enddate = so_startend(reE_x,"End",df);
endExist = true;
}
}
if (RC[3-CellAdj].getAttribute('colspan') > 1) { CellAdj++; }
if (reS) _template += "| start"+ThisKey+" = "+startdate+so_ref(RC[3-CellAdj],true)+"\n";
if (reE) _template += "| end"+ThisKey+" = "+enddate+so_ref(RC[4-CellAdj],true)+"\n";
else if (SpecialSplit[0] != 'S' && RC[3-CellAdj].getAttribute('colspan') > 1) _template += "| end"+ThisKey+" = start\n";
// Emissora
if (network && RC[5-CellAdj] !== undefined && RC[5-CellAdj].getAttribute('rowspan')) {
_template += "| network"+ThisKey+" = "+so_link(RC[5-CellAdj].children[0])+"\n";
CellAdj++;
}
// Células de informações
if (infoRows > 0) {
for (var j = 0; j < 26; j++) {
var infoCell = RC[5+j-CellAdj];
if (infoCell && so_tba(so_ref(infoCell,false))) {
_template += "| info"+String.fromCharCode(65+j)+ThisKey+" = "+so_ref(infoCell,false)+"\n";
}
}
}
// Novas linhas e atualização de variáveis de temporada dividida
if (i < overview.rows.length-1 && SpecialSplit[1] != 'A') _template += "\n";
if (SpecialSplit[1] == 'B') SpecialSplit[1] = ' ';
else if (SpecialSplit[1] == 'A') _i--;
_i++;
}
// Parâmetros de lançamento
var released = '';
for (a = 1; a < overview.rows[0].cells.length; a++) {
if (overview.rows[0].cells[a].innerHTML.indexOf('release') != -1) {
released += "| released = y\n";
break;
}
}
if (!endExist) {
released += "| allreleased = y\n";
}
if (released) _template = _template.replace(_template_head, _template_head+released+"\n");
// Terminado
_template = _template.replace('<small><span style="color: #2C2C2C" title="A ser anunciado">TBA</span></small>','{{TableTBA}}');
_template += "}}";
copyContent(_template);
alert("Copiado! Agora vá para a página de edição e cole o código da linha.");
// Função: Remover referência
function so_ref(cell,comment) {
if (comment) {
if (cell.innerHTML.indexOf("<sup") < 0) return '';
else return "<!--Certifique-se de restaurar a referência que existia aqui!-->";
} else {
if (cell.innerHTML.indexOf("<sup") < 0) return cell.innerHTML;
else return cell.innerHTML.replace(/<sup(.*)\/sup\>/g,"<!--Certifique-se de restaurar a referência que existia aqui!-->");
}
}
// Função: Ligação da linha para ligação da wiki
function so_link(link) {
if (link === undefined || link.href === undefined) return link;
var nwlink = link.href.replace("https://pt.wikipedia.org/wiki/","").replace("_", " ");
return "[["+(link.innerHTML==nwlink?"":nwlink+"|")+link.innerHTML+"]]";
}
// Função: Remover referência de dados
function so_dateref(cell) {
return (cell.innerHTML.indexOf('<')>=0 ? cell.innerHTML.substr(0, cell.innerHTML.indexOf('<')) : cell.innerHTML).split(' ').join(' ');
}
// Função: Texto negrito e itálico para wiki
function so_wiki(text) {
return text.replace(/<i>(.*)<\/i>/g, "''$1''").replace(/<b>(.*)<\/b>/g, "'''$1'''");
}
// Função: Datas para predefinições da wiki
function so_startend(cell,se,df) {
var monthNumber = new Date(Date.parse(cell[1]+" 1, 2005")).getMonth()+1;
return "{{"+se+" date|"+cell[2]+
(!isNaN(monthNumber) && cell[1] !== undefined ? "|"+monthNumber : "")+
(!isNaN(cell[0]) && cell[0] !== undefined ? "|"+cell[0] : "")+
(df ? "|df=y" : "")+"}}";
}
// Função: Verifique se classname contém classe única
function so_tableIndexOf(className,content) {
return (className.indexOf(content) >= 0);
}
// Função: Verifique se o texto contém TBA, TBD, N/A
function so_tba(text) {
text = text.toLowerCase();
return text.indexOf("tba") < 0 && text.indexOf("tbd") < 0 && text.indexOf("n/a") < 0;
}
// Função: Obter valor de ThisKey
function so_ThisKey(_i,SpecialSplit) {
var ThisKey = _i+SpecialSplit[1]+SpecialSplit[0];
if (ThisKey.indexOf(' ') != -1) {
var spaces = ThisKey.length-ThisKey.replace(/\s/g,'').length;
ThisKey = ThisKey.replace(/\s/g,'');
for (a = 0; a < spaces; a++) ThisKey += ' ';
}
return ThisKey;
}
});
});
},600);
});