This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
97 lines (89 loc) · 3.88 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
(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);