GGn Title Formatter

Formats title and sets alias if applicable

Verzia zo dňa 13.11.2023. Pozri najnovšiu verziu.

// ==UserScript==
// @name         GGn Title Formatter
// @namespace    none
// @version      1
// @description  Formats title and sets alias if applicable
// @author       ingts
// @match        https://gazellegames.net/upload.php
// ==/UserScript==
function formatTitle(str) {
    const smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|v.?|vs.?|via)$/i;
    const alphanumericPattern = /([A-Za-z0-9\u00C0-\u00FF])/;
    const wordSeparators = /([ :–—-])/;
    const p = /[^a-zA-Z0-9 .?!]/g;
    const match = str.match(p)
    const aliases = document.getElementById('aliases');
    if (match)
        aliases.value = match.join('')
    str = str
        .replace(/\s/g, ' ')
        .replace(/ ~ /, ': ').replace(/ ~/, ': ').replace(/~$/, '').replace(/ ~$/, '').replace(/ - /, ': ').replace(/ -/, ': ').replace(/-$/, '')
        .replace(p, "")
        .trim().toLowerCase()
    
    return str.split(wordSeparators)
        .map(function (current, index, array) {
            if (
                /* Check for small words */
                current.search(smallWords) > -1 &&
                /* Skip first and last word */
                index !== 0 &&
                index !== array.length - 1 &&
                /* Ignore title end and subtitle start */
                array[index - 3] !== ':' &&
                array[index + 1] !== ':' &&
                /* Ignore small words that start a hyphenated phrase */
                (array[index + 1] !== '-' ||
                    (array[index - 1] === '-' && array[index + 1] === '-'))
            ) {
                return current.toLowerCase()
            }
            /* Capitalize the first letter */
            return current.replace(alphanumericPattern, function (match) {
                return match.toUpperCase()
            })
        })
        .join('')
}

const titleInput = document.getElementById('title')
const t = setInterval(() => {
    if (document.activeElement === titleInput || !titleInput.value)
        return
    titleInput.value = formatTitle(titleInput.value)
    clearInterval(t)
}, 1000)