Skip to content

Commit ca48d7c

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

1 file changed

Lines changed: 24 additions & 21 deletions

File tree

chatgpt/bravegpt/bravegpt.user.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
// @description:zu Yengeza izimpendulo ze-AI ku-Brave Search (inikwa amandla yi-GPT-4o!)
149149
// @author KudoAI
150150
// @namespace https://kudoai.com
151-
// @version 2025.9.16.1
151+
// @version 2025.9.16.2
152152
// @license MIT
153153
// @icon https://assets.bravegpt.com/images/icons/app/icon48.png?v=e8ca7c2
154154
// @icon64 https://assets.bravegpt.com/images/icons/app/icon64.png?v=e8ca7c2
@@ -1304,30 +1304,33 @@
13041304

13051305
window.get = {
13061306

1307-
json(url) {
1307+
json(url) { // requires lib/json5.js
13081308
return new Promise((resolve, reject) => {
1309-
let retryCnt = 0;
1310-
(function getData(url) { xhr({
1311-
method: 'GET', url: url, onload: resp => {
1312-
if (resp.status == 404 && retryCnt < 1) { // try other format
1313-
retryCnt++ ; getData(url.endsWith('.json') ? url + '5' : url.slice(0, -1))
1314-
} else handleResp(resp, resolve, reject)
1315-
},
1316-
onerror: err => reject(new Error(`LOAD ERROR: ${err.message}`))
1317-
})})(url)
1318-
1319-
function handleResp(resp, resolve, reject) {
1320-
if (resp.status >= 300) { // status error
1321-
const errType = resp.status >= 300 && resp.status < 400 ? 'REDIRECT'
1322-
: resp.status >= 400 && resp.status < 500 ? 'CLIENT' : 'SERVER'
1323-
return reject(new Error(`${errType} ERROR: ${resp.status}`))
1324-
}
1325-
try { resolve(JSON5.parse(resp.responseText)) }
1326-
catch (err) { reject(new Error(`PARSE ERROR: ${err.message}`)) }
1309+
let retryCnt = 0 ; getData(url)
1310+
1311+
function getData(currentURL) {
1312+
xhr({
1313+
method: 'GET', url: currentURL,
1314+
onload: ({ responseText, status }) => {
1315+
if (status >= 300 && status != 404) {
1316+
const errType = status < 400 ? 'REDIRECT' : status < 500 ? 'CLIENT' : 'SERVER'
1317+
return reject(new Error(`${errType} ERROR: ${status}`))
1318+
}
1319+
try {
1320+
resolve(currentURL.endsWith('.json') ? JSON.parse(responseText)
1321+
: JSON5.parse(responseText))
1322+
} catch (err) {
1323+
retryCnt < 1 ? tryAltDataFormat() : reject(new Error(`PARSE ERROR: ${err.message}`)) }
1324+
},
1325+
onerror: err => {
1326+
retryCnt < 1 ? tryAltDataFormat() : reject(new Error(`LOAD ERROR: ${err.message}`)) }
1327+
})
13271328
}
1329+
1330+
function tryAltDataFormat() {
1331+
retryCnt++ ; getData(url.endsWith('.json') ? url + '5' : url.slice(0, -1)) }
13281332
})
13291333
},
1330-
13311334
async related(query) {
13321335

13331336
// Init API attempt props

0 commit comments

Comments
 (0)