Coursera EXT - Auto next video

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name            Coursera EXT - Auto next video
// @description     Coursera Extension -- Automatically go to the next video upon reaching the end of the current one
// @namespace       http://sepczuk.com/
// @version         0.36
// @include         https://*.coursera.org/*/lecture/view*
// @match           https://*.coursera.org/*/lecture/view*
// @copyright       2012-2013, Damian Sepczuk, damian at sepczuk period delme com; loopkid
// ==/UserScript==

function mainWrapper(){
    var debug = false;
    var US_SHORT_NAME = 'CEXT-ANVid';
    var US_VERSION = 0.36;
    
  	function debugLog(msg) {
        if (!debug) return;
        console.log(US_SHORT_NAME + ": " + msg);
  	}

    /*!
     * jQuery Cookie Plugin
     * https://github.com/carhartl/jquery-cookie
     *
     * Copyright 2011, Klaus Hartl
     * Dual licensed under the MIT or GPL Version 2 licenses.
     * http://www.opensource.org/licenses/mit-license.php
     * http://www.opensource.org/licenses/GPL-2.0
     */
    (function($) {
        $.cookie = function(key, value, options) {
    
            // key and at least value given, set cookie...
            if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
                options = $.extend({}, options);
    
                if (value === null || value === undefined) {
                    options.expires = -1;
                }
    
                if (typeof options.expires === 'number') {
                    var days = options.expires, t = options.expires = new Date();
                    t.setDate(t.getDate() + days);
                }
    
                value = String(value);
    
                return (document.cookie = [
                    encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
                    options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                    options.path    ? '; path=' + options.path : '',
                    options.domain  ? '; domain=' + options.domain : '',
                    options.secure  ? '; secure' : ''
                ].join(''));
            }
    
            // key and possibly options given, get cookie...
            options = value || {};
            var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
    
            var pairs = document.cookie.split('; ');
            for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
                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
            }
            return null;
        };
    })(jQuery);
    
    function main(){
        debugLog("Running main!");
        if (window.QL_player === undefined) {
            var retryAfterTImeout = 300;
            debugLog("QL_player window not found. Setting timeout to " + retryAfterTImeout + "ms.");
            setTimeout(main, retryAfterTImeout);
            return;
        }
        
        var CEXT_cookieName = 'autoNextVideo___ByFireBiker';
        var isAutoNextEnabledC = $.cookie(CEXT_cookieName);
        
        if ( typeof(isAutoNextEnabledC) === 'undefined' ) {
            debugLog("Cookie " + CEXT_cookieName + " not found. Setting true.");
            isAutoNextEnabledC = true;
        } else {
            isAutoNextEnabledC = isAutoNextEnabledC == 'true';
            debugLog("Cookie " + CEXT_cookieName + " found. Setting " + isAutoNextEnabledC + ".");
        }
        // style="margin: 0; padding: 0; vertical-align: bottom; position: relative; top: -2px; left: -5px;"
        var autoNextEl = $('<input type="checkbox" id="autonext" name="autonext" style="width: 13px; height: 13px; margin-right: .6em; margin-bottom: 2px;"/>')
             .click( function(e) {
                         $.cookie(CEXT_cookieName, this.checked, {expires: 24*7*30*365*10, secure: true});
                         e.stopPropagation(); // don't click the underlying link
                     })
             .prependTo('.course-lecture-view-next-link')
             .prop('checked', isAutoNextEnabledC);       

        if(window.top.wasPlayerFullScreen) {
            var makeFullScreenInt = setInterval(function(){
                console.log("Testing full screen clickable... ")
                if($('.mejs-fullscreen-button button')){
                    console.log("... OK ")
                    $('.mejs-fullscreen-button button').click();
                    clearInterval(makeFullScreenInt);
                } else {
                    console.log("not yet!")
                }
             }, 10);
        }

        // Center frame on no-full screen
        /* // No more fancy box and centering on coursera site!
        $('.mejs-fullscreen-button button').click(function(){            
            var centerNonFullScreenItv = setInterval(function(){
                    if (!QL_player.mediaelement_handle.isFullScreen) {
                        console.log("Trying to center fancy-box... ")
                        window.parent.$.fancybox.center();
                        clearInterval(centerNonFullScreenItv);
                    }
                }, 100);
        });
        */

        window.top.wasPlayerFullScreen = undefined;
        
        QL_player.mediaelement_media.addEventListener("ended", function(){
            window.top.wasPlayerFullScreen = QL_player.mediaelement_handle.isFullScreen;
            
            var isAutoNextEnabled = autoNextEl[0].checked;
            if ( isAutoNextEnabled ) {
                $('.course-lecture-view-next-link').click();
            }
        });
    };

    main();
};


if (!document.xmlVersion) {
    var script = document.createElement('script');
    script.appendChild(document.createTextNode('('+ mainWrapper +')();'));
    document.documentElement.appendChild(script);
}