// ==UserScript== // @name study-hardly-wego // @namespace scriptcat.org/users/211395 // @version 0.0.7 // @description 好好学习——wego大学学习助手 // @author Song // @match *://km.weigaoholding.com:9081/kms/learn/kms_learn_courseware/kmsLearnCourseware.do?* // @license MIT // @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(); 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 入口 function awaitReady() { function detectReady() { if (window.frames.length < 1) { return; } 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); } console.warn('[learn] ready.', `mode=${mode}`); }, 1e3); }); } console.info('[learn] startup ...'); awaitReady().then((mode) => { console.info('[learn] ready', `mode=${mode}`); switch (mode) { case 'video': return initVideo(); case 'ppt': return initPPT(); } }) //endregion })();