Greasy Fork

wsxy_autoPlay

网上学院函数库:优化视频

目前为 2020-02-10 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.icu/scripts/395994/771511/wsxy_autoPlay.js

// ==UserLibrary==
// @name          wsxy_autoPlay
// @namespace     Vionlentmonkey
// @version       0.3
// @description   网上学院函数库:优化视频
// ==/UserLibrary==

/**
 * 使两类播放器均自动播放
 */
const autoPlay = () => {
  // 自动从课程封面进入播放页面
  if (document.querySelector('img[src="courseware/iconImg/z3.png"]')) {
    document.querySelector('img[src="courseware/iconImg/z3.png"]').click();
  }
  const video_media = document.getElementById('video_media'); // 新播放器
  const html5Player = document.getElementById('course_player5'); // 旧播放器
  // 学习进度超过 90% 和部分报错会以 alert 弹出。
  unsafeWindow.alert = message => {
    GM_notification(message, 'Alert');
    unsafeWindow.console.log(message);
  };
  if (video_media) {
    /**
     * 新播放器是否继续学习对话框调用 confirm,阻塞脚本运行。
     * 测试例:http://218.94.1.175:8087/sfxzwsxy/jypxks/modules/train/ware/course_ware_view.jsp?applyPk=3063755&courseType=1
     */
    unsafeWindow.confirm = message => {
      if (message === '是否继续学习?') {
        unsafeWindow.console.log(message);
        return true;
      } else {
        GM_notification(message, 'Confirm');
        unsafeWindow.console.log(message);
        // 新旧播放器统一从头播放重新来过
        return false;
      }
    };
    /**
     * 静音模式下自动播放无需用户授权
     * https://developer.mozilla.org/docs/Web/Media/Autoplay_guide#Autoplay_availability
     */
    if (GM_config.get('muted')) {
      video_media.querySelector('video').muted = true;
    }
    /**
     * https://developer.mozilla.org/docs/Web/Guide/Events/Media_events
     * 此处不支持用 .next(clearInterval(...))
     */
    video_media.querySelector('video').play();
  } else if (html5Player) {
    if (GM_config.get('muted')) {
      html5Player.muted = true;
    }
    // 旧播放器是否继续学习对话框
    if (document.getElementById('cancel')) {
      //document.getElementById('confirm').click(); // 继续学习;可能需要多次重复才能完成该课程。
      document.getElementById('cancel').click(); // 大侠还请重新来过
    }
    //旧播放器自动做题
    if (document.querySelectorAll('div.option>label>input[name="que"]').length > 0) {
      document.querySelectorAll('div.option>label>input[name="que"]')[0].click();
      document.querySelectorAll('div.option>label>input[name="que"]')[1].click(); // 兼容多选题
      document.getElementsByClassName('button')[0].click(); // 提交
      if (document.getElementsByClassName('button_xia').length === 1) {
        document.getElementsByClassName('button_xia')[0].click(); // 下一题
      }
      if (document.getElementsByClassName('button_wan').length === 1) {
        document.getElementsByClassName('button_wan')[0].click(); // 完成
      }
    }
    // 此处支持 .next(clearInterval(...)),但不取消可以保证持续播放,即使用户点击页面也不影响。
    html5Player.play();
  } else {
    // 旧版本播放器能否成功调用 HTML5 似乎是玄学问题,检测不到 HTML5 播放器则刷新。
    location.reload(true);
  }
};