Skip to content

Instantly share code, notes, and snippets.

@shykai
Last active September 5, 2024 15:27
Show Gist options
  • Save shykai/7ab11631d127fda2f07a9d5581624b72 to your computer and use it in GitHub Desktop.
Save shykai/7ab11631d127fda2f07a9d5581624b72 to your computer and use it in GitHub Desktop.
async function fetchMarketJSON(forceFetch = false) {
let sendReq = (typeof GM.xmlHttpRequest === 'function')? GM.xmlHttpRequest : GM_xmlhttpRequest;
if (typeof sendReq !== 'function') {
console.error("fetchMarketJSON GM.xmlHttpRequest null function");
isUsingLocalMarketJson = true;
const jsonStr = MARKET_JSON_LOCAL_BACKUP;
const jsonObj = JSON.parse(jsonStr);
if (jsonObj && jsonObj.time && jsonObj.market) {
jsonObj.market.Coin.ask = 1;
jsonObj.market.Coin.bid = 1;
console.log(jsonObj);
localStorage.setItem("MWITools_marketAPI_timestamp", Date.now());
localStorage.setItem("MWITools_marketAPI_json", JSON.stringify(jsonObj));
return jsonObj;
}
}
if (
!forceFetch &&
localStorage.getItem("MWITools_marketAPI_timestamp") &&
Date.now() - localStorage.getItem("MWITools_marketAPI_timestamp") < 900000
) {
return JSON.parse(localStorage.getItem("MWITools_marketAPI_json"));
}
console.log("fetchMarketJSON fetch github start");
let jsonStr = null;
jsonStr = await new Promise((resolve, reject) => {
sendReq({
url: MARKET_API_URL,
method: "GET",
synchronous: true,
timeout: 5000,
onload: async (response) => {
if (response.status == 200) {
console.log("fetchMarketJSON fetch github success 200");
resolve(response.responseText);
} else {
console.error("fetchMarketJSON fetch github onload with HTTP status failure " + response.status);
resolve(null);
}
},
onabort: () => {
console.error("fetchMarketJSON fetch github onabort");
resolve(null);
},
onerror: () => {
console.error("fetchMarketJSON fetch github onerror");
resolve(null);
},
ontimeout: () => {
console.error("fetchMarketJSON fetch github ontimeout");
resolve(null);
},
});
});
if (jsonStr === null && settingsMap.tryBackupApiUrl.isTrue) {
console.log("fetchMarketJSON fetch backup start");
jsonStr = await new Promise((resolve, reject) => {
sendReq({
url: MARKET_API_URL_BACKUP,
method: "GET",
synchronous: true,
timeout: 5000,
onload: async (response) => {
if (response.status == 200) {
console.log("fetchMarketJSON fetch backup success 200");
resolve(response.responseText);
} else {
console.error("fetchMarketJSON fetch backup onload with HTTP status failure " + response.status);
resolve(null);
}
},
onabort: () => {
console.error("fetchMarketJSON fetch backup onabort");
resolve(null);
},
onerror: () => {
console.error("fetchMarketJSON fetch backup onerror");
resolve(null);
},
ontimeout: () => {
console.error("fetchMarketJSON fetch backup ontimeout");
resolve(null);
},
});
});
}
if (!jsonStr) {
console.error("fetchMarketJSON network error, using local version");
isUsingLocalMarketJson = true;
jsonStr = MARKET_JSON_LOCAL_BACKUP;
} else {
isUsingLocalMarketJson = false;
}
const jsonObj = JSON.parse(jsonStr);
if (jsonObj && jsonObj.time && jsonObj.market) {
jsonObj.market.Coin.ask = 1;
jsonObj.market.Coin.bid = 1;
console.log(jsonObj);
localStorage.setItem("MWITools_marketAPI_timestamp", Date.now());
localStorage.setItem("MWITools_marketAPI_json", JSON.stringify(jsonObj));
return jsonObj;
}
console.error("MWITools: fetchMarketJSON JSON.parse error");
localStorage.setItem("MWITools_marketAPI_timestamp", 0);
localStorage.setItem("MWITools_marketAPI_json", "");
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment