MCPcopy
hub / github.com/ocsjs/ocsjs / workAndExam

Function workAndExam

packages/scripts/src/projects/icourse.ts:473–638  ·  view source on GitHub ↗
(
	rp: RemotePage,
	type: 'chapter-test' | 'work' | 'exam',
	{ answererWrappers, redundanceWordsText, upload, stopSecondWhenFinish, answerSeparators }: CommonWorkOptions
)

Source from the content-addressed store, hash-verified

471}
472
473function workAndExam(
474 rp: RemotePage,
475 type: 'chapter-test' | 'work' | 'exam',
476 { answererWrappers, redundanceWordsText, upload, stopSecondWhenFinish, answerSeparators }: CommonWorkOptions
477) {
478 CommonProject.scripts.workResults.methods.init({
479 questionPositionSyncHandlerType: 'icourse'
480 });
481
482 const titleTransform = (titles: (HTMLElement | undefined)[]) => {
483 return removeRedundantWords(
484 titles
485 .filter((t) => t?.innerText || t?.querySelector('img'))
486 .map((t) => {
487 if (t) {
488 const el = optimizationElementWithImage(t, true);
489 // textContent can still read the hidden image URL placeholders.
490 return (el.textContent || '').replace(/\s+/g, ' ').trim() || '';
491 }
492 return '';
493 })
494 .filter((t) => t.trim() !== '')
495 .join(',')
496 // /\u200B/g 排除不可见的空格
497 .replace(/[\u200A-\u200F]/g, ''),
498 redundanceWordsText.split('\n')
499 );
500 };
501 const work_type = type;
502
503 /** 新建答题器 */
504 const worker = new OCSWorker({
505 root: type === 'exam' ? '[class*=questionBody]' : '.u-questionItem',
506 elements: {
507 title: type === 'exam' ? '[class*=questionInfo]' : '.j-title .j-richTxt',
508 options: type === 'exam' ? '[class*=index-module__optionBody]' : '.choices li,.inputArea'
509 },
510 thread: 1,
511 answerSeparators: answerSeparators.split(',').map((s) => s.trim()),
512 /** 默认搜题方法构造器 */
513 answerer: (elements, ctx) => {
514 const title = titleTransform(elements.title);
515 if (title) {
516 return CommonProject.scripts.apps.methods.searchAnswerInCaches(title, async () => {
517 await $.sleep(5 * 1000);
518 return defaultAnswerWrapperHandler(answererWrappers, {
519 type: ctx.type || 'unknown',
520 title,
521 options: ctx.elements.options.map((o) => optimizationElementWithImage(o, true).innerText).join('\n')
522 });
523 });
524 } else {
525 throw new Error('题目为空,请查看题目是否为空,或者忽略此题');
526 }
527 },
528 work: {
529 /** 自定义处理器 */
530 async handler(type, answer, option) {

Callers 1

startFunction · 0.85

Calls 7

doWorkMethod · 0.95
uploadHandlerMethod · 0.95
titleTransformFunction · 0.70
$msg_and_logFunction · 0.70
initMethod · 0.65

Tested by

no test coverage detected