MCPcopy
hub / github.com/ocsjs/ocsjs / discussion

Function discussion

packages/scripts/src/projects/icourse.ts:689–749  ·  view source on GitHub ↗
(
	remotePage: RemotePage,
	discussionStrategy: typeof ICourseProject.scripts.study.cfg.discussionStrategy
)

Source from the content-addressed store, hash-verified

687}
688
689async function discussion(
690 remotePage: RemotePage,
691 discussionStrategy: typeof ICourseProject.scripts.study.cfg.discussionStrategy
692) {
693 if (discussionStrategy === 'not-reply') {
694 return $msg_and_log('warn', '讨论自动回复功能已关闭(上方菜单栏-中国大学MOOC-学习脚本中开启)。');
695 }
696
697 let res = '';
698
699 if (discussionStrategy === 'max-show-up') {
700 const list = Array.from(document.querySelectorAll('.j-reply-all .f-pr .j-content'));
701 const mapping = new Map();
702 for (const item of list) {
703 mapping.set(item.textContent, (mapping.get(item.textContent) || 0) + 1);
704 }
705 const content = [...mapping.entries()].sort((a, b) => b[1] - a[1])?.[0]?.[0];
706 if (!content) {
707 $msg_and_log('error', '读取出现最多评论失败!');
708 }
709 res = content;
710 } else if (discussionStrategy === 'max-fav') {
711 const list = Array.from(document.querySelectorAll('.j-reply-all .f-pr'));
712 let max = 0;
713 let maxEl = undefined as Element | undefined;
714 for (const item of list) {
715 const num = parseInt(item.querySelector('.bar .num')?.textContent || '0');
716 if (num > max) {
717 max = num;
718 maxEl = item;
719 }
720 }
721 const content = maxEl?.querySelector('.j-content')?.textContent || '';
722 if (!content) {
723 $msg_and_log('error', '读取最多点赞评论失败!');
724 }
725 res = content;
726 } else if (discussionStrategy === 'use-newest') {
727 const content = document.querySelector('.j-reply-all .f-pr .first .j-content')?.textContent || '';
728 if (!content) {
729 $msg_and_log('error', '读取最新评论失败!');
730 }
731 res = content;
732 }
733
734 const p = document.querySelector<HTMLDivElement>('.j-reply-add div.ql-editor.ql-blank p');
735 if (p) {
736 p.innerText = res;
737 await $.sleep(1000);
738 const submit = document.querySelector('.j-reply-add .editbtn');
739 if (submit) {
740 await remotePage.click(submit);
741 $message.info('提交回复成功!');
742 } else {
743 $msg_and_log('error', '获取提交按钮失败!');
744 }
745 await $.sleep(2000);
746 } else {

Callers 1

studyFunction · 0.85

Calls 2

fromMethod · 0.80
$msg_and_logFunction · 0.70

Tested by

no test coverage detected