itemDB Fetch Lib

Fetches something from itemDB, handling authentication issues arising from the March 2026 API update.

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greasyfork.org/scripts/567036/1759045/itemDB%20Fetch%20Lib.js

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

/*
•:•.•:•.•:•:•:•:•:•:•:••:•.•:•.•:•:•:•:•:•:•:•:•.•:•.•:•:•:•:•:•:•:••:•.•:•.•:•.•:•:•:•:•:•:•:•:•.•:•:•.•:•.••:•.•:•.••:
........................................................................................................................
☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦ ⠂⠄⠄⠂⠁⠁⠂⠄⠂⠄⠄⠂☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦ ⠂⠄⠄⠂⠁⠁⠂⠄⠂⠄⠄⠂☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦
    This is not a script and does nothing on its own. It was created to be @required by userscripts.
    This allows an userscript to send a query to itemDB, showing an error message if the user is unauthenticated.
    To use it, asynchronously call the function fetchItemDb(url, scriptName), using the URL you would use for a fetch
    (example: https://itemdb.com.br/api/v1/items/negg) as url, and your script's name for UI purposes in case
    authorization fails.

    Version: 1.0.0

    ✦ ⌇ saahphire
☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦ ⠂⠄⠄⠂⠁⠁⠂⠄⠂⠄⠄⠂☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦ ⠂⠄⠄⠂⠁⠁⠂⠄⠂⠄⠄⠂☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦
........................................................................................................................
•:•.•:•.•:•:•:•:•:•:•:••:•.•:•.•:•:•:•:•:•:•:•:•.•:•.•:•:•:•:•:•:•:••:•.•:•.•:•.•:•:•:•:•:•:•:•:•.•:•:•.•:•.••:•.•:•.••:
*/

let hasCreatedItemDBUnauthorizedDialog = false;

const createItemDBUnauthorizedDialog = () => {
    const dialog = document.createElement('dialog');
    document.body.appendChild(dialog);
    dialog.classList.add('itemDB-rejection-modal');
    return dialog;
}

const createItemDBUnauthorizedDialogText = (scriptName) => {
    const p = document.createElement('p');
    p.textContent = `Your itemDB authorization has expired! You need to visit it every 24 hours (logged-in) or 14 days (logged in) so the script ${scriptName} can work.`;
    return p;
}

const createItemDBUnauthorizedButton = (text, callback) => {
    const button = document.createElement('button');
    button.role = 'button';
    button.textContent = text;
    button.addEventListener('click', callback);
    return button;
}

const fillItemDBUnauthorizedDialog = (scriptName, dialog) => {
    dialog.appendChild(createItemDBUnauthorizedDialogText(scriptName));
    dialog.appendChild(createItemDBUnauthorizedButton('✖️ Close', () => {
        dialog.close();
    }));
    dialog.appendChild(createItemDBUnauthorizedButton('🔗 Go to itemDB', () => {
        dialog.close();
        window.open('https://itemdb.com.br', '_blank').focus();
    }));
}

const onUnauthorized = (scriptName, channel) => {
    if(!hasCreatedItemDBUnauthorizedDialog) {
        hasCreatedItemDBUnauthorizedDialog = true;
        document.head.insertAdjacentHTML('beforeend', itemDBCSS);
        const dialog = createItemDBUnauthorizedDialog();
        fillItemDBUnauthorizedDialog(scriptName, dialog, channel);
        document.body.appendChild(dialog);
    }
    document.getElementsByClassName('itemDB-rejection-modal')[0].showModal();
}

const fetchItemDb = (url, scriptName) => {
    return new Promise((res, rej) => {
        fetch(url, {credentials: 'include'})
            .then(response => response.json())
            .then(json => res(json))
            .catch(e => {
                if(e.name !== 'TypeError') throw(e);
                console.log(`[${scriptName}] You just saw two scary red errors in the console! Don't worry, they're normal until March.`);
                fetch(url)
                    .then(response => {
                        if(response.status === 401) {
                            onUnauthorized(scriptName);
                            document.querySelectorAll('.itemDB-rejection-modal button').forEach(button => 
                                button.addEventListener('click', (e) => {
                                    if(e.target.textContent === '✖️ Close') rej('Canceled');
                                    else setTimeout(async () => res(await fetchItemDb(url, scriptName)), 2000);
                                }), {once: true});
                        } else res(response.json());
                    }).catch(e => console.error(e));
            })
    });
}

const itemDBCSS = `<style>
.itemDB-rejection-modal {
    display: flex;
    justify-content: space-between;
    width: 30%;
    flex-wrap: wrap;

    & p {
        width: 100%;
    }
}
</style>`;