Skip to content

Commit 9877cc9

Browse files
committed
Fixed get.json() to try both JSON + JSON5 ↞ [auto-sync from https://github.com/adamlui/ai-web-extensions/tree/main/amazongpt]
1 parent 2b39040 commit 9877cc9

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

chatgpt/amazongpt/amazongpt.user.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @description Add AI chat & product/category summaries to Amazon shopping, powered by the latest LLMs like GPT-4o!
44
// @author KudoAI
55
// @namespace https://kudoai.com
6-
// @version 2025.9.16.1
6+
// @version 2025.9.16.2
77
// @license MIT
88
// @icon https://amazongpt.kudoai.com/assets/images/icons/app/black-gold-teal/icon48.png?v=8e8ed1c
99
// @icon64 https://amazongpt.kudoai.com/assets/images/icons/app/black-gold-teal/icon64.png?v=8e8ed1c
@@ -834,27 +834,31 @@
834834

835835
window.get = {
836836

837-
json(url) {
837+
json(url) { // requires lib/json5.js
838838
return new Promise((resolve, reject) => {
839-
let retryCnt = 0;
840-
(function getData(url) { xhr({
841-
method: 'GET', url: url, onload: resp => {
842-
if (resp.status == 404 && retryCnt < 1) { // try other format
843-
retryCnt++ ; getData(url.endsWith('.json') ? url + '5' : url.slice(0, -1))
844-
} else handleResp(resp, resolve, reject)
845-
},
846-
onerror: err => reject(new Error(`LOAD ERROR: ${err.message}`))
847-
})})(url)
848-
849-
function handleResp(resp, resolve, reject) {
850-
if (resp.status >= 300) { // status error
851-
const errType = resp.status >= 300 && resp.status < 400 ? 'REDIRECT'
852-
: resp.status >= 400 && resp.status < 500 ? 'CLIENT' : 'SERVER'
853-
return reject(new Error(`${errType} ERROR: ${resp.status}`))
854-
}
855-
try { resolve(JSON5.parse(resp.responseText)) }
856-
catch (err) { reject(new Error(`PARSE ERROR: ${err.message}`)) }
839+
let retryCnt = 0 ; getData(url)
840+
841+
function getData(currentURL) {
842+
xhr({
843+
method: 'GET', url: currentURL,
844+
onload: ({ responseText, status }) => {
845+
if (status >= 300 && status != 404) {
846+
const errType = status < 400 ? 'REDIRECT' : status < 500 ? 'CLIENT' : 'SERVER'
847+
return reject(new Error(`${errType} ERROR: ${status}`))
848+
}
849+
try {
850+
resolve(currentURL.endsWith('.json') ? JSON.parse(responseText)
851+
: JSON5.parse(responseText))
852+
} catch (err) {
853+
retryCnt < 1 ? tryAltDataFormat() : reject(new Error(`PARSE ERROR: ${err.message}`)) }
854+
},
855+
onerror: err => {
856+
retryCnt < 1 ? tryAltDataFormat() : reject(new Error(`LOAD ERROR: ${err.message}`)) }
857+
})
857858
}
859+
860+
function tryAltDataFormat() {
861+
retryCnt++ ; getData(url.endsWith('.json') ? url + '5' : url.slice(0, -1)) }
858862
})
859863
},
860864

0 commit comments

Comments
 (0)