# Api文档

# 超星刷课脚本框架

# 开箱即用

直接复制源代码到超星界面即可运行

var ocs = Ocs(winodw)//Ocs(unsafeWindow)
ocs.CxPageLisenner(function (page) {
	console.log("监听到页面改变信息")
	console.log(page)     
    //............进行任务点的代码
    ocs.next(page.pageInfo) //下一个界面
})
字段解释 类型 返回值 说明
Ocs object object ocs 对象,包含封装好的方法CxPageLisenner next getIframeJob ...等
Ocs(winodw) void object 当前窗口对象,如果是js文件,直接传入 window 即可
如果是油猴脚本,请传入 油猴内置变量:unsafeWindow
CxPageLisenner(callback) void undefined CxPageLisenner 函数会监听超星学习界面的动作:上一页,下一页,标签栏点击,侧边栏点击,进入新旧章节,如果其中一个事件发生,就会被 CxPageLisenner 监听到,并且返回一个 page 对象,对象属性参考下面__page对象的json实例__
next(pageInfo) void boolean next 函数会跳转到下一个界面,并且返回一个boolean值:
true,跳转下一个界面成功
false,课程进行到最后章节,跳转失败

page对象的json实例

//page对象的json格式,测试结果,仅当参考
{
	"pageInfo": {	//页面信息,下面都是超星的字段,chapterId是章节的id,direction 是监听到的事件类型
		"num": 1,
		"totalnum": 4,
		"chapterId": "362395106",
		"courseId": "214954980",
		"clazzid": "32958198",
		"knowledgestr": "",
		"direction": "tabtags",		//这里是事件类型,下面有说明
		"cpi": "109313056"
	},
	"pageName": "唐诗与科举(一)",
	"pageJob": [{	//页面所有的任务点(包括已经完成的)
		"finish": true, 
		"start": false,
		"error": false,
		"document": {...},			//这里是任务点的document布局
		"type": "视频",			  //这里是任务点的类型,下面有说明
		"url": "https://mooc1-1.chaoxing.com/ananas/modules/video/index.html?v=2020-1016-2010",
		"desc": "1.唐诗与科举(一)-720p.mp4",
		"chapter": "1.1唐诗与科举(一)"
	}]
}

# 事件类型

属性 类型 对应事件
page.pageInfo.direction String next
previous
new
old
tabtags
sidebar
下一页
上一页
进入新章节
进入旧章节
点击标签栏
点击侧边栏
  • tabtags

    image-20201024164813064

  • previous/ next

    image-20201024164902290

  • sidebar

    image-20201024165052338

  • new / old,如果下一页是链接到新的章节,比如唐诗与科举(二),那么事件应该为new而不是next

# 任务点类型

属性 类型
page.pageJob.type String 视频
音频
章节测验
ppt
文档
书本

# 核心源代码

点击查看代码


function Ocs(unsafeWindow) {
    return {
        //获取url参数
        top: unsafeWindow.top,
        GetQueryString(name, url) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = url != undefined ? (url.substring(url.indexOf("?") + 1, url.length)).match(reg) : window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        },
        has(url, str) {
            return url.indexOf(str) != -1
        },
        getType(url) {
            let t = ''
            if (this.has(url, '/ananas/modules/video')) t = "视频"
            else if (this.has(url, '/ananas/modules/audio')) t = "音频"
            else if (this.has(url, '/ananas/modules/work')) t = "章节测验"
            else if (this.has(url, '/ananas/modules/ppt')) t = "ppt"
            else if (this.has(url, '/ananas/modules/pdf')) t = "文档"
            else if (this.has(url, '/ananas/modules/innerbook/')) t = "书本"
            return t;
        },
        //获取当前界面的所有任务点,包括已完成的
        getIframeJob(iframe) {
            let alljob = []
            let _this = this
            findJob(iframe)
            function findJob(iframe) {
                if (iframe != undefined) {
                    $(iframe).each(function (i, e) {
                        try {
                            //判断是否完成
                            let finish = $(e).parent('div').hasClass('ans-job-finished') != 0
                            //判断是否是任务点
                            if ($(e).parent('div').find('.ans-job-icon').length != 0) {
                                let type = _this.getType($(e).contents()[0].URL)
                                let job = {
                                    finish: finish,
                                    start: false,
                                    error: false,
                                    document: (type == '章节测验' || type == '书本') ? $(e).contents().find('iframe').contents()[0] : $(e).contents()[0],
                                    type: type,
                                    url: $(e).contents()[0].URL,
                                    desc: $(e).parent('div').text().replace(/\s+/g, ""),
                                    chapter: $('.currents a').not('span').text().replace(/\s+/g, "")
                                }
                                if (type != '') {
                                    alljob.push(job)
                                }
                            }
                            if ($(e).contents().find('iframe').length != 0) {
                                findJob($(e).contents().find('iframe'))
                            }
                        } catch (error) {
                            console.log(error)
                        }
                    })
                }
            }
            return alljob
        },
        //界面变化监听函数
        CxPageLisenner(pageChange) {
            let _this = this

            //开启监听
            this.lisenner(function (num, totalnum, chapterId, courseId, clazzid, knowledgestr, direction) {
                hasChang(num, totalnum, chapterId, courseId, clazzid, knowledgestr, direction)
            })

            //劫持函数,监听界面变化,并判断界面加载完成
            let change = this.top.changeDisplayContent
            this.top.changeDisplayContent = function (num, totalnum, chapterId, courseId, clazzid, knowledgestr, direction) {
                change(num, totalnum, chapterId, courseId, clazzid, knowledgestr, direction)
            }
            let getTeacherAjax = this.top.getTeacherAjax
            this.top.getTeacherAjax = function (courseId, clazzid, chapterId, cpi, chapterVerCode) {
                hasChang('', '', chapterId, courseId, clazzid, '', 'sidebar')
                getTeacherAjax(courseId, clazzid, chapterId, cpi, chapterVerCode)
            }
            function hasChang(num, totalnum, chapterId, courseId, clazzid, knowledgestr, direction) {
                let pageInfo = {
                    num: num,
                    totalnum: totalnum,
                    chapterId: chapterId,
                    courseId: courseId,
                    clazzid: clazzid,
                    knowledgestr: knowledgestr,
                    direction: direction,
                    cpi: _this.GetQueryString("cpi", $('iframe').eq(0).attr('src'))
                }
                let page = {
                    pageInfo: pageInfo,
                    pageName: $('h1').text(),
                    pageJob: [],
                }

                if (pageChange) {
                    setTimeout(() => {
                        pageInfo.num = num
                        pageInfo.totalnum = totalnum
                        pageInfo.chapterId = chapterId
                        pageInfo.courseId = courseId
                        pageInfo.clazzid = clazzid
                        pageInfo.knowledgestr = knowledgestr
                        pageInfo.direction = direction
                        page.pageJob = _this.getIframeJob(_this.top.$('iframe'))
                        pageChange(page)
                    }, 3000)
                }
            }


        },
        lisenner(pageChange) {
            //重写超星函数
            let _this = this
            this.top.changeDisplayContent = function (num, totalnum, chapterId, courseId, clazzid, knowledgestr, direction) {
                num = parseInt(num);
                totalnum = parseInt(totalnum);
                _this.top.PCount.setIndex(num);
                for (var i = 1; i <= totalnum; i++) {
                    var titledoc = document.getElementById("dct" + i);
                    var leftdoc = document.getElementById("left" + i);
                    var rightdoc = document.getElementById("right" + i);
                    if (i == num) {
                        titledoc.className = "c" + i + " currents";
                        leftdoc.style.display = "block";
                        rightdoc.style.display = "block";
                        num = num - 1;

                        console.log(_this.GetQueryString("cpi", $('iframe').eq(0).attr('src')))
                        document.getElementById("iframe").src = "/knowledge/cards?clazzid=" + clazzid + "&courseid=" + courseId + "&knowledgeid=" + chapterId + "&num=" + num + "&ut=s&cpi=" + _this.GetQueryString("cpi", $('iframe').eq(0).attr('src')) + "&v=20160407-1";
                        //监听界面加载完成
                        document.getElementById("iframe").onload = function () {
                            if (pageChange) pageChange(num, totalnum, chapterId, courseId, clazzid, knowledgestr, 'tabtags')
                        }
                        var el = $('#iframe');
                        unsafeWindow.ed_reinitIframe = function ed_reinitIframe() {
                            var iframe = el[0];

                            try {
                                var bHeight = iframe.contentWindow.document.body.scrollHeight;
                                var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
                                var height = Math.max(bHeight, dHeight);
                                el.attr('height', height);
                            } catch (ex) { }
                        }
                        //var openlockdiv=document.getElementById("openlock");
                        if ($("#openlock").length > 0) {
                            if (num + 1 == totalnum) {
                                setTimeout('openlockshow();', 2000);
                            } else {
                                document.getElementById("openlock").style.display = "none";
                            }
                        }
                        unsafeWindow.setInterval("ed_reinitIframe()", 200);
                        //jobflagOperation();
                    } else {
                        titledoc.className = "c" + i;
                        leftdoc.style.display = "none";
                        rightdoc.style.display = "none";
                    }
                }
                if (typeof (MoocPlayers) != 'undefined') {
                    _this.top.MoocPlayers.clearMids();
                }
            }
            this.top.PCount = (function () {
                var cur = 1, inner = _this.top.changeDisplayContent;

                return {
                    setIndex: function (index) {
                        cur = index;
                    },
                    next: function (count, chapterId, courseId, clazzid, knowledgestr) {
                        cur = parseInt(cur);
                        count = parseInt(count);
                        if (cur >= count) {
                            document.getElementById("mainid").innerHTML = "<div style=\"width:32px;height:32px;margin:0 auto;padding:300px 0\"><img src=\"/images/courselist/loading.gif\" /></div>"
                            cur = 1;
                            $.post("/mycourse/changeCapter",
                                {
                                    courseId: courseId
                                    , clazzid: clazzid
                                    , chapterId: chapterId
                                    , knowledgestr: knowledgestr
                                    , type: 1
                                    , date: new Date()
                                },
                                function (data) {
                                    //alert(20160407);
                                    data = data.replace(/(^\s*)|(\s*$)/g, "");
                                    var doc = document.getElementById("mainid");
                                    $(doc).html(data);
                                    chapterId = document.getElementById("chapterIdid").value;
                                    $(".ncells h4").removeClass();
                                    $(".ncells h5").removeClass();
                                    $("#cur" + chapterId).addClass("currents");
                                    $(".ncells .flush").css('display', 'none');
                                    $("#cur" + chapterId).parent().parent().find(".flush").css('display', 'block');
                                    var el = $('#iframe');

                                    unsafeWindow.ed_reinitIframe = function ed_reinitIframe() {
                                        var iframe = el[0];

                                        try {
                                            var bHeight = iframe.contentWindow.document.body.scrollHeight;
                                            var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
                                            var height = Math.max(bHeight, dHeight);
                                            el.attr('height', height);
                                        } catch (ex) {
                                        }
                                    }
                                    // var openlockdiv=document.getElementById("openlock");
                                    if ($("#openlock").length > 0) {
                                        var count = document.getElementById("cardcount").value;
                                        if (count == 1) {
                                            setTimeout('openlockshow();', 2000);
                                        }
                                    }

                                    if ($("#cur" + chapterId + " .orange01").length > 0) {

                                        $.ajax({
                                            type: "get",
                                            url: "/edit/validatejobcount",
                                            data: {
                                                courseId: courseId
                                                , clazzid: clazzid
                                                , nodeid: chapterId
                                            },
                                        });
                                    }

                                    unsafeWindow.setInterval("ed_reinitIframe()", 200);
                                    //jobflagOperation();
                                    _this.top.getClazzNote();
                                }
                            );
                            //监听回调,这里表示新的章节
                            if (pageChange) pageChange(cur, count, chapterId, courseId, clazzid, knowledgestr, "new")
                        } else {
                            cur++;
                            //监听回调,这里是下一个界面
                            inner(cur, count, chapterId, courseId, clazzid, knowledgestr, "next");

                        }
                        scroll(0, 0);
                    },
                    previous: function (count, chapterId, courseId, clazzid, knowledgestr) {
                        cur = parseInt(cur);
                        count = parseInt(count);
                        if (cur <= 1) {
                            document.getElementById("mainid").innerHTML = "<div style=\"width:32px;height:32px;margin:0 auto;padding:300px 0\"><img src=\"/images/courselist/loading.gif\" /></div>"
                            cur = 1;
                            $.post("/mycourse/changeCapter",
                                {
                                    courseId: courseId
                                    , clazzid: clazzid
                                    , chapterId: chapterId
                                    , knowledgestr: knowledgestr
                                    , type: 0
                                    , date: new Date()
                                },
                                function (data) {
                                    data = data.replace(/(^\s*)|(\s*$)/g, "");
                                    var doc = document.getElementById("mainid");
                                    $(doc).html(data);
                                    chapterId = document.getElementById("chapterIdid").value;
                                    $(".ncells h4").removeClass();
                                    $(".ncells h5").removeClass();
                                    $("#cur" + chapterId).addClass("currents");
                                    $(".ncells .flush").css('display', 'none');
                                    $("#cur" + chapterId).parent().parent().find(".flush").css('display', 'block')
                                    var el = $('#iframe');

                                    unsafeWindow.ed_reinitIframe = function ed_reinitIframe() {
                                        var iframe = el[0];

                                        try {
                                            var bHeight = iframe.contentWindow.document.body.scrollHeight;
                                            var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
                                            var height = Math.max(bHeight, dHeight);
                                            el.attr('height', height);
                                        } catch (ex) {
                                        }
                                    }
                                    // var openlockdiv=document.getElementById("openlock");
                                    if ($("#openlock").length > 0) {
                                        var count = document.getElementById("cardcount").value;
                                        if (count == 1) {
                                            setTimeout('openlockshow();', 2000);
                                        }
                                    }

                                    if ($("#cur" + chapterId + " .orange01").length > 0) {

                                        $.ajax({
                                            type: "get",
                                            url: "/edit/validatejobcount",
                                            data: {
                                                courseId: courseId
                                                , clazzid: clazzid
                                                , nodeid: chapterId
                                            },
                                        });
                                    }

                                    unsafeWindow.setInterval("ed_reinitIframe()", 200);
                                    //jobflagOperation();
                                    _this.top.getClazzNote();
                                }
                            );
                            //监听回调,这里表示旧的章节
                            if (pageChange) pageChange(cur, count, chapterId, courseId, clazzid, knowledgestr, "old")
                        } else if (cur > 0) {
                            cur--;
                            //监听回调,这里是上一个界面
                            inner(cur, count, chapterId, courseId, clazzid, knowledgestr, "previous");
                        }
                        scroll(0, 0);
                    }
                }
            })();

        }

    }
}