// ==UserScript== // @name study-hardly-wego // @namespace scriptcat.org/users/211395 // @version 0.0.8 // @description 好好学习——wego大学学习助手 // @author Song // @match *://km.weigaoholding.com:9081/kms/learn/kms_learn_courseware/kmsLearnCourseware.do?* // @match *://km.weigaoholding.com:9081/ui-ext/scormcourse/* // @license LGPL // @grant none // ==/UserScript== (function () { 'use strict'; //region utils function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } /** * * @param container * @param el * @returns {boolean} */ function inScrollView(container, el) { if (!el) return false; const containerRect = container.getBoundingClientRect(); const elRect = el.getBoundingClientRect(); // 元素顶部 < 容器底部 const topIn = elRect.top < containerRect.bottom; // 元素底部 > 容器顶部 const bottomIn = elRect.bottom > containerRect.top; // 垂直方向可视(只判断垂直滚动常用) return topIn && bottomIn; } /** * * @returns {{name:string,url:string}[]} */ function collectFrames() { const arr = [] for (let i = 0; i < frames.length; i++) { const f = frames[i]; arr.push({name: f.name, url: f.location.href}); } return arr; } //endregion //region video function findVideo() { let frames = window.frames; let frame; for (let i = 0; i < frames.length; i++) { let f = frames[i];//name=mediaFramefrma if (f.name === 'mediaFrame') { frame = f; break; } } if (frame) { let v = frame.document.querySelector('video'); if (v) return v; } return null; } function play() { let v = findVideo(); v.playbackRate = 2; setTimeout(() => { v.play().then(() => { console.debug('自动播放'); }); }, 1e3) } function endCourse() { setTimeout(() => { let btn = document.querySelector('#finish_btn .course_btn'); if (btn) { btn.dispatchEvent(new MouseEvent('click')); } else { console.warn('[learn] Not Found "#finish_btn .course_btn" ') alert('Not Found "#finish_btn .course_btn"') } }, 1e3); } function initVideo() { let v = findVideo(); if (v.readyState === 4) { play(); } else { v.addEventListener('readystatechange', () => { if (v.readyState === 4) { play(); } }); v.addEventListener('canplay', play); } v.addEventListener('ended', endCourse); document.addEventListener('visibilitychange', (e) => { console.info('visibilitychange', e.visibilityState); if (document.visibilityState === 'visible') { play(); } }); } //endregion //region ppt function findPPT() { /** * * @param w {Window} * @returns {HTMLElement} */ function findInWindow(w) { let p = w.document.querySelector('#page-wrap'); if (p) { return p; } for (let i = 0; i < w.frames.length; i++) { const f = w.frames[i]; if (f.location.href.indexOf('/ui-ext/scormcourse') > 0) { p = f.document.querySelector('#page-wrap'); if (p) { return p; } } } return null } let p = findInWindow(window); if (p) { return p; } for (let i = 0; i < frames.length; i++) { const f = frames[i]; p = findInWindow(f); if (p) { return p; } } return null; } function initPPT() { /** * @type {HTMLElement|null} */ let page = null; const conf = {scroll: 320}; /** * 查找继续元素。 * @returns {{el:HTMLButtonElement,category:'continue'|'next'}|null} 初次学习返回下一章按钮,重复学习返回下一章的链接,找不到返回 null。 */ function findNextElement() { /** * * @type {HTMLButtonElement} */ let el = page.querySelector('.continue-btn'); if (el) { return {el, category: 'continue'}; } el = page.querySelector('.lesson-nav-link a[data-direction="next"]'); if (el) { return {el, category: 'next'}; } return null; } function study() { page = findPPT(); if (!page) { const link = document.querySelector('.cover__header-content-action-link'); if (link) { link.click(); return 'goto ppt'; } console.warn('[learn]', 'page not found'); return 'Not Found' } const video = page.querySelector('video'); if (video && !video.ended && inScrollView(page, video)) { if (video.readyState < 3) { return 'Video loading'; } if (video.paused) { video.playbackRate = 2; video.play().then(); return 'Video play'; } else { const p = 100 * video.currentTime / video.duration; return `Video playing ${p.toFixed(2)}`; } } const next = findNextElement(); if (!next) { page.scrollBy({top: conf.scroll, behavior: 'smooth'}); return 'Cannot Found Next-Element'; } if (inScrollView(page, next.el)) { next.el.click(); return `Click Next-${next.category}`; } else { page.scrollBy({top: conf.scroll, behavior: 'smooth'}); return `Scroll +${conf.scroll}`; } } function endIfPossible() { let b = document.querySelector('#learning_btn'); if (b.classList.contains('learning_btn')) { return false; } let btn = b.querySelector('.course_btn'); if (btn) { btn.dispatchEvent(new MouseEvent('click')); return true; } return false; } const timerId = setInterval(async () => { const s = study(); console.debug('[learn]', 'try study ppt', s); await sleep(1e3); if (endIfPossible()) { console.info('[learn]', 'ended'); clearInterval(timerId); } }, 3e3); } //endregion //region scorm function initScorm() { async function waitReady() { for (let i = 0; i < 202; i++) { if (document.querySelector('#slide-window svg')) { return true; } if (i % 10 === 0) { console.warn('[learn]', 'Course svg not found', i); } await sleep(1000); } return false; } function tryFinishBtn() { if (!window.parent) { return null; } let btn = window.parent.document.querySelector('finish_btn'); if (!btn || btn.display === 'none') { return 'none'; } try { btn.querySelector('.course_btn').click(); } catch (err) { console.error('[learn]', 'while click finish button', err); } return true; } async function playScorm() { const bot = document.querySelector('#bottom-bar'); const p = bot.querySelector('input'); if (!p) { return 310; } const max = parseInt(p.getAttribute('max')) const value = parseInt(p.value); const rest = max - value; return new Promise(resolve => { setTimeout(() => { const btn = bot.querySelector('#next') if (!btn) { return 410; } if (btn.style.display === 'none') { // 区分 等待测试 还是 学习完成 if (tryFinishBtn() === true) { resolve(200); } else { resolve(400); } } btn.click(); resolve(300); }, rest + 1e3); }) // return 200; } async function startScorm() { try { const code = await playScorm() let action = 'unknown'; if (code === 200) { action = 'end'; } else if (code >= 300 && code < 400) { action = 'continue'; setTimeout(startScorm, 1e3); } else if (code >= 400 && code < 500) { action = 'waiting test'; setTimeout(startScorm, 5e3); } else if (code >= 500 && code < 600) { action = 'nothing'; } console.info('[learn]', `play scorm return ${code}`, {action}); } catch (err) { console.error('[learn]', 'while play Scorm', err); } } console.info('[learn]', 'waiting ready'); waitReady().then(async (ready) => { console.info('[learn]', `ready = ${ready}`); if (ready) { startScorm().catch(err => { console.error('[learn]', 'while start Scorm', err); startScorm(); }); } else { alert('未检测到课程,请刷新'); } }).catch(err => { console.error('[learn]', 'while wait ready', err); }); } //endregion //region 入口 function awaitReady() { const prop = '$wego$_ready' function detectReady() { if (location.pathname.startsWith('/ui-ext/scormcourse')) { // 父窗口找不到此 frame ; 因此从子窗口通知父窗口。 if (window.parent) { window.parent[prop] = 'done'; } return 'scorm'; } if (window.frames.length < 1) { return 'Frame Not Found'; } if (findPPT()) { return 'ppt'; } if (findVideo()) { return 'video' } } return new Promise((resolve) => { let intervalId = setInterval(() => { const mode = detectReady(); if (mode) { if (intervalId) clearInterval(intervalId); resolve(mode); } if (window[prop] === 'done') { // 子窗口检测到,停止。 clearInterval(intervalId); } console.info('[learn] ready.', `mode=${mode}`, window[prop]); }, 1e3); }); } console.info('[learn] startup ...'); awaitReady().then((mode) => { console.info('[learn] ready', `mode=${mode}`); switch (mode) { case 'video': return initVideo(); case 'ppt': return initPPT(); case 'scorm': return initScorm(); } }) //endregion })();