Skip to content

Commit

Permalink
feat(script): 新增【职教云】和【智慧职教】脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Mar 19, 2023
1 parent cb5fe71 commit 0cf366a
Show file tree
Hide file tree
Showing 3 changed files with 560 additions and 0 deletions.
151 changes: 151 additions & 0 deletions packages/scripts/src/projects/icve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { $el, Project, Script, $, $script, $$el, $creator, $model, $message } from '@ocsjs/core';
import { volume } from '../utils/configs';
import { createRangeTooltip, playMedia } from '../utils';

const state = {
study: {
currentMedia: undefined as HTMLMediaElement | undefined
}
};

export const ICVEProject = Project.create({
name: '智慧职教',
domains: ['icve.com.cn', 'course.icve.com.cn'],
studyProject: true,
scripts: {
study: new Script({
name: '🧑‍💻 课程学习',
namespace: 'icve.study.main',
url: [['课程学习页面', 'course.icve.com.cn/learnspace/learn/learn/templateeight/index.action']],
configs: {
notes: {
defaultValue: $creator.notes(['请手动点击任意章节以触发自动学习脚本']).outerHTML
},
volume,
playbackRate: {
label: '视频倍速',
attrs: {
type: 'range',
step: 1,
min: 1,
max: 16
},
defaultValue: 1,
onload() {
createRangeTooltip(this, '1', (val) => `${val}x`);
}
},
showScrollBar: {
label: '显示右侧滚动条',
attrs: { type: 'checkbox' },
defaultValue: true
},
expandAll: {
label: '展开所有章节',
attrs: { type: 'checkbox' },
defaultValue: true
}
},
async oncomplete() {
$script.pin(this);

await $.sleep(3000);

this.onConfigChange('volume', (v) => state.study.currentMedia && (state.study.currentMedia.volume = v));
this.onConfigChange(
'playbackRate',
(r) => state.study.currentMedia && (state.study.currentMedia.playbackRate = r)
);

const mainContentWin = $el<HTMLIFrameElement>('#mainContent')?.contentWindow as Window & { [x: string]: any };

if (mainContentWin) {
const _openLearnResItem: Function = mainContentWin.openLearnResItem;
mainContentWin.openLearnResItem = async (...args: any[]) => {
// 调用原函数
_openLearnResItem.apply(mainContentWin, args);
await $.sleep(3000);
await study();
};
}

if (this.cfg.showScrollBar) {
const bar = $el('.dumascroll_area', mainContentWin.document);
bar && (bar.style.overflow = 'auto');
}

if (this.cfg.expandAll) {
$$el('.s_sectionlist,.s_sectionwrap', mainContentWin.document).forEach((el) => (el.style.display = 'block'));
}

// 任务点
const jobs = $$el('.item_done_icon:not(.done_icon_show)', mainContentWin.document);

console.log(jobs);

/** 学习 */
const study = async () => {
const iframe = $el<HTMLIFrameElement>('iframe', mainContentWin.document);
const win = iframe?.contentWindow;
if (win) {
const doc = win.document;
if (iframe.src.includes('content_video.action')) {
// 视频
const video = $el<HTMLVideoElement>('video', doc);
state.study.currentMedia = video;

if (video) {
if (video.ended) {
video.currentTime = 0;
}

video.playbackRate = this.cfg.playbackRate;
video.volume = this.cfg.volume;

await new Promise<void>((resolve, reject) => {
try {
video.addEventListener('ended', async () => {
await $.sleep(3000);
resolve();
});
video.addEventListener('pause', async () => {
if (!video.ended) {
await $.sleep(1000);
playMedia(() => video.play());
}
});
// 开始播放
playMedia(() => video.play());
} catch (err) {
reject(err);
}
});
} else {
$message('error', { content: '未检测到视频,请刷新页面重试。' });
}
} else if (iframe.src.includes('content_doc.action')) {
// 文档只需点击就算完成,等待5秒下一个
await $.sleep(5000);
}
} else {
// 如果为 null 证明跨域
}

// 递归调用直到完成为止
if (jobs.length) {
const job = jobs.shift();
// 如果不是当前所处的任务点,则点击,否则可直接开始学习
if (job) {
// 这里不要调用 study() !!!,是通过上面回调进行调用 study,这里触发 openLearnResItem 即可
job.click();
}
} else {
$model('alert', {
content: '全部任务已完成'
});
}
};
}
})
}
});

0 comments on commit 0cf366a

Please sign in to comment.