SubtitleDownload

download a course's subtitle from edX, XueTangX and coursera

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        SubtitleDownload
// @namespace   github.com/lizheming/SubtitleDownload 
// @include     https://futurelearn.com/*
// @include     https://www.futurelearn.com/*
// @include     https://courses.edx.org/courses/*
// @include     http://www.xuetangx.com/courses/*
// @include     http://xuetangx.com/courses/*
// @include     https://class.coursera.org/*
// @version     1
// @require     http://libs.baidu.com/jquery/2.0.3/jquery.min.js
// @description   download a course's subtitle from edX, XueTangX and coursera
// @description:zh-cn  从 edX, XueTangX and coursera 下载字幕
// @grant       none
// ==/UserScript==

(function($) {
window.location.host.match(/futurelearn\.com/) && (function() {
function file_get_contents(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
var h = document.createElement("html");
h.innerHTML = xhr.responseText;
callback(h);
}
}
xhr.open("GET", url, true);
xhr.send(null);
}
file_get_contents(location.href, function(d) {
document.querySelector(".related-files").innerHTML += [].map.call(d.querySelectorAll("video div[data-kind='subtitles']"), function(sub) {
return '<li class="media media-small media-sectioned">'+
'<div class="media_centered_container">'+
'<div aria-hidden="true" class="media_icon icon" data-icon="x"></div>'+
'<div class="media_body">'+
'<div class="header header-double">'+
'<h5 class="headline headline-primary">'+
'<a class="futerlearnDown" href="'+sub.getAttribute('data-src')+'">'+sub.getAttribute('data-label')+' Subtitle Download</a>'+
'<span class="headline headline-secondary" style="margin-left:5px;">srt</span>'+
'</h5></div></div></div></li>';
}).join('');
})
}());
window.location.host == "class.coursera.org" && $('.course-item-list-section-list li').map(function(){return $('a:last', $(this)).get(0)}).each(function(i,item){
var chinese = this.href.replace('download.mp4?lecture_id', 'subtitles?q');
chinese += '_zh&format=srt';
$.ajax({type:"HEAD", url:chinese, complete:function(xhr,data) {
if(xhr.status != 200) {
chinese = chinese.replace('_zh&', '_zh-cn&');
$.ajax({type:"HEAD", url:chinese, complete:function(xhr,data) {
if(xhr.status != 200) return false;
$(item).before('<a target="_new" href="'+chinese+'" title="中文字幕下载">中</a>');
}})
} else $(item).before('<a target="_new" href="'+chinese+'" title="中文字幕下载">中</a>');
}})
})
/** edX **/
if(!$('li[data-index]')) return false;
function fake_click(obj) {
var ev = document.createEvent("MouseEvents");
ev.initMouseEvent(
"click", true, false, window, 0, 0, 0, 0, 0
, false, false, false, false, 0, null
);
obj.dispatchEvent(ev);
}
function export_raw(name, data) {
var urlObject = window.URL || window.webkitURL || window;
var export_blob = new Blob([data]);
var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
save_link.href = urlObject.createObjectURL(export_blob);
save_link.download = name;
fake_click(save_link);
}
function parseTime(time) {
var min = Math.floor(time / 60000);
var hour = Math.floor(time / 3600000);
var second = time / 1000 - hour * 3600 - min * 60;
min = String(min).length < 2 ? '0'+min : String(min);
hour = String(hour).length < 2 ? '0'+hour : String(hour);
second = second.toFixed(3);
second = String(second).length<6 ? '0'+second : String(second);
second = second.split('.').join(',');
return hour+':'+min+':'+second;
}
var l = '<a href="#" id="Subtitle_download" style="font-size:0.7em;">Subtitle download</a>';
$('section h2') && $('section h2').last().append(l);
$(document).on('click', '#Subtitle_download', function() {
if($('li[data-index]').length == 0) {
alert('Apologies, but no subtitles were found.');
return false;
}
subtitles = [];
$('li[data-index]').each(function() {
var start = parseTime($(this).attr('data-start'));
var end = parseTime( $(this).next().attr('data-start') ? $(this).next().attr('data-start') : +$(this).attr('data-start')+1000 );
subtitles.push($(this).attr('data-index'));
subtitles.push(start+' --> '+end);
subtitles.push($(this).text());
subtitles.push('');
})
export_raw($.trim($('ul li.active p').text().split(', current section')[0])+'.srt', subtitles.join('\r\n'));
})
})(jQuery);