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 = 'https://cdn.wikimg.net/en/strategywiki/images/f/f8/Button_achievements.png'; image.border = 0; image.title = 'Format achievements'; image.style.cursor = 'pointer'; image.onclick = formatAchievements; if (document.getElementById('toolbar').childNodes.length == 0) { document.getElementById('toolbar').appendChild(image); } else { 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; var regex = /	(.*?)	(\d+)\n(.*?)	.*?$/gm; var foundMatch = null; var cleanImg; while (foundMatch = regex.exec(txtarea.value)) { cleanImg = foundMatch[1].replace(/(?:[\|\?#!%,=…:])/g,).replace(/\//g,'-').replace(/"/g,"'").replace(/(?:^ +| +$)/,);   txtarea.value = txtarea.value.replace(foundMatch[0],'|  || ' + foundMatch[1] + ' || ' + foundMatch[3] + ' || \n|-');  }; };