User:Najzere/formatachievements.js

// Author: User:Najzere // License: CC-BY-SA 3.0 - http://creativecommons.org/licenses/by-sa/3.0/ // Description: Adds a button to the edit toolbar that attempts to format copy/paste from xbox360achievements.com into SW achievements table.

// Only add button when in edit mode if (wgAction == 'edit' || wgAction == 'submit') { addOnloadHook(addFormatAchievementsButton); };

// Add button to toolbar set to perform format when clicked function addFormatAchievementsButton { var image = document.createElement('img'); image.width = 23; image.height = 22; image.className = 'mw-toolbar-editbutton'; image.id = 'format-achievements-button'; image.src = 'http://media.strategywiki.org/images/f/f8/Button_achievements.png'; image.border = 0; image.title = 'Format achievements'; image.style.cursor = 'pointer'; image.onclick = formatAchievements; document.getElementById('toolbar').insertBefore(image, document.getElementById('toolbar').lastChild.nextSibling); };

// If there is something in the edit textbox, try to format it into an achievements table function formatAchievements { var txtarea = document.editform.wpTextbox1; if (txtarea.value != '') { var endPos; var endLine; var endLine2; var aName; var aDesc;

var startPos = txtarea.value.search(/\n	/g) + 2; while (startPos != 1) { endPos = txtarea.value.search(/[^\n]	/g) + 1 - startPos; aName = txtarea.value.substr(startPos, endPos).replace(/([\|\?#!%\.,=…:])/g,'').replace(/\//g,'-'); endLine = txtarea.value.substr(startPos + 2, txtarea.value.length).search(/\n/g) + startPos + 2; aDesc = txtarea.value.substr(endLine + 1, txtarea.value.substr(endLine + 1,txtarea.value.length).search(/\n/g)); txtarea.value = txtarea.value.substr(0, endLine).replace(/\n	/g, '\n| || ') + txtarea.value.substr(endLine, txtarea.value.length); txtarea.value = txtarea.value.substr(0, txtarea.value.search(/	/g)) + ' || ' + aDesc + ' || \n|-' + txtarea.value.substr(endLine2, txtarea.value.length); startPos = txtarea.value.search(/\n	/g) + 2; }; alert('Done.'); }; };