Skip to content

Commit b8cd45b

Browse files
committed
fix: 🐛 修复自定义翻译 URL 为 ngrok 时需要验证警告页面的 bug
#307
1 parent 2a705b5 commit b8cd45b

1 file changed

Lines changed: 31 additions & 15 deletions

File tree

src/components/Manga/actions/translation/selfhosted.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {
22
createEffectOn,
33
createEqualsSignal,
4+
createRootMemo,
45
lang,
56
log,
67
sleep,
78
t,
89
} from 'helper';
9-
import { request } from 'request';
10+
import { request, type RequestDetails } from 'request';
1011

1112
import type { TaskState } from './helper';
1213

@@ -16,13 +17,30 @@ import { setOption } from '../helper';
1617
import { createFormData, createOptions, setMessage } from './helper';
1718

1819
const apiUrl = () =>
19-
store.option.translation.localUrl || 'http://127.0.0.1:5003';
20+
store.option.translation.localUrl?.replace(/\/$/, '') ||
21+
'http://127.0.0.1:5003';
22+
23+
const headers = createRootMemo(() => {
24+
if (apiUrl().includes('.ngrok-free.'))
25+
return { 'ngrok-skip-browser-warning': '69420' };
26+
});
27+
28+
export const api = async <T>(
29+
url: string,
30+
details?: RequestDetails<T>,
31+
retryNum = 0,
32+
) =>
33+
request<T>(
34+
`${apiUrl()}${url}`,
35+
{ ...details, headers: { ...details?.headers, ...headers() } },
36+
retryNum,
37+
);
2038

2139
// api 文档:<http://0.0.0.0:5003/docs>
2240

2341
/** 使用自部署服务器翻译指定图片 */
2442
export const selfhostedTranslation = async (url: string): Promise<string> => {
25-
const html = await request(apiUrl(), {
43+
const html = await api('/', {
2644
errorText: `${t('setting.option.paragraph_translation')} - ${t('alert.server_connect_failed')}`,
2745
});
2846

@@ -46,7 +64,7 @@ export const selfhostedTranslation = async (url: string): Promise<string> => {
4664
task_id: string;
4765
status: string;
4866
};
49-
const res = await request<resData>(`${apiUrl()}/submit`, {
67+
const res = await api<resData>('/submit', {
5068
method: 'POST',
5169
responseType: 'json',
5270
data: createFormData(imgBlob, 'selfhosted-old'),
@@ -63,10 +81,9 @@ export const selfhostedTranslation = async (url: string): Promise<string> => {
6381
while (!taskState?.finished) {
6482
try {
6583
await sleep(200);
66-
const res = await request<TaskState>(
67-
`${apiUrl()}/task-state?taskid=${task_id}`,
68-
{ responseType: 'json' },
69-
);
84+
const res = await api<TaskState>(`/task-state?taskid=${task_id}`, {
85+
responseType: 'json',
86+
});
7087
taskState = res.response;
7188
setMessage(
7289
url,
@@ -83,15 +100,15 @@ export const selfhostedTranslation = async (url: string): Promise<string> => {
83100
}
84101

85102
return URL.createObjectURL(
86-
await downloadImg(`${apiUrl()}/result/${task_id}`),
103+
await downloadImg(`${apiUrl()}/result/${task_id}`, {
104+
headers: headers(),
105+
}),
87106
);
88107
}
89-
90-
const headers_ngrok = apiUrl().includes('ngrok-free')? new Headers({ "ngrok-skip-browser-warning": "69420" }) : undefined;
91108
try {
92109
const res = await fetch(`${apiUrl()}/translate/with-form/image/stream`, {
93110
method: 'POST',
94-
headers: headers_ngrok,
111+
headers: headers(),
95112
body: createFormData(imgBlob, 'selfhosted'),
96113
});
97114

@@ -144,12 +161,11 @@ export const selfhostedTranslation = async (url: string): Promise<string> => {
144161
// 在拷贝漫画上莫名有概率报错
145162
// 虽然猜测可能是 cors connect-src 导致的,但在类似的 fantia 上却也无法复现
146163
// 也找不到第二个同样问题的网站,考虑到应该没人会在拷贝上翻译,就暂且不管了
147-
const res = await request<Blob>(`${apiUrl()}/translate/with-form/image`, {
164+
const res = await api<Blob>('/translate/with-form/image', {
148165
method: 'POST',
149166
responseType: 'blob',
150167
fetch: false,
151168
timeout: 1000 * 60 * 10,
152-
headers: headers_ngrok,
153169
data: createFormData(imgBlob, 'selfhosted'),
154170
errorText: t('translation.tip.upload_error'),
155171
});
@@ -168,7 +184,7 @@ export const updateSelfhostedOptions = async (noTip = false) => {
168184
if (store.option.translation.server !== 'selfhosted') return;
169185

170186
try {
171-
const res = await request(`${apiUrl()}`, {
187+
const res = await api('/', {
172188
noTip,
173189
errorText: `${t('setting.option.paragraph_translation')} - ${t('alert.server_connect_failed')}`,
174190
});

0 commit comments

Comments
 (0)