jjm2473 / Mooc.ChaoxingUnlock

// ==UserScript==
// @name         Mooc.ChaoxingUnlock
// @namespace    https://openuserjs.org/user/jjm2473
// @version      0.2
// @description  Course unlock
// @author       jjm2473
// @match        http://mooc1-1.chaoxing.com/mycourse/studentstudy?*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var foreach=function(ary,cb){
        for(var i=0;i<ary.length;++i){
            cb(i,ary[i]);
        }
    };
    var targetWindow = window;
    if(typeof unsafeWindow != 'undefined'){
        targetWindow = unsafeWindow;
    }
    var targetDocument = targetWindow.document;
    console.error('Script Start!');
    var parseQueryString = function(str){
        var map={};
        var kvs=str.substring(1).split('&');
        foreach(kvs,function(i,kv){
            var kva=kv.split('=');
            map[kva[0]]=kva[1];
        });
        return map;
    };
    var querys = parseQueryString(targetWindow.location.search);
    var courseId = querys['courseId'];
    var clazzid = querys['clazzid'];
    targetWindow.courseId=courseId;
    targetWindow.clazzid=clazzid;
    var unlockOne=function(cell){
        if(cell.children.length == 0)
            return;
        var a=cell.children[0];
        if(a.href.startsWith('javascript:void')){
            var h4=a.children[0];
            var chapterId = h4.id.substring(3);
            a.href='javascript:getTeacherAjax(\''+courseId+'\',\''+clazzid+'\',\''+chapterId+'\');';
            h4.children[1].className='roundpointStudent';
        }
    };
    var unlockAll=function(){
        var ncells = targetDocument.getElementsByClassName('ncells');
        foreach(ncells,function(i,cell){
            unlockOne(cell);
        });
        console.log('Unlock All Chapters.');
    };
    targetWindow.unlockAll = unlockAll;
    //setTimeout(unlockAll,1000);
    var onReadComplete1 = function(){
        var chapterId = document.getElementById("chapterIdid").value;
        jQuery.ajax({
            type: "get",
            url : "/mycourse/studentstudycourselist",
            dataType:'html',
            data: {courseId:courseId,chapterId:chapterId,clazzid:clazzid},
            success: function(data){
                document.getElementById("coursetree").innerHTML=data;
                jQuery(".ncells h4").removeClass();
                jQuery(".ncells h5").removeClass();
                jQuery("#cur"+chapterId).addClass("currents");
                jQuery(".ncells .flush").css('display','none');
                jQuery("#cur"+chapterId).parent().parent().find(".flush").css('display','block');
                unlockAll();
            }
        });
    };
    var script = targetDocument.createElement('script');
    script.innerText='onReadComplete1='+onReadComplete1.toString();
    targetDocument.body.appendChild(script);
    setTimeout(unlockAll,10);
    setTimeout(function(){
    		top.document.onfocusin =  null;
			top.document.onfocusout = null;
			top.onfocus = window.onfocus = null;
			top.onblur = window.onblur = null;
       },500);
    console.error('Script End!');
})();