Skip to content

Commit

Permalink
fix(script): 修复搜索结果超时后全部题库都显示超时的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Apr 4, 2023
1 parent a1de6ab commit a11b2a5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AnswererWrapper, SearchInformation, Result } from './interface';
import { request } from '../utils/request';
import { $ } from '../../utils';

/**
*
Expand Down Expand Up @@ -78,7 +79,10 @@ export async function defaultAnswerWrapperHandler(
headers: JSON.parse(JSON.stringify(headers || {}))
};
// 发送请求
const responseData = await request(url, requestData);
const responseData = await Promise.race([request(url, requestData), $.sleep(30 * 1000)]);
if (responseData === undefined) {
throw new Error('题库连接超时,请检查网络或者重试。');
}
/** 从 handler 获取搜索到的题目和回答 */

// eslint-disable-next-line no-new-func
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function request<T extends 'json' | 'text'>(
reject(new Error('GM_xmlhttpRequest is not defined'));
}
} else {
const fet: (...args: any[]) => Promise<Response> = env === 'node' ? require('node-fetch').default : fetch;
const fet: typeof fetch = env === 'node' ? require('node-fetch').default : fetch;

fet(url, { body: method === 'post' ? JSON.stringify(data) : undefined, method, headers })
.then((response) => {
Expand Down
83 changes: 27 additions & 56 deletions packages/core/src/core/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,49 +146,43 @@ export class OCSWorker<E extends RawElements = RawElements> extends CommonEventE
});
}

/** 查找题目 */
const searchInfos = await this.doAnswer(ctx.elements, type, ctx);

/** 查找答案 */
const searchInfos = await this.opts.answerer(ctx.elements, type, ctx);
let resultPromise: { (): Promise<ResolverResult> } | undefined;

if (!searchInfos) {
error = '答案获取失败, 请重新运行, 或者忽略此题。';
} else {
// 答案为 undefined 的情况, 需要赋值给一个空字符串,因为可能传回的题目中带有其他提示信息,或者题目里包含答案。
searchInfos.forEach((info) => {
info.results = info.results.map((ans) => {
ans.answer = ans.answer ? ans.answer : '';
return ans;
});
// 答案为 undefined 的情况, 需要赋值给一个空字符串,因为可能传回的题目中带有其他提示信息,或者题目里包含答案。
searchInfos.forEach((info) => {
info.results = info.results.map((ans) => {
ans.answer = ans.answer ? ans.answer : '';
return ans;
});
});

ctx.searchInfos = searchInfos;
ctx.searchInfos = searchInfos;

if (searchInfos.length === 0) {
error = '搜索不到答案, 请重新运行, 或者忽略此题。';
}
if (searchInfos.length === 0) {
error = '搜索不到答案, 请重新运行, 或者忽略此题。';
}

/** 开始处理 */
if (typeof this.opts.work === 'object') {
if (ctx.elements.options) {
/** 使用默认处理器 */

if (type) {
const resolver = defaultQuestionResolve(ctx)[type];
const handler = this.opts.work.handler;
resultPromise = async () =>
await resolver(searchInfos, ctx.elements.options as HTMLElement[], handler);
} else {
error = '题目类型解析失败, 请自行提供解析器, 或者忽略此题。';
}
/** 开始处理 */
if (typeof this.opts.work === 'object') {
if (ctx.elements.options) {
/** 使用默认处理器 */

if (type) {
const resolver = defaultQuestionResolve(ctx)[type];
const handler = this.opts.work.handler;
resultPromise = async () => await resolver(searchInfos, ctx.elements.options as HTMLElement[], handler);
} else {
error = 'elements.options 为空 ! 使用默认处理器, 必须提供题目选项的选择器。';
error = '题目类型解析失败, 请自行提供解析器, 或者忽略此题。';
}
} else {
/** 使用自定义处理器 */
const work = this.opts.work;
resultPromise = async () => await work(ctx);
error = 'elements.options 为空 ! 使用默认处理器, 必须提供题目选项的选择器。';
}
} else {
/** 使用自定义处理器 */
const work = this.opts.work;
resultPromise = async () => await work(ctx);
}

if (resultPromise) {
Expand Down Expand Up @@ -290,29 +284,6 @@ export class OCSWorker<E extends RawElements = RawElements> extends CommonEventE
return results;
}

/** 获取答案 */
private async doAnswer(elements: WorkContext<E>['elements'], type: string | undefined, ctx: WorkContext<E>) {
const timeout = 30;
/** 解析选项,可以自定义查题器 */

const answer = async () => {
return await Promise.race([
this.opts.answerer(elements, type, ctx),
/** 最长请求时间 */
$.sleep(timeout * 1000)
]);
};

let answers = await answer();
await $.sleep(3000);
if (!answers) {
/** 重试一次获取答案 */
answers = await answer();
}

return answers;
}

/** 答题结果处理器 */
async uploadHandler(options: {
// doWork 的返回值结果
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/elements/search.infos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class SearchInfosElement extends IElement {
el('summary', [el('a', { href: info.homepage, innerText: info.name })]),
...(info.error
? /** 显示错误信息 */
[el('span', { className: 'error' }, ['此题库搜题时发生错误:', info.error || '网络错误或者未知错误'])]
[el('span', { className: 'error' }, [info.error || '网络错误或者未知错误'])]
: /** 显示结果列表 */
[
...info.results.map((ans) => {
Expand Down
5 changes: 5 additions & 0 deletions packages/scripts/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,11 @@ search-infos-element .search-infos:hover .copy {
display: inline;
font-size: 12px;
}
search-infos-element .error {
color: #ff6b6ded;
display: inline-block;
padding-left: 12px;
}
.copy {
margin-left: 4px;
padding: 2px 4px;
Expand Down
6 changes: 6 additions & 0 deletions packages/scripts/assets/less/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,12 @@ search-infos-element {
font-size: 12px;
}
}

.error {
color: #ff6b6ded;
display: inline-block;
padding-left: 12px;
}
}

.copy {
Expand Down

0 comments on commit a11b2a5

Please sign in to comment.