Skip to content

Commit

Permalink
fix(script): 全面优化自动答题逻辑,并将搜索结果直接显示在各自的脚本面板下,无需反复跳转查看。
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed May 9, 2023
1 parent f793d43 commit 8712b24
Show file tree
Hide file tree
Showing 10 changed files with 496 additions and 848 deletions.
49 changes: 31 additions & 18 deletions packages/core/src/core/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export class OCSWorker<E extends RawElements = RawElements> extends CommonEventE
this.resolverIndex = 0;
this.totalQuestionCount = 0;

this.emit('start');
this.isRunning = true;

this.once('close', () => {
this.isClose = true;
});
Expand All @@ -65,9 +68,6 @@ export class OCSWorker<E extends RawElements = RawElements> extends CommonEventE
this.isStop = false;
});

this.emit('start');
this.isRunning = true;

/** 寻找题目父节点 */
const questionRoot: HTMLElement[] | null =
typeof this.opts.root === 'string' ? Array.from(document.querySelectorAll(this.opts.root)) : this.opts.root;
Expand Down Expand Up @@ -113,15 +113,18 @@ export class OCSWorker<E extends RawElements = RawElements> extends CommonEventE
/** 强行关闭 */
if (this.isClose === true) {
this.isRunning = false;
this.emit('close');
this.emit('done');
return results;
}

let type;
let error: string | undefined;

try {
/** 检查是否暂停中 */
if (this.isStop) {
await waitForContinuate(() => this.isStop);
}

/** 获取题目类型 */
if (typeof this.opts.work === 'object') {
type =
Expand All @@ -134,18 +137,6 @@ export class OCSWorker<E extends RawElements = RawElements> extends CommonEventE
: this.opts.work.type(ctx);
}

/** 检查是否暂停中 */
if (this.isStop) {
await new Promise<void>((resolve, reject) => {
const interval = setInterval(() => {
if (this.isStop === false) {
clearInterval(interval);
resolve();
}
}, 200);
});
}

/** 查找答案 */
const searchInfos = await this.opts.answerer(ctx.elements, type, ctx);
let resultPromise: { (): Promise<ResolverResult> } | undefined;
Expand Down Expand Up @@ -229,6 +220,16 @@ export class OCSWorker<E extends RawElements = RawElements> extends CommonEventE
/** 答题选择器线程, */
const resolverThread = new Promise<void>((resolve) => {
const start = async () => {
/** 强行关闭 */
if (this.isClose === true) {
this.isRunning = false;
return;
}
/** 检查是否暂停中 */
if (this.isStop) {
await waitForContinuate(() => this.isStop);
}

if (this.resolverIndex < this.totalQuestionCount) {
const resolver = resolvers.shift();

Expand Down Expand Up @@ -279,7 +280,6 @@ export class OCSWorker<E extends RawElements = RawElements> extends CommonEventE
]);

this.isRunning = false;
this.emit('done');
return results;
}

Expand Down Expand Up @@ -314,3 +314,16 @@ export class OCSWorker<E extends RawElements = RawElements> extends CommonEventE
}
}
}

async function waitForContinuate(isStopping: () => boolean) {
if (isStopping()) {
await new Promise<void>((resolve, reject) => {
const interval = setInterval(() => {
if (isStopping() === false) {
clearInterval(interval);
resolve();
}
}, 200);
});
}
}
4 changes: 2 additions & 2 deletions packages/core/src/elements/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class MessageElement extends IElement {
type: 'info' | 'success' | 'warn' | 'error' = 'info';
/** 内容 */
content: string | HTMLElement = '';
/** 持续时间,如果为0的话则一直存在 */
/** 持续时间(秒),如果为0的话则一直存在,默认为: 5 */
duration?: number;
/** 是否允许关闭 */
closeable?: boolean = true;
Expand All @@ -28,7 +28,7 @@ export class MessageElement extends IElement {
this.contentContainer.append(this.content);
}

this.duration = Math.max(this.duration === 0 ? 0 : this.duration || 5, 0);
this.duration = Math.max(this.duration ?? 5, 0);
this.append(this.contentContainer);

if (this.closeable) {
Expand Down
Loading

0 comments on commit 8712b24

Please sign in to comment.