AutoPlay

auto play video!

  1. // ==UserScript==
  2. // @name AutoPlay
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description auto play video!
  6. // @author Confusion
  7. // @match http://mooc1-2.chaoxing.com/mycourse/studentstudy?*
  8. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. var play_index = 1;
  17. var play_finish = true;
  18.  
  19. function set_auto_play() {
  20. var title = window.top.document.evaluate('//*[@id="mainid"]/h1', document).iterateNext();
  21. var title_text = title.textContent.replace(/[\r\n\t]/g, "");
  22.  
  23. var topWin = window.top.document.getElementById("iframe").contentWindow;
  24. var iframe1_element = topWin.document.evaluate('//*[@id="ext-gen1044"]/iframe', topWin.document).iterateNext();
  25. if (iframe1_element) {
  26. var status = topWin.document.evaluate('//*[@id="ext-gen1044"]', topWin.document).iterateNext();
  27. if (status && status.getAttribute("class").indexOf("ans-job-finished") == -1) {
  28. var topWin2 = iframe1_element.contentWindow;
  29. var video = topWin2.document.evaluate('//*[@id="video_html5_api"]', topWin2.document).iterateNext();
  30. if (video) {
  31. video.playbackRate = 2;
  32. video.muted = true;
  33. video.play();
  34. console.log("开始播放 " + title_text);
  35. video.addEventListener('pause', function () { //暂停开始执行的函数
  36. console.log("自动暂停播放");
  37. if (video.ended) {
  38. play_finish = true;
  39. console.log("播放完成 " + title_text);
  40. } else {
  41. video.play();
  42. }
  43. });
  44. return;
  45. }
  46. }
  47. }
  48. play_finish = true;
  49. console.log("任务跳过 " + title_text);
  50. return;
  51. }
  52. function play_next() {
  53. play_finish = false;
  54. var span_list = document.evaluate('//*[@id="coursetree"]/div/div/h4/a/span', document);
  55. for (var i = 1; i > 0; i++) {
  56. var element = span_list.iterateNext();
  57. if (!element){
  58. return;
  59. }
  60. if (i < play_index) {
  61. continue;
  62. }
  63. else {
  64. play_index += 1;
  65. setTimeout(set_auto_play, 3000);
  66. element.click();
  67. console.log('点击下一个');
  68. return;
  69. }
  70. };
  71. }
  72. function check() {
  73. console.log('状态检查');
  74. if (play_finish) {
  75. console.log('完成开始下一个');
  76. play_next();
  77. }
  78. }
  79.  
  80. function find_current_and_start() {
  81. try{
  82. window.clearInterval(cheak_interval);
  83. console.log('清理成功');
  84. }catch{
  85. pass;
  86. }
  87. var show_status_elem = document.evaluate('//*[@id="tit5"]', document).iterateNext();
  88. show_status_elem.textContent = '自动学习中...';
  89. var span_list = document.evaluate('//*[@id="coursetree"]/div/div/h4', document);
  90. for (var i = 1; i > 0; i++) {
  91. var element = span_list.iterateNext();
  92. if (element.getAttribute("class") == "currents") {
  93. play_index = i;
  94. console.log('找到当前位置');
  95. var cheak_interval = setInterval(check, 5000);
  96. return;
  97. }
  98. }
  99. }
  100. window.onload = function () {
  101. var button = document.createElement("li"); //创建一个input对象
  102. var show_status = document.createElement("li"); //创建一个input对象
  103. button.id = "tit4";
  104. button.style.fontSize = "15px";
  105. button.textContent = "自动学习视频";
  106. show_status.id = "tit5";
  107. show_status.style.fontSize = "15px";
  108. show_status.style.color = "red";
  109. button.onclick = function (){
  110. find_current_and_start();
  111. return;
  112. };
  113. var x = document.evaluate('//*[@id="selector"]/div[1]/div[1]/ul', document).iterateNext();
  114. x.appendChild(button);
  115. x.appendChild(show_status);
  116. };
  117. })();