MCPcopy
hub / github.com/ocsjs/ocsjs / doWork

Method doWork

packages/core/src/core/worker/worker.ts:38–329  ·  view source on GitHub ↗

启动答题器

(options?: { enable_debug?: boolean })

Source from the content-addressed store, hash-verified

36
37 /** 启动答题器 */
38 async doWork(options?: { enable_debug?: boolean }) {
39 this.emit('start');
40 this.isRunning = true;
41
42 this.once('close', () => {
43 this.isClose = true;
44 });
45
46 this.on('stop', () => {
47 this.isStop = true;
48 });
49
50 this.on('continuate', () => {
51 this.isStop = false;
52 });
53
54 /** 寻找题目父节点 */
55 const questionRoots: HTMLElement[] | null =
56 typeof this.opts.root === 'string' ? Array.from(document.querySelectorAll(this.opts.root)) : this.opts.root;
57
58 this.totalQuestionCount += questionRoots.length;
59
60 if (options?.enable_debug) {
61 console.debug('开始答题', this);
62 console.debug('题目数量: ', questionRoots.length);
63 console.debug('父节点列表: ', questionRoots);
64 }
65
66 /** 答题结果 */
67 const results: WorkResult<E>[] = [];
68
69 if (questionRoots.length === 0) {
70 throw new Error('未找到任何题目,答题结束。');
71 }
72
73 /** 搜索元素 */
74 for (const questionRoot of questionRoots) {
75 // 初始化上下文
76 const ctx: WorkContext<E> = {
77 searchInfos: [],
78 root: questionRoot,
79 elements: domSearchAll<E>(this.opts.elements, questionRoot),
80 type: undefined,
81 answerSeparators: this.opts.answerSeparators
82 };
83
84 /** 执行元素搜索钩子 */
85 await this.opts.onElementSearched?.(ctx.elements, questionRoot);
86 /** 排除掉 null 的元素 */
87 ctx.elements.title = ctx.elements.title?.filter(Boolean) as HTMLElement[];
88 ctx.elements.options = ctx.elements.options?.filter(Boolean) as HTMLElement[];
89
90 /** 获取题目类型 */
91 if (typeof this.opts.work === 'object') {
92 ctx.type =
93 this.opts.work.type === undefined
94 ? // 使用默认解析器
95 defaultWorkTypeResolver(ctx)

Callers 14

workOrExamFunction · 0.95
chapterFunction · 0.95
workAndExamFunction · 0.95
workFunction · 0.95
aiWorkFunction · 0.95
workOrExamFunction · 0.95
gxkWorkAndExamFunction · 0.95
xnkWorkFunction · 0.95
smartWorkFunction · 0.95
smartExamFunction · 0.95
fusioncourseWorkFunction · 0.95
hikeWorkFunction · 0.95

Calls 3

domSearchAllFunction · 0.90
defaultWorkTypeResolverFunction · 0.90
fromMethod · 0.80

Tested by

no test coverage detected