|
3 | 3 | // @description Add AI chat & product/category summaries to Amazon shopping, powered by the latest LLMs like GPT-4o! |
4 | 4 | // @author KudoAI |
5 | 5 | // @namespace https://kudoai.com |
6 | | -// @version 2025.9.16.1 |
| 6 | +// @version 2025.9.16.2 |
7 | 7 | // @license MIT |
8 | 8 | // @icon https://amazongpt.kudoai.com/assets/images/icons/app/black-gold-teal/icon48.png?v=8e8ed1c |
9 | 9 | // @icon64 https://amazongpt.kudoai.com/assets/images/icons/app/black-gold-teal/icon64.png?v=8e8ed1c |
|
834 | 834 |
|
835 | 835 | window.get = { |
836 | 836 |
|
837 | | - json(url) { |
| 837 | + json(url) { // requires lib/json5.js |
838 | 838 | 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 | + }) |
857 | 858 | } |
| 859 | + |
| 860 | + function tryAltDataFormat() { |
| 861 | + retryCnt++ ; getData(url.endsWith('.json') ? url + '5' : url.slice(0, -1)) } |
858 | 862 | }) |
859 | 863 | }, |
860 | 864 |
|
|
0 commit comments