>>15896
A ver lo editaste? aquí dejo una versión actualizada del mio
Otro test 🎲 [ 6d15 ] ➔ (6 + 1 + 6 + 4 + 6 + 8) =
31 💥
document.addEventListener('click', function(e) {
// Detecta si el elemento clickeado es un botón de responder (ya sea de Dollchan o del foro)
const target = e.target;
const isPostButton = target.id === 'formButton' ||
target.value === 'Reply' ||
target.value === 'Post' ||
target.classList.contains('qr-submit') ||
target.textContent.toLowerCase().includes('post') ||
target.textContent.toLowerCase().includes('reply');
if (isPostButton) {
// Localiza el cuadro de texto activo en la página
const textareas = document.querySelectorAll('textarea');
// Regex para buscar patrones como 🎲 [ 2d6 ] ➔ (1 + 6) =
7 💥, 🎲 [ 1d20 ] ➔ 2 🎯, 🎲 [ 3d14 ] ➔ (12 + 1 + 11) =
24 💥, etc. (insensible a mayúsculas/minúsculas)
const regex = /!(\d+)d(\d+)/gi;
textareas.forEach(textarea => {
if (regex.test(textarea.value)) {
// Reiniciamos el índice de la regex por seguridad
regex.lastIndex = 0;
// Reemplazamos todas las ocurrencias que aparezcan en el texto
textarea.value = textarea.value.replace(regex, (match, cantidadStr, carasStr) => {
const cantidad = parseInt(cantidadStr, 10);
const caras = parseInt(carasStr, 10);
// Validación básica para evitar valores absurdos o vacíos
if (cantidad <= 0 || caras <= 0) return match;
let tiradas = [];
let total = 0;
for (let i = 0; i < cantidad; i++) {
const tirada = Math.floor(Math.random() * caras) + 1;
tiradas.push(tirada);
total += tirada;
}
// Formato más lindo y colorido usando emojis nativos soportados por cualquier foro
if (cantidad === 1) {
return `🎲 [ ${cantidad}d${caras} ] ➔ ${tiradas[0]} 🎯`;
} else {
// Se cambió ** por == para el resaltado
return `🎲 [ ${cantidad}d${caras} ] ➔ (${tiradas.join(' + ')}) =
${total} 💥`;
}
});
// Fuerza a Dollchan y al DOM a leer el nuevo texto modificado
textarea.dispatchEvent(new Event('input', { bubbles: true }));
textarea.dispatchEvent(new Event('change', { bubbles: true }));
}
});
}
}, true);