GGn E-Books upload assistant

Scans the filename for month, year, issue, format, language and fills in info based on that

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         GGn E-Books upload assistant
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Scans the filename for month, year, issue, format, language and fills in info based on that
// @author       fordtransit
// @match        https://gazellegames.net/upload.php*
// @grant        GM.xmlHttpRequest
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';
    const MonthsToFind = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', 'christmas']; // Possible additions could be Summer, Fall, Spring, Easter, ...
    const languageMap = {
        'en': 'English',
        'fr': 'French',
        'es': 'Spanish',
        'de': 'German',
        'it': 'Italian',
        'pt': 'Portuguese',
        'cz': 'Czech',
        'zh': 'Chinese',
        'ja': 'Japanese',
        'ko': 'Korean',
        'pl': 'Polish',
        'ru': 'Russian'
    };
    const fileFormats = ['pdf', 'epub', 'mobi', 'cbz', 'cbr', 'cb7', 'azw3'];

    function performScript() {
        let FoundMonth = [];
        let FoundYear = [];
        let FullTitle = [];
        let FoundLanguages = [];
        let FoundFileFormats = [];
        let Issue = [];

        const Title = document.getElementById('title').value;
        const fileInputValue = $("#file").val().toLowerCase();

        MonthsToFind.forEach(month => {
            const capitalizedMonth = month.charAt(0).toUpperCase() + month.slice(1);
            if (fileInputValue.includes(month.toLowerCase())) {
                FoundMonth.push(capitalizedMonth);
            }
        });

        const yearPattern = /\b(19|20)\d{2}\b/g;
        let yearmatch;

        while ((yearmatch = yearPattern.exec(fileInputValue)) !== null) {
            const year = parseInt(yearmatch[0]);
            FoundYear.push(year);
        }

        const regex = /\((.*?)\)/g;
        let match;

        while ((match = regex.exec(fileInputValue)) !== null) {
            const abbreviation = match[1].toLowerCase();
            if (languageMap[abbreviation]) {
                FoundLanguages.push(languageMap[abbreviation]);
            }
        }

        fileFormats.forEach(format => {
            if (fileInputValue.includes(format)) {
                FoundFileFormats.push(format.toUpperCase());
            }
        });

        // Issue number patterns
    const numberPattern = /\b\d{3,}\b/g;     // At least 3 digits
    const hashPattern = /#(\d+)/g;           // Digits after #
    const issuePattern = /Issue\s+(\d+)/gi;  // Digits after Issue

    function formatIssueNumber(number) {
        return number.padStart(3, '0');
    }

    function isValidIssue(number) {
        const num = parseInt(number);
        return !FoundYear.includes(num);
    }

    while ((match = numberPattern.exec(fileInputValue)) !== null) {
        const number = match[0];
        if (isValidIssue(number)) {
            Issue.push(formatIssueNumber(number));
        }
    }

    while ((match = hashPattern.exec(fileInputValue)) !== null) {
        const number = match[1];
        if (isValidIssue(number)) {
            Issue.push(formatIssueNumber(number));
        }
    }

    while ((match = issuePattern.exec(fileInputValue)) !== null) {
        const number = match[1];
        if (isValidIssue(number)) {
            Issue.push(formatIssueNumber(number));
        }
        }

        Issue = [...new Set(Issue)];

        if (FoundMonth.length > 0 && FoundYear.length > 0) {
            FullTitle = Title + " (" + FoundMonth.join(", ") + " " + FoundYear.join(", ") + ")";
            document.getElementById('remaster').checked = !document.getElementById('remaster').checked;
            document.getElementById('remaster_true').classList.remove('hidden');
            $("#remaster_year").val(FoundYear.join(", "));
        } else if (FoundYear.length > 0) {
            FullTitle = Title + " (" + FoundYear.join(", ") + ")";
            document.getElementById('remaster').checked = !document.getElementById('remaster').checked;
            document.getElementById('remaster_true').classList.remove('hidden');
            $("#remaster_year").val(FoundYear.join(", "));
        } else {
            FullTitle = Title;
        }

        $("#release_title").val(FullTitle);

        if (FoundLanguages.length > 0) {
            const detectedLanguage = FoundLanguages[0];
            $("#language").val(detectedLanguage);
        } else {
            $("#language").val('English');
        }

        if (FoundFileFormats.length > 0) {
            const detectedFormat = FoundFileFormats[0];
            $("#format").val(detectedFormat);
        }

        if (Issue.length > 0) {
            const detectedIssue = Issue[0];
            $("#issue").val(detectedIssue);
        }
    }

    $("#file").on('change', function() {
        if ($("#categories").val() === 'E-Books') {
            performScript();
        }
    });

})();