UTILS_FETCH Library

fetch library

Versione datata 08/03/2025. Vedi la nuova versione l'ultima versione.

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greasyfork.org/scripts/528458/1549748/UTILS_FETCH%20Library.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         UTILS_FETCH Library
// @namespace    dannysaurus.epik
// @version      1.0
// @description  fetch library
//
// @license      MIT
// @grant        GM_xmlhttpRequest
// @grant        GM.getValue
// @grant        GM.setValue
// @grant        unsafeWindow
// ==/UserScript==

/* jslint esversion: 11 */
/* global unsafeWindow */
(() => {
    'use strict';
    unsafeWindow.dannysaurus_epik ??= {};
    unsafeWindow.dannysaurus_epik.libraries ??= {};

    unsafeWindow.dannysaurus_epik.libraries.UTILS_FETCH = (() => {

        /**
         * General fetch method using GM_xmlhttpRequest.
         * 
         * @param {string} url - The URL to fetch the data from.
         * @param {string} [method='GET'] - The HTTP method to use for the request.
         * @returns {Promise<Response>} The fetched response.
         * @throws {Error} If there is an error fetching the data.
         */
        const fetch = async (url, method = 'GET') => {
            return new Promise((resolve, reject) => GM_xmlhttpRequest({
                method: method,
                url: url,
                onload: (response) => {
                    if (response.status >= 200 && response.status < 300) {
                        resolve(response);
                    } else {
                        console.error('HTTP error! status:', response.status);
                        reject(new Error(`HTTP error! status: ${response.status}`));
                    }
                },
                onerror: (error) => {
                    console.error('Request error:', error);
                    reject(error);
                }
            }));
        };

        /**
         * Fetches JSON data from the specified URL.
         * 
         * @param {string} url - The URL to fetch the JSON data from.
         * @param {string} [method='GET'] - The HTTP method to use for the request.
         * @returns {Promise<Object>} The fetched JSON data.
         * @throws {Error} If there is an error fetching or parsing the JSON data.
         */
        const fetchJSON = async (url, method = 'GET') => {
            const response = await fetch(url, method);
            try {
                return JSON.parse(response.responseText);
            } catch (error) {
                console.error('Error parsing JSON:', error);
                throw error;
            }
        };

        /**
         * Fetches the names of accepted users from the users - JSON.
         * 
         * @param {Object} options
         * @param {string} [options.url] - The URL to fetch the JSON data from.
         * @param {string} [options.localStorageId] - The ID to store the fetched JSON data in the local storage.
         * @param {number} [options.delayMs] - The delay in milliseconds to wait before fetching the data again.
         * 
         * @returns {Promise<Object>} fetched JSON data.
         */
        const fetchJsonWithStorageCache = (() => {
            const cacheByUrl = {};

            return async ({ url, localStorageId, delayMs } = {}) => {
                localStorageId = localStorageId.replace(/\s/g, '');
                const cache = cacheByUrl[url] ??= {
                    jsonData: null,
                    timestamp: 0,
                };
                let fetchedJsonData = null;

                if (cache.jsonData === null || Date.now() - cache.timestamp >= delayMs) {
                    try {
                        fetchedJsonData = await fetchJSON(url);

                        const propertyNamesCached = Object.keys(cache.jsonData || {});
                        const propertyNamesFetched = Object.keys(fetchedJsonData || {});

                        if (propertyNamesCached.length !== propertyNamesFetched.length
                            || propertyNamesCached.some(propName => propertyNamesCached[propName] !== propertyNamesFetched[propName])
                        ) {
                            try {
                                await GM.setValue(localStorageId, fetchedJsonData);
                            } catch (storageError) {
                                console.error(`Error saving json data to local storage:`, storageError);
                            }
                        }
                    } catch (error) {
                        try {
                            fetchedJsonData = await GM.getValue(localStorageId, null);
                        } catch (storageError) {
                            console.error(`Error loading ${localStorageId} from local storage:`, storageError);
                        }
                    }


                    if (fetchedJsonData) {
                        cache.jsonData = fetchedJsonData;
                    }
                    cache.timestamp = Date.now();
                }

                return cache.jsonData || {};
            };
        })();

        return {
            fetch,
            fetchJSON,
            fetchJsonWithStorageCache,
        };
    })();
})();