Greasy Fork is available in English.

Fuck Chaoxing

解除超星自动暂停播放的限制并添加自动播放下一集的功能

目前為 2018-06-06 提交的版本,檢視 最新版本

// ==UserScript==
// @name			Fuck Chaoxing
// @namespace		xuyiming.open@outlook.com
// @description		解除超星自动暂停播放的限制并添加自动播放下一集的功能
// @author			依然独特
// @version			1.2.0
// @grant			none
// @run-at			document-start
// @require			https://greasyfork.org/scripts/18715-hooks/code/Hooks.js?version=603418
// @require			https://greasyfork.org/scripts/29782-docsready/code/docsReady.js?version=603417
// @include			*://*.chaoxing.com/mycourse/studentstudy*
// @include			*://*.chaoxing.com/ananas/modules/video/index.html*
// @match			*://*.chaoxing.com/mycourse/studentstudy*
// @match			*://*.chaoxing.com/ananas/modules/video/index.html*
// @license			BSD 2-Clause
// @homepageURL		https://gist.github.com/xymopen/eb65046644ff5cb7c0668e5d4f9607d1
// ==/UserScript==

( function () {


	// So, let's first clarify the structure of Chaoxing Student Study Page

	// A course is made up of mulitple chapters
	// A chapter is made up of mulitple cards, saying a multi-media card and a unit test card
	// A card is made up of multiple jobs, saying two video jobs and a ppt job

	// When a video job is finished, finishJob() would be called on MoocPlayer,
	// which calls proxy_completed(),
	// whick calls ed_complete(),
	// which calls JC.completed on card iframe,
	// which emits an completed event,
	// which trigger unlock(),
	// which calls onReadComplete() on the top window

	// When a ppt job is loaded, uParse() would be called
	// which calls unlock()

	// onReadComplete() then calls onReadComplete1() to pull updated chapter list from the server
	// and calls jobflag() to figure out how many jobs remaining to finish

	// jobflag() looks in card iframe for .ans-job-icon as total jobs and .ans-job-finished as unfinished ones.
	// ppt jobs doesn't count for they don't have .ans-job-icon or .ans-job-finished

	// Generally speaking we only need to handle video jobs
	// However Chrome blocks Flash. :facepalm:

	function hookCXPlayer( onPlayerInit, contextWindow ) {
		if ( undefined === contextWindow ) {
			contextWindow = window;
		}

		// CXPlayer and pauseMovie() loaded as jQuery plug-ins
		// so hook jQuery to access it.
		Hooks.set( contextWindow, "jQuery", function ( target, propertyName, ignored, jQuery ) {
			Hooks.method( jQuery.fn, "cxplayer", function ( target, methodName, method, thisArg, args ) {
				var replyArgs = arguments, $globalPlayer, $player,
					globalConfig = args[ 0 ];

				function createCXPlayer( config ) {
					if ( undefined !== config ) {
						globalConfig = config;
						args[ 0 ] = config;
					}

					$globalPlayer = Hooks.Reply.method( replyArgs );

					return $globalPlayer;
				}

				$player = onPlayerInit( globalConfig, createCXPlayer );

				if ( undefined !== $player ) {
					$globalPlayer = $player;
				}

				return $globalPlayer;
			} );

			return Hooks.Reply.set( arguments );
		} );
	};

	function findCurIdx( list ) {
		return Array.prototype.findIndex.call( list, function ( chapter ) {
			return chapter.classList.contains( "currents" );
		} );
	};

	function canNextCard() {
		var contextDocument = window.top.document.querySelector( "iframe" ).contentDocument;

		return Array.prototype.filter.call( contextDocument.querySelectorAll( ".ans-job-icon" ), function ( jobContainer ) {
			return !jobContainer.parentNode.classList.contains( "ans-job-finished" );
		} ).length === 0;
	}

	function nextCard() {
		var document = window.document.querySelector( "iframe" ).contentDocument,
			cards, nextSectionIndex;

		cards = document.querySelectorAll( "#mainid .tabtags span" );
		nextSectionIndex = findCurIdx( cards ) + 1;

		if ( nextSectionIndex < cards.length ) {
			cards[ nextSectionIndex ].click();

			return true;
		} else {
			return false;
		}
	}

	function nextChapter() {
		var document = window.document,
			chapters = document.querySelectorAll(
				"#coursetree .ncells h1," +
				"#coursetree .ncells h2," +
				"#coursetree .ncells h3," +
				"#coursetree .ncells h4," +
				"#coursetree .ncells h5," +
				"#coursetree .ncells h6"
			),
			lastestChapter = Array.prototype.find.call( chapters, function ( chapter ) {
				// finished chapters are classified as blue
				// and locked chapters are classified as lock
				return !chapter.querySelector( ".blue" ) && !chapter.querySelector( ".lock" );
			} );


		// Go to the first unfinished and unlocked chapter
		if ( lastestChapter ) {
			lastestChapter.click();

			return true;
		} else {
			// or wait for next call when one locked chapter may be unlocked
			return false;
		}
	}

	if ( "/ananas/modules/video/index.html" === window.location.pathname ) {
		// Video Job iframe
		hookCXPlayer( function ( config, createCXPlayer ) {
			var $player;

			// https://mooc1-1.chaoxing.com/ananas/modules/video/cxplayer/moocplayer_4.0.11.js
			config.datas.pauseAdvertList = [];
			config.datas.preAdvertList = [];
			config.datas.isAutoPlayNext = true;
			config.datas.isDefaultPlay = true;
			config.datas.enableFastForward = true;
			config.datas.errorBackTime = false;

			// if ( config.events &&
			// 	config.events.onAnswerRight &&
			// 	!config.events.onAnswerRight.toString()
			// 		.replace( /(function .*?\(.*?\))/g, "" ).trim()		// remove function signifigure
			// 		.replace( /^\{|\}$/g, "" )
			// 		.replace( /\/\/.*(\r|\n|(\r\n))/g, "" )				// remove single line comment
			// 		.replace( /\/\*.*\*\//mg, "" )						// remove multiple line comment
			// 		.match( /^\s*$/ )
			// ) {
			// 	window.alert( "onAnswerRight() is not empty. It's unsafe to block the resource URL." );
			// }

			$player = createCXPlayer();

			// Unpausable playback
			// $player.bind( "onPause", function () {
			// 	$player.playMovie();
			// } );

			$player.bind( "onError", function () {
				if ( 4 === $player.getPlayState() ) {
					window.location.reload();
				}
			} );

			Hooks.method( window.jQuery.fn, "pauseMovie", function ( target, methodName, method, thisArg, args ) {
				/* empty */
			} );

			Object.keys( config.events ).forEach( e => $player.bind( e, () => {
				let state = [ "error", "playing", "paused", "hanging", "stop" ][ $player.getPlayState() ];

				console.debug( `[Fuck Chaoxing]${ e } is triggered. Player is ${ state }.` );
			} ) );
		} );
	} else if ( "/mycourse/studentstudy" === window.location.pathname ) {
		// Card iframe
		domReady().then( function () {

			var hasNextCard = true,
				onJobflagApplied = function () { },
				onAjaxCompleted = function () { };

			window.jQuery( document ).ajaxComplete( function () {
				onAjaxCompleted();
			} );

			Hooks.method( window, "onReadComplete1", function ( target, methodName, method, thisArg, args ) {
				var returns = Hooks.Reply.method( arguments );

				Promise.all( [ new Promise( function ( resolve ) {
					onJobflagApplied = resolve;
				} ), new Promise( function ( resolve ) {
					onAjaxCompleted = resolve;
				} ) ] ).then( function () {
					if ( !hasNextCard ) {
						nextChapter();
					}
				} );

				return returns;
			} );

			Hooks.method( window, "jobflag", function ( target, methodName, method, thisArg, args ) {
				if ( canNextCard() ) {
					hasNextCard = nextCard();
				}

				onJobflagApplied();

				return Hooks.Reply.method( arguments );
			} );
		} );
	}
} )();