(
rp: RemotePage,
type: 'chapter-test' | 'work' | 'exam',
{ answererWrappers, redundanceWordsText, upload, stopSecondWhenFinish, answerSeparators }: CommonWorkOptions
)
| 471 | } |
| 472 | |
| 473 | function 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) { |
no test coverage detected