User:LOL/mmarecmethod.js
Appearance
< User:LOL
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. |
Documentation for this user script can be added at User:LOL/mmarecmethod. |
var mma = mma || {};
mma.getFights = function(src) {
var fights = [];
var srcLeft = src.indexOf('<div id="fighter_stat">');
if (srcLeft === -1) {
window.alert('Invalid page');
return;
}
var srcRight = src.indexOf('</table>', srcLeft);
if (srcRight === -1) {
window.alert('Invalid page');
return;
}
var i = srcLeft;
while ((i = src.indexOf('<tr align="center"', i + 1)) > -1) {
var row = [];
var k = i;
for (var j = 0; j < 7; j++) {
k = src.indexOf('<td', k);
var l = src.indexOf('>', k);
k = src.indexOf('</td>', l);
row[j] = mma.removeTags(src.substring(l + 1, k))
.replace(/(^\s+|\s+$)/g, '');
}
fights.push({opponent: row[1], date: row[4], data: row});
}
return fights;
};
mma.recmethod = function() {
var columns = [
{name: 'Method', sherdogIndex: 2, templateIndex: 3},
{name: 'Round', sherdogIndex: 5, templateIndex: 6},
{name: 'Time', sherdogIndex: 6, templateIndex: 7}
];
for (var i in columns) {
columns[i].index = -1;
columns[i].regex = new RegExp('.+?\\b' + columns[i].name
+ '\\b');
}
var tb = document.getElementById('wpTextbox1');
var tableStart = tb.value.indexOf('\n{|');
var tableEnd = tb.value.indexOf('\n|}', tableStart);
if (tableStart === -1 || tableEnd === -1) {
tableStart = tb.value.indexOf('-start}}');
tableEnd = tb.value.indexOf('-end}}');
if (tableStart === -1 || tableEnd === -1) {
tableStart = tb.value.indexOf('{{MMA record start}}');
tableEnd = tb.value.indexOf('{{end}}', tableStart);
if (tableStart === -1 || tableEnd === -1) {
window.alert('Record table not found.');
} else {
for (var i in columns) {
columns[i].index = columns[i].templateIndex;
}
}
}
}
var i = tableStart;
if (columns[0].index === -1) {
// Find the lines that look like a column heading
var count = 0; // Number of cells seen in this row so far
while (i > -1) {
var i1 = tb.value.indexOf('\n|', i + 1);
var i2 = tb.value.indexOf('\n!', i + 1);
if (i1 === -1) {
i1 = tableEnd;
}
if (i2 === -1) {
i2 = tableEnd;
if (i1 === i2) {
// No more cells found in source
break;
}
}
i = Math.min(i1, i2);
if (tb.value[i + 2] === '-') {
count = 0;
} else {
for (var j = 0; j < columns.length; j++) {
if (columns[j].regex.test(tb.value.substring(i, tb.value.indexOf('\n', i + 1)))) {
columns[j].index = count;
break;
}
}
count++;
}
}
}
for (var i in columns) {
if (columns[i].index === -1) {
window.alert('"' + columns[i].name + '" column not found.');
return;
}
}
window.alert('Debug: Found Method/Round/Time at index ' + columns[0].index
+ '/' + columns[1].index + '/' + columns[2].index);
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
// Get fight data from external site
var src = xmlhttp.responseText;
var fights = mma.getFights(src);
var fightIndex = 0;
var count = 0;
var i = tableStart;
while ((i = tb.value.indexOf('\n|', i + 1)) > -1 && i < tableEnd) {
if (tb.value[i + 2] === '-') {
count = 0;
} else {
for (var j = 0; j < columns.length; j++) {
if (count === columns[j].index) {
var contentStart = i + 2; // Immediately after pipe
var endLine = tb.value.indexOf('\n', i + 1);
var content = tb.value.substring(contentStart, endLine);
if (/^[^\|\[\{\n]+\|/.test(content)) {
// Evil hack for "align=center|"
contentStart = tb.value.indexOf('|',
contentStart) + 1;
content = tb.value.substring(contentStart, endLine);
}
content = content.replace(/(^\s+|\s+$)/g, '');
if (fightIndex > 0 || content.length > 0
&& !/\{\{ *[Nn]\/A *\}\}/.test(content)) {
// Ignore empty cell -- probably future fight=
if (mma.removeWikilinks(content).toLowerCase()
!== fights[fightIndex].data[columns[j]
.sherdogIndex].toLowerCase()) {
if (window.confirm('For the '
+ columns[j].name
+ ' of the bout vs. '
+ fights[fightIndex].opponent
+ ' on '
+ fights[fightIndex].date
+ ':\nChange "' + content + '" to "'
+ fights[fightIndex].data[columns[j].sherdogIndex]
+ '"?')) {
tb.value = tb.value.substring(0, contentStart)
+ ' ' + fights[fightIndex].data[columns[j].sherdogIndex]
+ tb.value.substring(endLine);
}
}
if (j + 1 === columns.length) {
fightIndex++;
}
}
break;
}
}
count++;
}
}
window.alert('MMA record method script finished.');
}
};
var url = window.prompt('Enter the URL of the fighter\'s Sherdog profile',
'http://www.sherdog.com/fighter/');
xmlhttp.open('GET', url, true);
xmlhttp.send();
};
mma.removeTags = function(s) {
return s.replace(/<[^>]+>/g, '');
};
mma.removeWikilinks = function(s) {
return s.replace(/\[\[([^\[\]\|]+)\]\]/g, '$1')
.replace(/\[\[[^\[\]\|]+\|([^\[\]\|]+)\]\]/g, '$1');
};
addOnloadHook(function() {
if (document.forms.editform) {
mw.util.addPortletLink('p-tb', 'javascript:mma.recmethod()', 'MMA record method', 't-mmarecmethod', 'Synchronize method column with Sherdog in record tables', '', '');
}
});