UTILS_FETCH Library

fetch library

Mint 2025.03.08.. Lásd a legutóbbi verzió

Ezt a szkriptet nem ajánlott közvetlenül telepíteni. Ez egy könyvtár más szkriptek számára, amik tartalmazzák a // @require https://update.greatest.deepsurf.us/scripts/528458/1549748/UTILS_FETCH%20Library.js hivatkozást.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

  1. // ==UserScript==
  2. // @name UTILS_FETCH Library
  3. // @namespace dannysaurus.epik
  4. // @version 1.0
  5. // @description fetch library
  6. //
  7. // @license MIT
  8. // @grant GM_xmlhttpRequest
  9. // @grant GM.getValue
  10. // @grant GM.setValue
  11. // @grant unsafeWindow
  12. // ==/UserScript==
  13.  
  14. /* jslint esversion: 11 */
  15. /* global unsafeWindow */
  16. (() => {
  17. 'use strict';
  18. unsafeWindow.dannysaurus_epik ??= {};
  19. unsafeWindow.dannysaurus_epik.libraries ??= {};
  20.  
  21. unsafeWindow.dannysaurus_epik.libraries.UTILS_FETCH = (() => {
  22.  
  23. /**
  24. * General fetch method using GM_xmlhttpRequest.
  25. *
  26. * @param {string} url - The URL to fetch the data from.
  27. * @param {string} [method='GET'] - The HTTP method to use for the request.
  28. * @returns {Promise<Response>} The fetched response.
  29. * @throws {Error} If there is an error fetching the data.
  30. */
  31. const fetch = async (url, method = 'GET') => {
  32. return new Promise((resolve, reject) => GM_xmlhttpRequest({
  33. method: method,
  34. url: url,
  35. onload: (response) => {
  36. if (response.status >= 200 && response.status < 300) {
  37. resolve(response);
  38. } else {
  39. console.error('HTTP error! status:', response.status);
  40. reject(new Error(`HTTP error! status: ${response.status}`));
  41. }
  42. },
  43. onerror: (error) => {
  44. console.error('Request error:', error);
  45. reject(error);
  46. }
  47. }));
  48. };
  49.  
  50. /**
  51. * Fetches JSON data from the specified URL.
  52. *
  53. * @param {string} url - The URL to fetch the JSON data from.
  54. * @param {string} [method='GET'] - The HTTP method to use for the request.
  55. * @returns {Promise<Object>} The fetched JSON data.
  56. * @throws {Error} If there is an error fetching or parsing the JSON data.
  57. */
  58. const fetchJSON = async (url, method = 'GET') => {
  59. const response = await fetch(url, method);
  60. try {
  61. return JSON.parse(response.responseText);
  62. } catch (error) {
  63. console.error('Error parsing JSON:', error);
  64. throw error;
  65. }
  66. };
  67.  
  68. /**
  69. * Fetches the names of accepted users from the users - JSON.
  70. *
  71. * @param {Object} options
  72. * @param {string} [options.url] - The URL to fetch the JSON data from.
  73. * @param {string} [options.localStorageId] - The ID to store the fetched JSON data in the local storage.
  74. * @param {number} [options.delayMs] - The delay in milliseconds to wait before fetching the data again.
  75. *
  76. * @returns {Promise<Object>} fetched JSON data.
  77. */
  78. const fetchJsonWithStorageCache = (() => {
  79. const cacheByUrl = {};
  80.  
  81. return async ({ url, localStorageId, delayMs } = {}) => {
  82. localStorageId = localStorageId.replace(/\s/g, '');
  83. const cache = cacheByUrl[url] ??= {
  84. jsonData: null,
  85. timestamp: 0,
  86. };
  87. let fetchedJsonData = null;
  88.  
  89. if (cache.jsonData === null || Date.now() - cache.timestamp >= delayMs) {
  90. try {
  91. fetchedJsonData = await fetchJSON(url);
  92.  
  93. const propertyNamesCached = Object.keys(cache.jsonData || {});
  94. const propertyNamesFetched = Object.keys(fetchedJsonData || {});
  95.  
  96. if (propertyNamesCached.length !== propertyNamesFetched.length
  97. || propertyNamesCached.some(propName => propertyNamesCached[propName] !== propertyNamesFetched[propName])
  98. ) {
  99. try {
  100. await GM.setValue(localStorageId, fetchedJsonData);
  101. } catch (storageError) {
  102. console.error(`Error saving json data to local storage:`, storageError);
  103. }
  104. }
  105. } catch (error) {
  106. try {
  107. fetchedJsonData = await GM.getValue(localStorageId, null);
  108. } catch (storageError) {
  109. console.error(`Error loading ${localStorageId} from local storage:`, storageError);
  110. }
  111. }
  112.  
  113.  
  114. if (fetchedJsonData) {
  115. cache.jsonData = fetchedJsonData;
  116. }
  117. cache.timestamp = Date.now();
  118. }
  119.  
  120. return cache.jsonData || {};
  121. };
  122. })();
  123.  
  124. return {
  125. fetch,
  126. fetchJSON,
  127. fetchJsonWithStorageCache,
  128. };
  129. })();
  130. })();