User:ZBroz/Tbot.js

// var Tbot; Tbot = Tbot || {};

/*

Change redlinks to greenlinks
*/

Tbot.greenifyTranslinks = function (langcode) { if(mw.config.get('wgAction') !== 'view') return; if(! document.querySelector) return; if(! document.querySelector        ('table.translations span:lang(' + langcode + ') > a.new')) return; var headers = document.querySelectorAll('h3, h4, h5'); for(var i = 0; i < headers.length; ++i) {   if($(headers[i].querySelector('.mw-headline')).text !== 'Translations') continue; var posHeader = getPosForTransSect(i); if(! posHeader) continue; for( var elem = headers[i].nextElementSibling;        elem && elem.tagName.search(/^[hH]\d$/) === -1;         elem = elem.nextElementSibling ) {     var linksToGreenify = elem.querySelectorAll('span:lang(' + langcode + ') > a.new'); if(linksToGreenify.length === 0) continue; var gloss = getGlossForTransTable(elem); for(var j = 0; j < linksToGreenify.length; ++j) {       var link = linksToGreenify[j];

var tbotData = {         lang:  langcode, pos:  posHeader.toLowerCase, head: $(link).text.trim.replace(/ +/g, ' '), xlit: getXlitForLink(link), g:    getGenderForLink(link), trans: mw.config.get('wgTitle'), gloss: gloss };       if(mw.util.wikiUrlencode(tbotData.head)             === link.getAttribute('href').match(/\?(?:.*?&)?title=([^&]+)/)[1]) delete tbotData.head; removeEmptyProps(tbotData);

var href = link.getAttribute('href'); href = href.split('#')[0]; // shouldn't be needed if(href.indexOf('?') === -1) // shouldn't happen href += '?'; href += '&tbotData=' + encodeURIComponent(JSON.stringify(tbotData));

link.setAttribute('href', href); link.style.color = 'rgb(34, 204, 0)'; }   }  }

function getPosForTransSect(i) // headers[i] is the trans-sect-header {   // We start at headers[i-1], and scroll back until we find a header at    // a higher level than headers[i]. For example, if ====Translations==== // is at L4, then the closest-previous L3 header is the POS header.

for(var j = i - 1; j >= 0; --j) if(headers[j].tagName < headers[i].tagName) {       var ret = $(headers[j].querySelector('.mw-headline')).text; ret = ret.replace(/\s+\d+$/, ''); // e.g. 'Noun 1' -> 'Noun' return ret; }   return null; // shouldn't happen }

function getGlossForTransTable(navFrame) {   var ret = $(navFrame.querySelector('.NavHead')).text; ret = ret.trim.replace(/^\[.*?\]\s*/, ).replace(/^\u00B1\s*/, ); return ret; }

function getXlitForLink(link) {   for(var node = link.parentNode.nextSibling; node; node = node.nextSibling) {     if(node.nodeType === Node.ELEMENT_NODE          && node.tagName.toUpperCase === 'A') // hit undefined or break; if(node.nodeType === Node.ELEMENT_NODE         && node.lang === langcode) // hit  or         break; if(node.nodeType !== Node.TEXT_NODE) continue; var match = node.data.match(/^ \((.*)\),? ?$/); if(match) return match[1]; }   return ''; }

function getGenderForLink(link) // e.g. '' or 'm' or 'c1' or 'm|f' {   var ret = ''; for( var elem = link.parentNode.nextElementSibling;        elem;         elem = elem.nextElementSibling ) if(elem.tagName.toUpperCase === 'A') // hit undefined or return ''; else if(elem.lang === langcode) // hit or         return ''; else if(elem.className === 'gender') return decodeGenderSpan(elem); return ''; }

function decodeGenderSpan(span) {   var child = span.firstChild; if(child.nodeValue && child.nodeValue.trim === 'class') { child = child.nextSibling; var ret = ''; while(child) { if(child.nodeType === Node.ELEMENT_NODE) { ret += '|c' + child.innerHTML.trim; }       child = child.nextSibling; }     return ret.replace(/^[|]/, ''); } else { var abbrToCode = { pl: 'p', anim: 'an', du: 'd', inan: 'in', sg: 's', pers: 'pr' }; var ret = ''; while(child) { if(child.nodeType === Node.ELEMENT_NODE) { var abbr = child.innerHTML.trim; ret += '-' + (abbrToCode[abbr] || abbr); } else if(child.nodeType === Node.TEXT_NODE) { if(child.nodeValue.trim === ',') ret += '|'; }       child = child.nextSibling; }     return ret.replace(/(^|[|])-/g, '$1'); } }

function removeEmptyProps(obj) {   var propsToRemove = []; for(var key in obj) if(obj[key] === undefined || obj[key] === null || obj[key] === '') propsToRemove.push(key); for(var i = 0; i < propsToRemove.length; ++i) delete obj[propsToRemove[i]]; } };

/*

Populate edit-window at target of a greenlink
*/

Tbot.getHeadLine = Tbot.getHeadLine || {};

if(! Tbot.getHeadLine.run) Tbot.getHeadLine.run = function(tbotData) {   var f;

// Has a special case has been set up for this lang & POS? f = f || Tbot.getHeadLine[tbotData.lang+'~'+tbotData.pos];

// . . . how about a special case for this lang, regardless of POS? f = f || Tbot.getHeadLine[tbotData.lang+'~'];

// . . . or for this POS, regardless of lang? f = f || Tbot.getHeadLine['~'+tbotData.pos];

// No? O.K., then just use the default: f = f || Tbot.getHeadLine.default;

return f(tbotData); };

// a suitable default, using the template if(! Tbot.getHeadLine.default) Tbot.getHeadLine.default = function (tbotData) {   var ret = ''; return ret; };

// An augmented default: use initially, but also launch an // AJAX request to see if there exists a more-specific template and, // if so, to replace with a stab in the dark at how to use // that template. Tbot.getHeadLine['attempt-'] = function (tbotData) { var lang = tbotData.lang; var pos = tbotData.pos;

jQuery.getJSON (   '/w/api.php?format=jsonfm&action=query&titles='      + 'Template:' + lang + '-' + pos,    function (data)    {      data = data && data.query && data.query.pages;      if(! data)       return;      var regex = new RegExp('[{][{]head[|]' + lang + '[|]' + pos + '(?=[|}]'); var wpTextbox1 = document.getElementById('wpTextbox1'); if(! wpTextbox1 || ! regex.test(wpTextbox1.value)) return; for(var pageid in data) if(pageid.charAt(0) !== '-') {         wpTextbox1.value = wpTextbox1.value.replace (regex, '{{' + data[pageid].title.substr(9)); return; }   }  );

return Tbot.getHeadLine[''](tbotData); };

Tbot.getHeadLine['ru~adjective'] = Tbot.getHeadLine['ru~adverb'] = Tbot.getHeadLine['ru~noun'] = Tbot.getHeadLine['ru~proper noun'] = Tbot.getHeadLine['ru~verb'] = function (tbotData) { var ret = ''; return ret; };

Tbot.getHeadLine['cs~adjective'] = Tbot.getHeadLine['cs~adverb'] = Tbot.getHeadLine['cs~noun'] = Tbot.getHeadLine['cs~proper noun'] = Tbot.getHeadLine['cs~verb'] = function (tbotData) { var ret = ''; return ret; }; Tbot.getHeadLine['tpi~verb'] = function (tbotData) { if(mw.config.get('wgPageName').search(/im$/) > -1) return ''; else return ''; };

Tbot.getHeadLine['tpi~verb'] = function (tbotData) { if(mw.config.get('wgPageName').search(/im$/) > -1) return ''; else return ''; };

Tbot.getHeadLine['yi~adjective'] = Tbot.getHeadLine['yi~adverb'] = Tbot.getHeadLine['yi~noun'] = Tbot.getHeadLine['yi~phrase'] = Tbot.getHeadLine['yi~proper noun'] = Tbot.getHeadLine['yi~verb'] = function (tbotData) { var ret = ''; return ret; };

Tbot.getInflSect = function (tbotData) { // allow special-casing of specific languages and POSes: if(Tbot.getInflSect[tbotData.lang + '~' + tbotData.pos]) return Tbot.getInflSect[tbotData.lang+'~'+tbotData.pos](tbotData);

return ''; };

Tbot.getInflSect['yi~adjective'] = function (tbotData) { var template = ''; return '\n====Declension====\n' + template + '\n'; };

// DP custom BEGIN Tbot.getLanguageHeading = function (langCode) { // Hardcode some languages for a nicer wiki markup before substitution if (langCode=="cs") return '==Czech==\n\n'; else return '==lookup_language==\n\n'; };

Tbot.getExternalLinks = function (langCode) { if (langCode=="cs") return "\n===External links===\n* \n* "; else return ""; }; // DP custom END

$(function {  if(mw.config.get('wgNamespaceNumber') !== 0)    return;  if(mw.config.get('wgAction') !== 'edit')    return;  if(! document.getElementById('ca-nstab-main'))   return;  if(document.getElementById('ca-nstab-main').className !== 'selected new')    return;  var tbotData = document.location.href.match(/&tbotData=([^&]+)/);  if(! tbotData)   return;  tbotData = JSON.parse(decodeURIComponent(tbotData[1]));  if(tbotData.pos)    tbotData.posHeader =      tbotData.pos.charAt(0).toUpperCase + tbotData.pos.substr(1);  var wikitext = '';  if(! tbotData.lang || tbotData.lang.search(/^[a-z-]+$/) === -1)   return;  //wikitext += '==lookup_language==\n\n';  wikitext += Tbot.getLanguageHeading(tbotData.lang);  if(! tbotData.posHeader)   return;  if(tbotData.posHeader.search(/^=|=$/) > -1)    wikitext += '=== ' + tbotData.posHeader + ' ===\n';  else    wikitext += '===' + tbotData.posHeader + '===\n';  wikitext += Tbot.getHeadLine.run(tbotData) + '\n\n';  if(! tbotData.trans)   return;  wikitext += '# ' + tbotData.trans + ;  if(tbotData.gloss)    wikitext += ' ' + tbotData.gloss + ;  wikitext += '\n';  wikitext += Tbot.getInflSect(tbotData);  wikitext += Tbot.getExternalLinks(tbotData.lang);  document.getElementById('wpTextbox1').value = wikitext; }); //