Coursera EXT - Auto next video

Coursera Extension -- Automatically go to the next video upon reaching the end of the current one

  1. // ==UserScript==
  2. // @name Coursera EXT - Auto next video
  3. // @description Coursera Extension -- Automatically go to the next video upon reaching the end of the current one
  4. // @namespace http://sepczuk.com/
  5. // @version 0.36
  6. // @include https://*.coursera.org/*/lecture/view*
  7. // @match https://*.coursera.org/*/lecture/view*
  8. // @copyright 2012-2013, Damian Sepczuk, damian at sepczuk period delme com; loopkid
  9. // ==/UserScript==
  10.  
  11. function mainWrapper(){
  12. var debug = false;
  13. var US_SHORT_NAME = 'CEXT-ANVid';
  14. var US_VERSION = 0.36;
  15. function debugLog(msg) {
  16. if (!debug) return;
  17. console.log(US_SHORT_NAME + ": " + msg);
  18. }
  19.  
  20. /*!
  21. * jQuery Cookie Plugin
  22. * https://github.com/carhartl/jquery-cookie
  23. *
  24. * Copyright 2011, Klaus Hartl
  25. * Dual licensed under the MIT or GPL Version 2 licenses.
  26. * http://www.opensource.org/licenses/mit-license.php
  27. * http://www.opensource.org/licenses/GPL-2.0
  28. */
  29. (function($) {
  30. $.cookie = function(key, value, options) {
  31. // key and at least value given, set cookie...
  32. if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
  33. options = $.extend({}, options);
  34. if (value === null || value === undefined) {
  35. options.expires = -1;
  36. }
  37. if (typeof options.expires === 'number') {
  38. var days = options.expires, t = options.expires = new Date();
  39. t.setDate(t.getDate() + days);
  40. }
  41. value = String(value);
  42. return (document.cookie = [
  43. encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
  44. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  45. options.path ? '; path=' + options.path : '',
  46. options.domain ? '; domain=' + options.domain : '',
  47. options.secure ? '; secure' : ''
  48. ].join(''));
  49. }
  50. // key and possibly options given, get cookie...
  51. options = value || {};
  52. var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
  53. var pairs = document.cookie.split('; ');
  54. for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
  55. if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
  56. }
  57. return null;
  58. };
  59. })(jQuery);
  60. function main(){
  61. debugLog("Running main!");
  62. if (window.QL_player === undefined) {
  63. var retryAfterTImeout = 300;
  64. debugLog("QL_player window not found. Setting timeout to " + retryAfterTImeout + "ms.");
  65. setTimeout(main, retryAfterTImeout);
  66. return;
  67. }
  68. var CEXT_cookieName = 'autoNextVideo___ByFireBiker';
  69. var isAutoNextEnabledC = $.cookie(CEXT_cookieName);
  70. if ( typeof(isAutoNextEnabledC) === 'undefined' ) {
  71. debugLog("Cookie " + CEXT_cookieName + " not found. Setting true.");
  72. isAutoNextEnabledC = true;
  73. } else {
  74. isAutoNextEnabledC = isAutoNextEnabledC == 'true';
  75. debugLog("Cookie " + CEXT_cookieName + " found. Setting " + isAutoNextEnabledC + ".");
  76. }
  77. // style="margin: 0; padding: 0; vertical-align: bottom; position: relative; top: -2px; left: -5px;"
  78. var autoNextEl = $('<input type="checkbox" id="autonext" name="autonext" style="width: 13px; height: 13px; margin-right: .6em; margin-bottom: 2px;"/>')
  79. .click( function(e) {
  80. $.cookie(CEXT_cookieName, this.checked, {expires: 24*7*30*365*10, secure: true});
  81. e.stopPropagation(); // don't click the underlying link
  82. })
  83. .prependTo('.course-lecture-view-next-link')
  84. .prop('checked', isAutoNextEnabledC);
  85.  
  86. if(window.top.wasPlayerFullScreen) {
  87. var makeFullScreenInt = setInterval(function(){
  88. console.log("Testing full screen clickable... ")
  89. if($('.mejs-fullscreen-button button')){
  90. console.log("... OK ")
  91. $('.mejs-fullscreen-button button').click();
  92. clearInterval(makeFullScreenInt);
  93. } else {
  94. console.log("not yet!")
  95. }
  96. }, 10);
  97. }
  98.  
  99. // Center frame on no-full screen
  100. /* // No more fancy box and centering on coursera site!
  101. $('.mejs-fullscreen-button button').click(function(){
  102. var centerNonFullScreenItv = setInterval(function(){
  103. if (!QL_player.mediaelement_handle.isFullScreen) {
  104. console.log("Trying to center fancy-box... ")
  105. window.parent.$.fancybox.center();
  106. clearInterval(centerNonFullScreenItv);
  107. }
  108. }, 100);
  109. });
  110. */
  111.  
  112. window.top.wasPlayerFullScreen = undefined;
  113. QL_player.mediaelement_media.addEventListener("ended", function(){
  114. window.top.wasPlayerFullScreen = QL_player.mediaelement_handle.isFullScreen;
  115. var isAutoNextEnabled = autoNextEl[0].checked;
  116. if ( isAutoNextEnabled ) {
  117. $('.course-lecture-view-next-link').click();
  118. }
  119. });
  120. };
  121.  
  122. main();
  123. };
  124.  
  125.  
  126. if (!document.xmlVersion) {
  127. var script = document.createElement('script');
  128. script.appendChild(document.createTextNode('('+ mainWrapper +')();'));
  129. document.documentElement.appendChild(script);
  130. }