// ==UserScript==
// @name 慕课网 下载视频
// @namespace https://github.com/Ahaochan/Tampermonkey
// @version 0.2.0
// @description 获取视频下载链接,使用方法:进入任意课程点击下载即可。如http://www.imooc.com/learn/814
// @author Ahaochan
// @match *://www.imooc.com/learn/*
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @require http://code.jquery.com/jquery-1.11.0.min.js
// @downloadURL none
// ==/UserScript==
//$(document).ready(function(){
/** 导出设置 */
var clarityType = 2;
var outTextType = "idm";
$("div.mod-tab-menu").after(
$("
")
);
$("input:radio").css("margin","auto 50px auto 3px");//设置单选框
$("input:radio[name=clarity]").change(function() { clarityType = this.value; textAreaChange(); });
$("input:radio[name=outText]").change(function() { outTextType = this.value; textAreaChange(); });
function textAreaChange(){
var downloadTextArea = getTextLinks(clarityType,outTextType);
GM_setClipboard(downloadTextArea);
$("#downloadBox textarea").text(downloadTextArea);
}
/** 导出设置 */
//获取下载链接
var videoes = [];
var selector = 'a.J-media-item';
var total = $(selector).length;
$(selector).each(function(index, element) {
var $this = $(this);
var vid = this.href.substring(this.href.lastIndexOf('/') + 1, this.href.length);
var name = this.innerText;
var pattern = /\(\d{2}:\d{2}\)/;
if (!pattern.test(name)) {
total--;
if (index == $(selector).length - 1 && !total) { console.log('没有视频可以下载!'); }
return;
}
name = name.replace(/\(\d{2}:\d{2}\)/, '').replace(/\s/g, '');
//v2(vid, name, $(this));
v3(vid, name, $(this));
});
//获取下载链接
/** 旧版接口,只能解析v1,v2 */
function v2(vid, name, $this){
$.getJSON("/course/ajaxmediainfo/?mid=" + vid + "&mode=flash", function(response) {
var url = response.data.result.mpath[0];
parseVideo(vid, name, url, $this);
});
}
/** 新版接口,解析v1,v2,v3 */
function v3(vid, name, $this){
GM_xmlhttpRequest({
method: "GET",
url: "http://m.imooc.com/video/"+vid,
onload: function(response) {
var pattern = /(http.+mp4)/;
var url = response.responseText.match(pattern)[0];
parseVideo(vid, name, url, $this);
}
});
}
/** 处理数据 */
function parseVideo(vid, name, url, $this){
var urlL = url.replace("H.mp4","M.mp4").replace("M.mp4","L.mp4");
var urlM = url.replace("H.mp4","M.mp4").replace("L.mp4","M.mp4");
var urlH = url.replace("L.mp4","M.mp4").replace("M.mp4","H.mp4");
var video = {
vid: vid,
name: name,
url: [ urlL, urlM, urlH ]
};
videoes.push(video);
//添加单个下载链接
var $link = $("下载");
$this.after($link);
$link.bind("DOMNodeInserted", function() { $(this).find("i").remove();} );//移除子标签
//添加全部下载链接
if (videoes.length == total) {
$("#downloadBox").append('共' + total + '个视频。已完成解析' + videoes.length + '个视频。已复制到剪贴板
');
$("#downloadBox").append($(""));//全部链接
videoes.sort(function(a,b){
if(a.name>b.name) return 1;
else if(a.name\n\t\t' + videoes[i].url[clarityType] + '\n\t\t' + videoes[i].name + '\n\t\n';
} else if(outTextType === "raw"){
str += videoes[i].url[clarityType]+"\n";
} else {//idm
str += "filename="+videoes[i].name+"&fileurl="+videoes[i].url[clarityType]+"\n";
}
}
if(outTextType === "xml") str = "\n\n"+str+'';
return str;
}
}
//});