Skip to content

Commit 84f86c5

Browse files
committed
even more fighting
1 parent 5cf431b commit 84f86c5

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/components/IMEField.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,35 @@ const JISHO_PROXY_BASE = import.meta.env.PROD
3838

3939
async function fetchKanjiFromJisho(reading: string): Promise<string[]> {
4040
if (!reading) return [];
41-
const jishoUrl = "https://jisho.org/api/v1/search/words?keyword=" + encodeURIComponent(reading);
4241

43-
const proxyUrl = import.meta.env.PROD
44-
? JISHO_PROXY_BASE + jishoUrl
45-
: JISHO_PROXY_BASE + "?url=" + encodeURIComponent(jishoUrl);
42+
const jishoBaseUrl = "jisho.org/api/v1/search/words?keyword=";
43+
const jishoFullUrl = jishoBaseUrl + encodeURIComponent(reading);
44+
45+
let proxyUrl: string;
46+
47+
if (import.meta.env.PROD) {
48+
proxyUrl = JISHO_PROXY_BASE + jishoFullUrl;
49+
} else {
50+
const fullJishoTarget =
51+
"https://jisho.org/api/v1/search/words?keyword=" + encodeURIComponent(reading);
52+
proxyUrl = JISHO_PROXY_BASE + "?url=" + encodeURIComponent(fullJishoTarget);
53+
}
4654

4755
try {
48-
const res = await fetch(proxyUrl, {
49-
headers: {
56+
const fetchOptions: RequestInit = {};
57+
58+
if (!import.meta.env.PROD) {
59+
fetchOptions.headers = {
5060
"X-Requested-With": "XMLHttpRequest",
51-
},
52-
});
61+
};
62+
}
63+
64+
const res = await fetch(proxyUrl, fetchOptions);
5365

5466
if (!res.ok) {
55-
console.error(`Error fetching from proxy: ${res.status} ${res.statusText}`);
67+
console.error(
68+
`Error fetching from proxy (${import.meta.env.PROD ? "CORS Anywhere" : "Thingproxy"}): ${res.status} ${res.statusText}`
69+
);
5670
try {
5771
const errorText = await res.text();
5872
console.error("Proxy error response:", errorText);

0 commit comments

Comments
 (0)