itemDB Fetch Lib

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

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/567036/1759045/itemDB%20Fetch%20Lib.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

/*
•:•.•:•.•:•:•:•:•:•:•:••:•.•:•.•:•:•:•:•:•:•:•:•.•:•.•:•:•:•:•:•:•:••:•.•:•.•:•.•:•:•:•:•:•:•:•:•.•:•:•.•:•.••:•.•:•.••:
........................................................................................................................
☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦ ⠂⠄⠄⠂⠁⠁⠂⠄⠂⠄⠄⠂☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦ ⠂⠄⠄⠂⠁⠁⠂⠄⠂⠄⠄⠂☆ ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂✦
    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>`;