Skip to content

Commit

Permalink
feat(script): 添加超星完成全部任务点后重新从开头学习功能
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Feb 26, 2024
1 parent cda009f commit 86454d3
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions packages/scripts/src/projects/cx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ export const CXProject = Project.create({
defaultValue: 'random' as VideoQuizStrategy
},
restudy: restudy,

backToFirstWhenFinish: {
label: '完成全部后重新学习',
attrs: {
type: 'checkbox',
title: '当章节已经学习完成至最后一章时,跳转到第一个章节重新开始学习。'
},
defaultValue: false
},
autoNextPage: {
label: '自动下一章',
attrs: { type: 'checkbox' },
Expand Down Expand Up @@ -489,8 +498,9 @@ export const CXProject = Project.create({
return;
}

let chapters = CXAnalyses.getChapterInfos();
let chapters = await CXAnalyses.waitForChapterInfos();

// 过滤掉已完成的章节
chapters = chapters.filter((chapter) => chapter.unFinishCount !== 0);

if (chapters.length === 0) {
Expand Down Expand Up @@ -838,6 +848,28 @@ const CXAnalyses = {
unFinishCount: parseInt(el.parentElement.querySelector('.jobUnfinishCount')?.value || '0')
}));
},
/**
* 等待并获取章节信息,直到获取到章节信息为止
* - 可设置超时时间(单位秒),默认10秒
* - 超时后返回空数组
*/
waitForChapterInfos(timeout = 10) {
return new Promise<any[]>((resolve, reject) => {
const interval = setInterval(() => {
const res = this.getChapterInfos();
if (res.length > 0) {
clearInterval(interval);
clearInterval(to);
resolve(res);
}
}, 1000);

const to = setTimeout(() => {
clearInterval(interval);
resolve([]);
}, timeout * 1000);
});
},
/** 检测页面是否使用字体加密 */
getSecretFont(doc: Document = document) {
return Array.from(doc.querySelectorAll('.font-cxsecret')).map((font) => {
Expand Down Expand Up @@ -969,6 +1001,7 @@ export async function study(opts: {
volume: number;
videoQuizStrategy: VideoQuizStrategy;
reloadVideoWhenError: boolean;
backToFirstWhenFinish: boolean;
workOptions: CommonWorkOptions;
}) {
await $.sleep(3000);
Expand Down Expand Up @@ -1038,12 +1071,24 @@ export async function study(opts: {

if (CXAnalyses.isInFinalChapter()) {
let content = '';
if (CXAnalyses.isFinishedAllChapters()) {
content = '全部任务点已完成!';

if (opts.backToFirstWhenFinish) {
content = '已经抵达最后一个章节,10秒后返回第一个章节重新开始。';
setTimeout(() => {
top?.document.querySelector<HTMLElement>('.posCatalog_name')?.click();
}, 10 * 1000);

$message('info', { content, duration: 30 });
} else {
content = '已经抵达最后一个章节!但仍然有任务点未完成,请手动切换至未完成的章节。';
if (CXAnalyses.isFinishedAllChapters()) {
content = '全部任务点已完成!';
} else {
content = '已经抵达最后一个章节!但仍然有任务点未完成,请手动切换至未完成的章节。';
}

$modal('alert', { content: content });
}
$modal('alert', { content: content });

CommonProject.scripts.settings.methods.notificationBySetting(content, {
duration: 0,
extraTitle: '超星学习通学习脚本'
Expand Down

0 comments on commit 86454d3

Please sign in to comment.