├── .gitignore
├── QRcode
└── WechatGroupQRcode.png
├── README.md
├── capture
├── Udemy 效果图.png
├── 中国大学 MOOC 效果图.png
├── 微信公众号文章内视频下载效果图.png
└── 网易云课堂效果图.png
└── javascript files
├── Udemy下载助手(需配合Aria2使用).user.js
├── 中国大学 MOOC 下载助手(需配合Aria2使用).user.js
├── 微信公众号文章内视频下载(需配合Aria2使用).user.js
└── 网易云课堂下载助手(需配合Aria2使用).user.js
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | .DS_Store
3 |
--------------------------------------------------------------------------------
/QRcode/WechatGroupQRcode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mofiter/UserScripts/fb3ee231ad67b24c7d60a1de5fbea14fc9e185f3/QRcode/WechatGroupQRcode.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 个人开发油猴脚本集合
2 | ## [中国大学 MOOC 下载助手](https://greasyfork.org/zh-CN/scripts/372684)
3 | ### 功能
4 | 在中国大学 MOOC 的课程学习页面添加批量下载按钮,用来下载视频和文档资源,可以进行下载设置,下载功能由 Aria2 提供,所以下载课程前一定要配置好 Aria2,不然脚本无效,Aria2 的配置请看这篇文章:[如何配置 Aria2 来进行文件下载](https://mofiter.com/2018-08-19/%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE-Aria2-%E6%9D%A5%E8%BF%9B%E8%A1%8C%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD.html) 。
5 | ### 注意
6 | - 请注意你所在的学习页面的地址,形式为 www.icourse163.org/learn/xxx 这样的才会有批量下载按钮,也就是你必须先参加这门课;
7 | - 第一次使用脚本时,请先点击下载设置,然后填写你要下载视频的保存地址,Mac 上的地址形式为:Users/xxx/Downloads/icourse163,Windows 上的地址形式为:D:\Downloads\icourse163,当然具体保存在哪个文件夹由你自己决定;
8 | - 只在 Chrome 浏览器测试了可以正常使用,其他浏览器能不能用全看缘分;
9 | - 此脚本的目的只为方便个人学习,请勿用于商业用途,否则后果自负。
10 | ## [网易云课堂下载助手](https://greasyfork.org/zh-CN/scripts/371551)
11 | ### 功能
12 | 在网易云课堂的课程详情的课程主页添加下载助手按钮,可以进行下载设置,然后批量下载当前课程,在每一个课时上面也添加了下载按钮,可以单独下载当前课时,下载功能由 Aria2 提供,所以下载课程前一定要配置好 Aria2,不然脚本无效,Aria2 的配置请看这篇文章:[如何配置 Aria2 来进行文件下载](https://mofiter.com/2018-08-19/%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE-Aria2-%E6%9D%A5%E8%BF%9B%E8%A1%8C%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD.html) 。
13 | ### 注意
14 | - 只有免费课程支持下载,收费课程下载之后无法播放,所以在收费课程的页面没有添加下载按钮;
15 | - 请注意你所在的课程目录页面地址,形式为 study.163.com/course/courseMain.htm?courseId=xxx 这样的才会有下载按钮(如果是 study.163.com/course/introduction.htm?courseId=xxx 这种形式,你需要先参加这门课程);
16 | - 第一次使用脚本时,请先在下载助手按钮中选择设置,然后填写你要下载视频的保存地址,Mac 上的地址形式为:Users/xxx/Downloads/study163,Windows 上的地址形式为:D:\Downloads\study163,当然具体保存在哪个文件夹由你自己决定;
17 | - 在使用批量下载前,建议先选择任何一个课时,点击课时上面的下载测试一下效果,如果可以成功下载再去使用批量下载;
18 | - Chrome 浏览器和 Firefox 浏览器已测试可正常使用,其他浏览器看缘分;
19 | - 此脚本的目的只为方便个人学习,请勿用于商业用途,否则后果自负。
20 | ## [Udemy 下载助手](https://greasyfork.org/zh-CN/scripts/371552)
21 | ### 功能
22 | 在 Udemy 的课程内容页面添加下载助手按钮,可以进行下载设置,然后批量下载当前课程,在每一个课时上面也添加了下载按钮,可以单独下载当前课时,下载功能由 Aria2 提供,所以下载课程前一定要配置好 Aria2,不然脚本无效,Aria2 的配置请看这篇文章:[如何配置 Aria2 来进行文件下载](https://mofiter.com/2018-08-19/%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE-Aria2-%E6%9D%A5%E8%BF%9B%E8%A1%8C%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD.html) 。
23 | ### 注意
24 | - 请注意你所在的课程内容页面地址,形式为 www.udemy.com/xxx/learn/v4/content 这样的才会有下载按钮;
25 | - 第一次使用脚本时,请先在下载助手按钮中选择设置,然后填写你要下载视频的保存地址,Mac 上的地址形式为:Users/xxx/Downloads/study163,Windows 上的地址形式为:D:\Downloads\study163,当然具体保存在哪个文件夹由你自己决定;
26 | - 在使用批量下载前,建议先选择任何一个课时,点击课时上面的下载测试一下效果,如果可以成功下载再去使用批量下载;
27 | - Chrome 浏览器、Firefox 浏览器、Safari 浏览器都已测试可正常使用,至于其他浏览器看缘分;
28 | - 此脚本的目的只为方便个人学习,请勿用于商业用途,否则后果自负。
29 | ## [微信公众号文章内视频下载](https://greasyfork.org/zh-CN/scripts/372880)
30 | ### 功能
31 | 在微信公众号的文章页面添加视频下载按钮(如果当前文章中包含视频的话),方便将视频下载到本地使用。下载功能由 Aria2 提供,所以下载视频前一定要配置好 Aria2,不然脚本无效,Aria2 的配置请看这篇文章:[如何配置 Aria2 来进行文件下载](https://mofiter.com/2018-08-19/%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE-Aria2-%E6%9D%A5%E8%BF%9B%E8%A1%8C%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD.html) 。
32 | ### 注意
33 | - 请先安装 Adblock 等去除广告的浏览器插件([Google 应用商店地址](https://chrome.google.com/webstore/detail/adblock/gighmmpiobklfepjocnamgkkbiglidom) 或 [非 Google 应用商店地址](https://www.chromedownloads.net/extensions/456.html)),因为有些公众号文章内的视频是有广告的,会影响脚本的正常使用;
34 | - 有一小部分视频是无法下载的,就没有提供下载按钮,可以多刷新页面试一下,在没有广告的前提下,如果一直没有下载按钮出现,那应该这个视频就无法下载;
35 | - 视频可能会自动播放,因为只有点击了视频播放,才能拿到视频地址,我已做点击播放后的暂停处理,但可能有时候效果不太好;
36 | - 时间特别长的视频可能下载不全,因为长视频是在播放的过程中分段加载的,每一段的地址都不同,没法一次性拿到全部视频的地址,但短视频应该没影响;
37 | - 第一次使用脚本时,请先点击下载地址设置,然后填写你要下载视频的保存地址,Mac 上的地址形式为:Users/xxx/Downloads,Windows 上的地址形式为:D:\Downloads,当然具体保存在哪个文件夹由你自己决定;
38 | - Chrome 浏览器和 Firefox 浏览器应该可以正常使用,其他浏览器看缘分;
39 | - 视频的版权归原所有者,此脚本的目的只为方便个人学习,请勿用于商业用途,否则后果自负。
40 | ## 如果有任何想法可以加群交流哦~
41 | >
42 |
--------------------------------------------------------------------------------
/capture/Udemy 效果图.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mofiter/UserScripts/fb3ee231ad67b24c7d60a1de5fbea14fc9e185f3/capture/Udemy 效果图.png
--------------------------------------------------------------------------------
/capture/中国大学 MOOC 效果图.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mofiter/UserScripts/fb3ee231ad67b24c7d60a1de5fbea14fc9e185f3/capture/中国大学 MOOC 效果图.png
--------------------------------------------------------------------------------
/capture/微信公众号文章内视频下载效果图.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mofiter/UserScripts/fb3ee231ad67b24c7d60a1de5fbea14fc9e185f3/capture/微信公众号文章内视频下载效果图.png
--------------------------------------------------------------------------------
/capture/网易云课堂效果图.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mofiter/UserScripts/fb3ee231ad67b24c7d60a1de5fbea14fc9e185f3/capture/网易云课堂效果图.png
--------------------------------------------------------------------------------
/javascript files/Udemy下载助手(需配合Aria2使用).user.js:
--------------------------------------------------------------------------------
1 | // ==UserScript==
2 | // @name Udemy下载助手(需配合Aria2使用)
3 | // @name:en Udemy Downloader
4 | // @namespace http://mofiter.com/
5 | // @version 0.3
6 | // @description 在 Udemy 上课程的课程内容页面添加下载按钮(可批量下载和单个视频下载),方便将视频下载到本地学习
7 | // @description:en add button on udemy's course content to download videos
8 | // @author mofiter
9 | // @require https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js
10 | // @match *://www.udemy.com/*/learn/v4/content
11 | // @grant GM_getValue
12 | // @grant GM_setValue
13 | // @grant GM_xmlhttpRequest
14 | // @grant GM_openInTab
15 | // ==/UserScript==
16 |
17 | (function() {
18 | 'use strict';
19 | var $ = $ || window.$;
20 | var log_count = 1;
21 | var hasOpenAriac2Tab = false;
22 | var video_quality = "720"; //视频清晰度
23 | var aria2_url = "http://127.0.0.1:6800/jsonrpc"; //Aria2 地址
24 | var course_save_path = '/Users/mofiter/Downloads/study163'; //课程保存路径
25 | var mycourses_url = "https://www.udemy.com/api-2.0/users/me/subscribed-courses"; //已购所有课程的地址
26 | var curricuclum_url = "https://www.udemy.com/api-2.0/courses/{0}/cached-subscriber-curriculum-items/?page_size=9999"; //当前课程的地址
27 | var lesson_url = "https://www.udemy.com/api-2.0/assets/{0}?fields[asset]=@min,status,delayed_asset_message,time_estimation,stream_urls,captions,body&fields[caption]=@default,is_translation"; //一节课的地址
28 | var cookies = document.cookie;
29 | var match_cookie = cookies.match(/access_token=(\w+?);/);
30 | var authorization = "Bearer " + match_cookie[1];
31 | var mycourses_info;
32 | var current_course_url = location.pathname;
33 | var current_course_info = {'course_id':{},'course_name':{},'chapter_info':[]};
34 | var isExpand = false; //下载助手是否展开
35 |
36 | //自定义 log 函数
37 | function mylog(param1,param2){
38 | param1 = param1 ? param1 : "";
39 | param2 = param2 ? param2 : "";
40 | console.log("#" + log_count++ + "-UdemyDownloader-log:",param1,param2);
41 | }
42 |
43 | //javascript 字符串使用占位符轻松拼接
44 | String.prototype.format = function() {
45 | if(arguments.length == 0) return this;
46 | var param = arguments[0];
47 | var s = this;
48 | if(typeof(param) == 'object') {
49 | for(var key in param)
50 | {s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);}
51 | return s;
52 | } else {
53 | for(var i = 0; i < arguments.length; i++)
54 | {s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);}
55 | return s;
56 | }
57 | }
58 |
59 | setTimeout(function(){
60 | getCourseInfo();
61 | loadSetting();
62 | addDownloadAssistant();
63 | addDownloadButton();
64 | mylog("Udemy 下载助手加载完成 ~");
65 | },5000); //页面加载完成后延时5秒执行
66 |
67 | //获取视频信息
68 | function getCourseInfo(){
69 | $.ajax({
70 | url:mycourses_url,
71 | async:false,
72 | method:'GET',
73 | beforeSend:function(xhr){
74 | xhr.setRequestHeader('authorization',authorization);
75 | },
76 | success:function(response){
77 | mycourses_info = response.results;
78 | }
79 | });
80 | $.each(mycourses_info,function(index,element){
81 | if (current_course_url.indexOf(element.url) > -1) {
82 | current_course_info.course_id = element.id;
83 | current_course_info.course_name = element.title.replace(/:|\?|\*|"|<|>|\|/g," ");
84 | return false;
85 | }
86 | });
87 | curricuclum_url = curricuclum_url.format(current_course_info.course_id);
88 | $.ajax({
89 | url:curricuclum_url,
90 | async:false,
91 | method:'GET',
92 | beforeSend:function(xhr){
93 | xhr.setRequestHeader('authorization',authorization);
94 | },
95 | success:function(response){
96 | var list = response.results;
97 | list.push({'_class':'chapter','id':'useless','title':''});
98 | var chapter = {'chapter_id':'','chapter_name':{},'lecture_info':[]};
99 | var lecture_sn = 0;
100 | list.forEach(function(item,index){
101 | if(item._class == 'chapter'){
102 | if(chapter.chapter_id != ''){
103 | var data = JSON.parse(JSON.stringify(chapter));
104 | current_course_info.chapter_info.push(data);
105 | }
106 | if(item.id != 'useless'){
107 | chapter.chapter_id = item.id;
108 | chapter.chapter_name = item.title.replace(/:|\?|\*|"|<|>|\|/g," ");
109 | chapter.lecture_info = [];
110 | }
111 | }else if(item._class == 'lecture'){
112 | var lecture = {'lecture_sn':(++lecture_sn),'lecture_id':item.id,'lecture_name':item.title.replace(/:|\?|\*|"|<|>|\|/g," "),'lecture_asset':{'asset_id':item.asset.id,'asset_name':item.asset.title.replace(/:|\?|\*|"|<|>|\|/g," "),'asset_type':item.asset.asset_type}};
113 | chapter.lecture_info.push(lecture);
114 | }
115 | });
116 | mylog(current_course_info);
117 | }
118 | });
119 | }
120 |
121 | //添加下载助手按钮
122 | function addDownloadAssistant(){
123 | var download_assistant = $('
');
124 | var download_assistant_a = $('下载助手');
125 | var assistant_content = $('');
126 | var batch_download = $('批量下载');
127 | var assistant_setting = $('设置');
128 | assistant_content.append(batch_download).append(assistant_setting);
129 | download_assistant.append(download_assistant_a).append(assistant_content);
130 | $('.nav-tabs').append(download_assistant);
131 | download_assistant.click(function(event){
132 | if(isExpand){
133 | isExpand = false;
134 | }else{
135 | isExpand = true;
136 | }
137 | assistant_content.toggle();
138 | event.stopPropagation();
139 | });
140 | $(document).click(function(){
141 | if(isExpand){
142 | assistant_content.hide();
143 | isExpand = false;
144 | }
145 | });
146 | batch_download.click(function(){
147 | loadSetting();
148 | if(course_save_path==""){
149 | alert("请到下载助手的设置里面填写文件保存位置");
150 | }else if(aria2_url==""){
151 | alert("请到下载助手的设置里面填写 Aria2 地址");
152 | }else{
153 | batchDownload();
154 | }
155 | });
156 | assistant_setting.click(function(){
157 | showSetting();
158 | });
159 | }
160 |
161 | //加载个人设置
162 | function loadSetting(){
163 | video_quality = GM_getValue('video_quality', '720');
164 | aria2_url = GM_getValue('aria2_url','http://127.0.0.1:6800/jsonrpc');
165 | course_save_path = GM_getValue('course_save_path','');
166 | }
167 |
168 | //打开设置
169 | function showSetting(){
170 | if(document.querySelector('#dl-setting') == null){
171 | var container = document.createElement("div");
172 | container.id = "dl-setting";
173 | container.style = "position:fixed;z-index:999999;top:10%;right:2%;width:270px;background-color:#eee;padding:5px 10px;font-size:14px;border:1px solid;";
174 | container.innerHTML =
175 | "" +
176 | "
下载助手设置
" +
177 | "
\n" +
188 | "
\n" +
189 | "
\n" +
190 | "
";
191 | document.body.appendChild(container);
192 | }else{
193 | loadSetting();
194 | if(video_quality=="720"){
195 | $('#video-quality-1').prop('checked',true);
196 | }else if(video_quality=="480"){
197 | $('#video-quality-2').prop('checked',true);
198 | }else if(video_quality=="360"){
199 | $('#video-quality-3').prop('checked',true);
200 | }else if(video_quality=="144"){
201 | $('#video-quality-4').prop('checked',true);
202 | }
203 | $('#aria2_url').value = aria2_url;
204 | $('#save_path').value = course_save_path;
205 | $('#dl-setting').show();
206 | }
207 | $('#save_button').click(function(){
208 | GM_setValue('video_quality',$('input[name="video-quality"]:checked').val());
209 | GM_setValue('aria2_url',$('input[name="aria2_url"]').val());
210 | GM_setValue('course_save_path',$('input[name="save_path"]').val());
211 | $('#dl-setting').hide();
212 | });
213 | $('#cancel_button').click(function(){
214 | $('#dl-setting').hide();
215 | });
216 | }
217 |
218 | //批量下载
219 | function batchDownload(){
220 | current_course_info.chapter_info.forEach(function(chapter,index){
221 | chapter.lecture_info.forEach(function(lecture){
222 | if(lecture.lecture_asset.asset_type == 'Video'){
223 | var file_name = lecture.lecture_sn + '.' + lecture.lecture_name;
224 | var save_path = course_save_path.replace(/\\/g,'\/') + '/' + current_course_info.course_name + '/' + (index + 1) + '.' + current_course_info.chapter_info[index].chapter_name;
225 | var real_lesson_url = lesson_url.format(lecture.lecture_asset.asset_id);
226 | getVideoUrl(real_lesson_url,file_name + '.mp4',save_path);
227 | }
228 | });
229 | });
230 | }
231 |
232 | //添加下载按钮
233 | function addDownloadButton(){
234 | var li_items = $('div.section--section--CIk7q li.curriculum-item--curriculum-item--iJwX5');
235 | $.each(li_items,function(index,element){
236 | if (element.firstChild.childNodes[2].innerHTML == '') {
237 | element.firstChild.style.paddingRight = "70px";
238 | return true;
239 | }
240 | var download_button = document.createElement('span');
241 | download_button.className = 'download-button';
242 | download_button.innerHTML = '下载';
243 | download_button.style.order = 5;
244 | download_button.style.marginLeft = '20px';
245 | (element.firstChild).append(download_button);
246 | });
247 | $('.download-button').each(function(){
248 | $(this).click(function(event){
249 | loadSetting();
250 | if(course_save_path==""){
251 | alert("请到下载助手的设置里面填写文件保存位置");
252 | }else if(aria2_url==""){
253 | alert("请到下载助手的设置里面填写 Aria2 地址");
254 | }else{
255 | var chapter_index = $(this).parents('.section--section--CIk7q').index();
256 | var lecture_index = $(this).parents('.curriculum-item--curriculum-item--iJwX5').index();
257 | var lecture = current_course_info.chapter_info[chapter_index].lecture_info[lecture_index];
258 | mylog("选择的课为【lecture_name:" + lecture.lecture_name + ",lecture_id:" + lecture.lecture_id + '】');
259 | var file_name = lecture.lecture_sn + '.' + lecture.lecture_name;
260 | var save_path = course_save_path.replace(/\\/g,'\/') + '/' + current_course_info.course_name + '/' + (chapter_index + 1) + '.' + current_course_info.chapter_info[chapter_index].chapter_name;
261 | var real_lesson_url = lesson_url.format(lecture.lecture_asset.asset_id);
262 | var url = 'https://www.udemy.com/vuejs-app/learn/v4/t/lecture/{0}?start=0'.format(lecture.lecture_id);
263 | getVideoUrl(real_lesson_url,file_name + '.mp4',save_path);
264 | }
265 | event.stopPropagation();
266 | });
267 | });
268 | }
269 |
270 | //获取视频下载地址
271 | function getVideoUrl(url,file_name,save_path){
272 | $.ajax({
273 | url:url,
274 | async:true,
275 | method:'GET',
276 | beforeSend:function(xhr){
277 | xhr.setRequestHeader('authorization',authorization);
278 | },
279 | success:function(response){
280 | var video_urls = response.stream_urls.Video;
281 | video_urls.forEach(function(value){
282 | if (value.label == video_quality){
283 | //mylog(value.file);
284 | sendDownloadTaskToAria2(value.file,file_name,save_path);
285 | }
286 | });
287 | }
288 | });
289 | }
290 |
291 | // 将下载链接发送到 Aria2 下载
292 | function sendDownloadTaskToAria2(download_url,file_name,save_path){
293 | var json_rpc = {
294 | id:'',
295 | jsonrpc:'2.0',
296 | method:'aria2.addUri',
297 | params:[
298 | [download_url],
299 | {
300 | dir:save_path,
301 | out:file_name
302 | }
303 | ]
304 | };
305 | GM_xmlhttpRequest({
306 | url:aria2_url,
307 | method:'POST',
308 | data:JSON.stringify(json_rpc),
309 | onerror:function(response){
310 | mylog(response);
311 | },
312 | onload:function(response){
313 | mylog(response);
314 | if (!hasOpenAriac2Tab){
315 | GM_openInTab('http://aria2c.com/',{active:true});
316 | hasOpenAriac2Tab = true;
317 | }
318 | }
319 | });
320 | }
321 |
322 | })();
--------------------------------------------------------------------------------
/javascript files/中国大学 MOOC 下载助手(需配合Aria2使用).user.js:
--------------------------------------------------------------------------------
1 | // ==UserScript==
2 | // @name 中国大学 MOOC 下载助手(需配合Aria2使用)
3 | // @name:en Icourse163 Downloader
4 | // @namespace http://mofiter.com/
5 | // @version 0.3
6 | // @description 在中国大学 MOOC 的课程学习页面添加批量下载按钮,方便将视频下载到本地学习
7 | // @description:en add download button on icourse163.org to download videos
8 | // @author mofiter
9 | // @require https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js
10 | // @match *://www.icourse163.org/learn/*
11 | // @grant unsafeWindow
12 | // @grant GM_getValue
13 | // @grant GM_setValue
14 | // @grant GM_xmlhttpRequest
15 | // @grant GM_openInTab
16 | // ==/UserScript==
17 |
18 | (function() {
19 | 'use strict';
20 | var $ = $ || window.$;
21 | var log_count = 1;
22 | var hasOpenAriac2Tab = false;
23 | var video_quality = 2; //视频清晰度
24 | var video_format = 'mp4'; //视频格式
25 | var aria2_url = "http://127.0.0.1:6800/jsonrpc"; //Aria2 地址
26 | var course_save_path = '/Users/mofiter/Downloads/icourse163'; //课程保存路径
27 | var video_save_path; //每个视频保存路径
28 | var video_download_url = ""; //视频下载地址
29 | var course_info = {'course_id': {},'course_name': {},'chapter_info': []}; //课程信息
30 | var cookies = document.cookie;
31 | var sessionId = cookies.match(/NTESSTUDYSI=(\w+)/)[1];
32 |
33 | //自定义 log 函数
34 | function mylog(param1,param2){
35 | param1 = param1 ? param1 : "";
36 | param2 = param2 ? param2 : "";
37 | console.log("#" + log_count++ + "-Icourse163Downloader-log:",param1,param2);
38 | }
39 |
40 | setTimeout(function(){
41 | getCourseIdAndName();
42 | getCourseContentInfo();
43 | loadSetting();
44 | addDownloadAssistant();
45 | mylog("中国大学 MOOC 下载助手加载完成~");
46 | },2000); //页面加载完成后延时2秒执行
47 |
48 | //获取课程名称
49 | function getCourseIdAndName(){
50 | var courseCardDto = unsafeWindow.courseCardDto;
51 | course_info.course_name = courseCardDto.name.replace(/\/|:|\?|\*|"|<|>|\|/g," ");
52 | if(location.href.match(/tid=(\d+)/)[1]){
53 | course_info.course_id = location.href.match(/tid=(\d+)/)[1];
54 | }else{
55 | course_info.course_id = courseCardDto.currentTermId;
56 | }
57 | }
58 |
59 | //添加批量下载和下载设置按钮
60 | function addDownloadAssistant(){
61 | var batch_download_li = $("");
62 | var batch_download = $("批量下载");
63 | batch_download_li.append(batch_download);
64 | var assistant_setting_li = $("");
65 | var assistant_setting = $("下载设置");
66 | assistant_setting_li.append(assistant_setting);
67 | $('#j-courseTabList').append(batch_download_li).append(assistant_setting_li);
68 | batch_download_li.click(function(){
69 | loadSetting();
70 | if(course_save_path==""){
71 | alert("请点击下载设置去填写文件保存位置");
72 | }else if(aria2_url==""){
73 | alert("请点击下载设置去填写 Aria2 地址");
74 | }else{
75 | batchDownload();
76 | }
77 | });
78 | assistant_setting_li.click(function(){
79 | showSetting();
80 | });
81 | }
82 |
83 | //加载个人设置
84 | function loadSetting(){
85 | video_quality = GM_getValue('video_quality', 2);
86 | video_format = GM_getValue('video_format','mp4');
87 | aria2_url = GM_getValue('aria2_url','http://127.0.0.1:6800/jsonrpc');
88 | course_save_path = GM_getValue('course_save_path','');
89 | }
90 |
91 | //打开设置
92 | function showSetting(){
93 | if(document.querySelector('#dl-setting') == null){
94 | var container = document.createElement("div");
95 | container.id = "dl-setting";
96 | container.style = "position:fixed;z-index:999999;bottom:10%;right:40px;width:220px;height:auto;background-color:#f8f8f8;padding:5px 10px;font-size:14px;border:1px solid #ccc;";
97 | container.innerHTML =
98 | "";
113 | document.body.appendChild(container);
114 | }else{
115 | loadSetting();
116 | if(video_quality==2){
117 | $('#video-quality-2').prop('checked',true);
118 | }else{
119 | $('#video-quality-1').prop('checked',true);
120 | }
121 | if(video_format=='mp4'){
122 | $('#video-format-mp4').prop('checked',true);
123 | }else{
124 | $('#video-format-flv').prop('checked',true);
125 | }
126 | $('#aria2_url').value = aria2_url;
127 | $('#save_path').value = course_save_path;
128 | $('#dl-setting').show();
129 | }
130 | $('#save_button').click(function(){
131 | GM_setValue('video_quality',$('input[name="video-quality"]:checked').val());
132 | GM_setValue('video_format',$('input[name="video-format"]:checked').val());
133 | GM_setValue('aria2_url',$('input[name="aria2_url"]').val());
134 | GM_setValue('course_save_path',$('input[name="save_path"]').val());
135 | $('#dl-setting').hide();
136 | });
137 | $('#cancel_button').click(function(){
138 | $('#dl-setting').hide();
139 | });
140 | }
141 |
142 | //获取课程信息
143 | function getCourseContentInfo(){
144 | var timestamp = new Date().getTime();
145 | var params = {
146 | "callCount":"1",
147 | "scriptSessionId":"${scriptSessionId}190",
148 | "httpSessionId":sessionId,
149 | "c0-scriptName":"CourseBean",
150 | "c0-methodName":"getLastLearnedMocTermDto",
151 | "c0-id":"0",
152 | "c0-param0":"number:" + course_info.course_id,
153 | "batchId":timestamp
154 | };
155 | $.ajax({
156 | url:'https://www.icourse163.org/dwr/call/plaincall/CourseBean.getLastLearnedMocTermDto.dwr',
157 | method:'POST',
158 | async: true,
159 | data: params,
160 | success: function (response){
161 | var chapter1 = response.match(/chapters=(.*?);/)[1]; //保存全部章节的变量
162 | var reg1 = new RegExp(chapter1 + '\\[\\d+]=(.*?);','g');
163 | var chapter2 = response.match(reg1); //保存各章节的变量
164 | chapter2.forEach(function(value){
165 | var chapter3 = value.match(/=(.*?);/)[1]; //保存各章节的变量名字
166 | var reg2 = new RegExp(chapter3 + '.id=(.*?);' + chapter3 + '.lessons=(.*?);' + chapter3 + '.name="(.*?)";');
167 | var chapter4 = response.match(reg2); //保存章节 id,lessons,name 的变量
168 | var reg3 = new RegExp(chapter4[2] + '\\[\\d+]=(.*?);','g');
169 | var lessons = response.match(reg3); //保存各小节的变量
170 | var chapter = {'chapter_id':chapter4[1],'chapter_name':unescape(chapter4[3].replace(/\\u/gi, '%u').replace(/\/|:|\?|\*|"|<|>|\|/g," ")),'lesson_info':[]};
171 | lessons.forEach(function(value){
172 | var lesson1 = value.match(/=(.*?);/)[1]; //保存各小节的变量名字
173 | var reg4 = new RegExp(lesson1 + '.chapterId=(.*?);.*?' + lesson1 + '.id=(.*?);.*?' + lesson1 + '.name="(.*?)";.*?' + lesson1 + '.units=(.*?);');
174 | var lesson2 = response.match(reg4);
175 | var reg5 = new RegExp(lesson2[4] + '\\[\\d+]=(.*?);','g');
176 | var sections = response.match(reg5);
177 | var lesson = {'chapter_id':lesson2[1],'lesson_id':lesson2[2],'lesson_name':unescape(lesson2[3].replace(/\\u/gi, '%u').replace(/\/|:|\?|\*|"|<|>|\|/g," ")),'section_info':[]};
178 | sections.forEach(function(value){
179 | var section1 = value.match(/=(.*?);/)[1];
180 | var reg6 = new RegExp(section1 + '.chapterId=(.*?);.*?' + section1 + '.contentId=(.*?);.*?' + section1 + '.contentType=(.*?);.*?' + section1 + '.id=(.*?);.*?' + section1 + '.lessonId=(.*?);.*?' + section1 + '.name="(.*?)";.*?');
181 | var section2 = response.match(reg6);
182 | var section = {'chapter_id':section2[1],'lesson_id':section2[5],'content_id':section2[2],'section_id':section2[4],'section_name':unescape(section2[6].replace(/\\u/gi, '%u').replace(/\/|:|\?|\*|"|<|>|\|/g," ")),'content_type':section2[3]};
183 | lesson.section_info.push(section);
184 | });
185 | chapter.lesson_info.push(lesson);
186 | });
187 | course_info.chapter_info.push(chapter);
188 | });
189 | mylog(course_info);
190 | }
191 | });
192 | }
193 |
194 | //批量下载
195 | function batchDownload(){
196 | course_info.chapter_info.forEach(function(chapter,index_chapter){
197 | chapter.lesson_info.forEach(function(lesson,index_lesson){
198 | lesson.section_info.forEach(function(section,index_section){
199 | var file_name = '第' + (index_section + 1) + '部分_' + section.section_name;
200 | var save_path = course_save_path.replace(/\\/g,'\/') + '/' + course_info.course_name + '/第' + (index_chapter + 1) + '章_' + chapter.chapter_name + '/第' + (index_lesson + 1) + '节_' + lesson.lesson_name;
201 | if(section.content_type == '1'){
202 | getVideoToken(section.content_id,file_name,save_path);
203 | } else if(section.content_type == '3'){
204 | getCourseContentUrl(section.content_id,section.section_id,file_name,save_path);
205 | }
206 | });
207 | });
208 | });
209 | }
210 |
211 | //获取文档下载地址
212 | function getCourseContentUrl(content_id,section_id,file_name,save_path){
213 | var timestamp = new Date().getTime();
214 | var params = {
215 | "callCount":"1",
216 | "scriptSessionId":"${scriptSessionId}190",
217 | "httpSessionId":sessionId,
218 | "c0-scriptName":"CourseBean",
219 | "c0-methodName":"getLessonUnitLearnVo",
220 | "c0-id":"0",
221 | "c0-param0":"number:" + content_id,
222 | "c0-param1":"number:3",
223 | "c0-param2":"number:0",
224 | "c0-param3":"number:" + section_id,
225 | "batchId":timestamp
226 | };
227 | $.ajax({
228 | url:'https://www.icourse163.org/dwr/call/plaincall/CourseBean.getLessonUnitLearnVo.dwr',
229 | method:'POST',
230 | async: true,
231 | data: params,
232 | success: function (response){
233 | var pdfUrl = response.match(/textOrigUrl:"(.*?)",/)[1];
234 | //mylog(pdfUrl);
235 | sendDownloadTaskToAria2(pdfUrl,file_name + ".pdf",save_path);
236 | }
237 | });
238 | }
239 |
240 | //获取视频信息
241 | function getVideoToken(content_id,file_name,save_path){
242 | var params = {
243 | 'videoId':content_id,
244 | 'targetId':course_info.course_id,
245 | 'targetType':'0'
246 | };
247 | $.ajax({
248 | url:'https://www.icourse163.org/web/j/resourceRpcBean.getVideoToken.rpc?csrfKey=' + sessionId,
249 | method:'POST',
250 | async: true,
251 | data: params,
252 | success: function (response){
253 | var signature = response.result.signature;
254 | getVideoUrl(content_id,signature,file_name,save_path);
255 | }
256 | });
257 | }
258 |
259 | //获取视频下载地址
260 | function getVideoUrl(content_id,signature,file_name,save_path){
261 | var params = {
262 | 'videoId':content_id,
263 | 'signature':signature,
264 | 'clientType':'1'
265 | };
266 | $.ajax({
267 | url:'https://vod.study.163.com/eds/api/v1/vod/video',
268 | method:'POST',
269 | async: true,
270 | data: params,
271 | success: function (response){
272 | var videoUrls = response.result.videos;
273 | var video_url_list = [];
274 | videoUrls.forEach(function(video){
275 | if(video.format == video_format) {
276 | video_url_list.push({'video_format': video.format,'video_quality': video.quality,'video_url': video.videoUrl});
277 | }
278 | });
279 | if(video_url_list.length != 0){
280 | if(video_quality=="2"){
281 | video_download_url = video_url_list[video_url_list.length-1].video_url;
282 | }else{
283 | video_download_url = video_url_list[0].video_url;
284 | }
285 | }
286 | if(video_download_url != ""){
287 | //mylog(video_download_url);
288 | sendDownloadTaskToAria2(video_download_url,file_name + '.' + video_format,save_path);
289 | }
290 | }
291 | });
292 | }
293 |
294 | // 将下载链接发送到 Aria2 下载
295 | function sendDownloadTaskToAria2(download_url,file_name,save_path){
296 | var json_rpc = {
297 | id:'',
298 | jsonrpc:'2.0',
299 | method:'aria2.addUri',
300 | params:[
301 | [download_url],
302 | {
303 | dir:save_path,
304 | out:file_name
305 | }
306 | ]
307 | };
308 | GM_xmlhttpRequest({
309 | url:aria2_url,
310 | method:'POST',
311 | data:JSON.stringify(json_rpc),
312 | onerror:function(response){
313 | mylog(response);
314 | },
315 | onload:function(response){
316 | mylog(response);
317 | if (!hasOpenAriac2Tab){
318 | GM_openInTab('http://aria2c.com/',{active:true});
319 | hasOpenAriac2Tab = true;
320 | }
321 | }
322 | });
323 | }
324 |
325 | })();
--------------------------------------------------------------------------------
/javascript files/微信公众号文章内视频下载(需配合Aria2使用).user.js:
--------------------------------------------------------------------------------
1 | // ==UserScript==
2 | // @name 微信公众号文章内视频下载(需配合Aria2使用)
3 | // @name:en WeChatOA Video Downloader
4 | // @namespace http://mofiter.com/
5 | // @version 0.1
6 | // @description 将微信公众号文章内的视频下载到本地
7 | // @description:en download videos from WeChatOA to your disk
8 | // @author mofiter
9 | // @create 2018-10-04
10 | // @lastmodified 2-18-10-05
11 | // @require https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js
12 | // @match http*://mp.weixin.qq.com/s*
13 | // @match http*://v.qq.com/txp/iframe/player.html*
14 | // @grant GM_getValue
15 | // @grant GM_setValue
16 | // @grant GM_xmlhttpRequest
17 | // @grant GM_openInTab
18 | // ==/UserScript==
19 |
20 | (function() {
21 | 'use strict';
22 |
23 | var $ = $ || window.$;
24 | var log_count = 1;
25 | var hasOpenAriac2Tab = false;
26 | var aria2_url = "http://127.0.0.1:6800/jsonrpc"; //Aria2 地址
27 | var video_save_path = '/Users/mofiter/Downloads'; //视频保存路径
28 |
29 | //自定义 log 函数
30 | function mylog(param1,param2){
31 | param1 = param1 ? param1 : "";
32 | param2 = param2 ? param2 : "";
33 | console.log("#" + log_count++ + "-WeChatOAVideoDownloader-log:",param1,param2);
34 | }
35 |
36 | setTimeout(function(){
37 | if (location.href.indexOf("v.qq.com/txp/iframe/player.html") > -1){
38 | var txp_controls = document.getElementsByClassName("txp_controls")[0];
39 | var txp_btn_volume = txp_controls.getElementsByClassName("txp_btn_volume")[0];
40 | txp_btn_volume.setAttribute("data-status","mute"); //静音
41 | var txp_btn_play = txp_controls.getElementsByClassName("txp_btn_play")[0];
42 | txp_btn_play.click();
43 | setTimeout(function(){
44 | txp_btn_play.click();
45 | var video = document.getElementsByTagName("video")[0];
46 | var video_src = video.getAttribute("src");
47 | if (video_src.indexOf("blob") === -1){
48 | window.parent.postMessage(video_src,"*");
49 | }else{
50 | window.parent.postMessage('',"*");
51 | }
52 | },1000);
53 | }
54 | if (location.href.indexOf("mp.weixin.qq.com/s") > -1){
55 | window.addEventListener('message', (e) => {
56 | var video_url = e.data;
57 | if (video_url.indexOf("http") > -1){
58 | var file_name = document.getElementsByTagName("h2")[0].innerText;
59 | var qr_code_pc = document.getElementsByClassName("qr_code_pc")[0];
60 | var downloadButton = document.createElement("p");
61 | downloadButton.innerHTML = "点此下载视频";
62 | downloadButton.style.cursor = "pointer";
63 | qr_code_pc.appendChild(downloadButton);
64 | var downloadSetting = document.createElement("p");
65 | downloadSetting.innerHTML = "下载地址设置";
66 | downloadSetting.style.cursor = "pointer";
67 | qr_code_pc.appendChild(downloadSetting);
68 | downloadButton.addEventListener("click",function(){
69 | loadSetting();
70 | if(video_save_path===""){
71 | alert("请点击下载地址设置填写文件保存位置");
72 | }else if(aria2_url===""){
73 | alert("请点击下载地址设置填写 Aria2 地址");
74 | }else{
75 | sendDownloadTaskToAria2(video_url,file_name + ".mp4",video_save_path);
76 | }
77 | });
78 | downloadSetting.addEventListener("click",function(){
79 | showSetting();
80 | });
81 | }else{
82 | mylog("当前视频无法下载");
83 | }
84 | });
85 | var iframe_document = document.getElementsByTagName("iframe")[0].contentWindow.document;
86 | var js_btn_play = iframe_document.getElementsByClassName("js_btn_play")[0];
87 | js_btn_play.click();
88 | setTimeout(function(){
89 | var js_switch = iframe_document.getElementsByClassName("js_switch")[0];
90 | js_switch.click();
91 | var video = iframe_document.getElementsByTagName("video")[0];
92 | var video_src = video.getAttribute("src");
93 | window.parent.postMessage(video_src,"*");
94 | },1000);
95 | }
96 | },2000); // 页面加载完成后2秒执行
97 |
98 | //加载个人设置
99 | function loadSetting(){
100 | aria2_url = GM_getValue('aria2_url','http://127.0.0.1:6800/jsonrpc');
101 | video_save_path = GM_getValue('video_save_path','');
102 | }
103 |
104 | //打开设置
105 | function showSetting(){
106 | if(document.querySelector('#dl-setting') == null){
107 | var container = document.createElement("div");
108 | container.id = "dl-setting";
109 | container.style = "position:fixed;z-index:999999;top:40px;right:10px;width:auto;height:auto;padding:5px 10px;font-size:14px;border:1px solid #d9dadc;";
110 | container.innerHTML =
111 | "";
122 | document.body.appendChild(container);
123 | }else{
124 | loadSetting();
125 | $('#aria2_url').value = aria2_url;
126 | $('#save_path').value = video_save_path;
127 | $('#dl-setting').show();
128 | }
129 | $('#save_button').click(function(){
130 | GM_setValue('aria2_url',$('input[name="aria2_url"]').val());
131 | GM_setValue('video_save_path',$('input[name="save_path"]').val());
132 | $('#dl-setting').hide();
133 | });
134 | $('#cancel_button').click(function(){
135 | $('#dl-setting').hide();
136 | });
137 | }
138 |
139 | function sendDownloadTaskToAria2(download_url,file_name,save_path){
140 | var json_rpc = {
141 | id:'',
142 | jsonrpc:'2.0',
143 | method:'aria2.addUri',
144 | params:[
145 | [download_url],
146 | {
147 | dir:save_path,
148 | out:file_name
149 | }
150 | ]
151 | };
152 | GM_xmlhttpRequest({
153 | url:aria2_url,
154 | method:'POST',
155 | data:JSON.stringify(json_rpc),
156 | onerror:function(response){
157 | mylog(response);
158 | },
159 | onload:function(response){
160 | mylog(response);
161 | if (!hasOpenAriac2Tab){
162 | GM_openInTab('http://aria2c.com/',{active:true});
163 | hasOpenAriac2Tab = true;
164 | }
165 | }
166 | });
167 | }
168 | })();
--------------------------------------------------------------------------------
/javascript files/网易云课堂下载助手(需配合Aria2使用).user.js:
--------------------------------------------------------------------------------
1 | // ==UserScript==
2 | // @name 网易云课堂下载助手(需配合Aria2使用)
3 | // @name:en Study163 Downloader
4 | // @namespace http://mofiter.com/
5 | // @version 0.6
6 | // @description 在网易云课堂的课程目录页面添加下载助手按钮(可批量下载和单个视频下载),方便将视频下载到本地学习
7 | // @description:en add download button on study.163.com to download videos
8 | // @author mofiter
9 | // @require https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js
10 | // @match *://study.163.com/course/courseMain.htm?courseId=*
11 | // @grant unsafeWindow
12 | // @grant GM_getValue
13 | // @grant GM_setValue
14 | // @grant GM_xmlhttpRequest
15 | // @grant GM_openInTab
16 | // ==/UserScript==
17 |
18 | (function() {
19 | 'use strict';
20 | var $ = window.$;
21 | var log_count = 1;
22 | var hasOpenAriac2Tab = false;
23 | var video_quality = 2; //视频清晰度
24 | var video_format = 'mp4'; //视频格式
25 | var video_file_name; //视频文件名称
26 | var aria2_url = "http://127.0.0.1:6800/jsonrpc"; //Aria2 地址
27 | var course_save_path = '/Users/mofiter/Downloads/study163'; //课程保存路径
28 | var video_save_path; //每个视频保存路径
29 | var video_download_url = ""; //视频下载地址
30 | var course_info = {'course_id': {},'course_name': {},'chapter_info': [],'course_price': {},'course_duration': {}}; //课程信息
31 | var cookies = document.cookie;
32 | var match_cookie = cookies.match(/NTESSTUDYSI=(\w+)/)[1];
33 |
34 | //自定义 log 函数
35 | function mylog(param1,param2){
36 | param1 = param1 ? param1 : "";
37 | param2 = param2 ? param2 : "";
38 | console.log("#" + log_count++ + "-Study163Downloader-log:",param1,param2);
39 | }
40 |
41 | setTimeout(function(){
42 | if(!getCourseInfo()){
43 | return;
44 | }
45 | loadSetting();
46 | addDownloadAssistant();
47 | addDownloadButton();
48 | mylog("网易云课堂下载助手加载完成~");
49 | },5000); //页面加载完成后延时5秒执行
50 |
51 | //加载个人设置
52 | function loadSetting(){
53 | video_quality = GM_getValue('video_quality', 2);
54 | video_format = GM_getValue('video_format','mp4');
55 | aria2_url = GM_getValue('aria2_url','http://127.0.0.1:6800/jsonrpc');
56 | course_save_path = GM_getValue('course_save_path','');
57 | }
58 |
59 | //添加下载助手按钮
60 | function addDownloadAssistant(){
61 | $(".u-navsearchUI").css("width","224px");
62 | var download_assistant_div = $("");
63 | var download_assistant = $("下载助手");
64 | var assistant_div = $("");
65 | var batch_download = $("批量下载");
66 | var assistant_setting = $("设置");
67 | var recommendation = $("
");
68 | assistant_div.append(batch_download).append(assistant_setting).append(recommendation);
69 | download_assistant_div.append(download_assistant).append(assistant_div);
70 | $('.m-nav').append(download_assistant_div);
71 | download_assistant_div.mouseover(function(){
72 | assistant_div.show();
73 | });
74 | download_assistant_div.mouseout(function(){
75 | assistant_div.hide();
76 | });
77 | batch_download.click(function(){
78 | assistant_div.hide();
79 | loadSetting();
80 | if(course_save_path==""){
81 | alert("请到下载助手的设置里面填写文件保存位置");
82 | }else if(aria2_url==""){
83 | alert("请到下载助手的设置里面填写 Aria2 地址");
84 | }else{
85 | batchDownload();
86 | }
87 | });
88 | assistant_setting.click(function(){
89 | assistant_div.hide();
90 | showSetting();
91 | });
92 | }
93 |
94 | //打开设置
95 | function showSetting(){
96 | if(document.querySelector('#dl-setting') == null){
97 | var container = document.createElement("div");
98 | container.id = "dl-setting";
99 | container.style = "position:fixed;z-index:999999;top:30%;left:45%;width:auto;height:auto;background-color:#eee;padding:5px 10px;font-size:14px;border:1px solid;";
100 | container.innerHTML =
101 | "";
116 | document.body.appendChild(container);
117 | }else{
118 | loadSetting();
119 | if(video_quality==2){
120 | $('#video-quality-2').prop('checked',true);
121 | }else{
122 | $('#video-quality-1').prop('checked',true);
123 | }
124 | if(video_format=='mp4'){
125 | $('#video-format-mp4').prop('checked',true);
126 | }else{
127 | $('#video-format-flv').prop('checked',true);
128 | }
129 | $('#aria2_url').value = aria2_url;
130 | $('#save_path').value = course_save_path;
131 | $('#dl-setting').show();
132 | }
133 | $('#save_button').click(function(){
134 | GM_setValue('video_quality',$('input[name="video-quality"]:checked').val());
135 | GM_setValue('video_format',$('input[name="video-format"]:checked').val());
136 | GM_setValue('aria2_url',$('input[name="aria2_url"]').val());
137 | GM_setValue('course_save_path',$('input[name="save_path"]').val());
138 | $('#dl-setting').hide();
139 | });
140 | $('#cancel_button').click(function(){
141 | $('#dl-setting').hide();
142 | });
143 | }
144 |
145 | //获取课程信息
146 | function getCourseInfo(){
147 | var courseVo = unsafeWindow.courseVo;
148 | course_info.course_id = courseVo.id; //课程 id
149 | course_info.course_name = courseVo.name.replace(/:|\?|\*|"|<|>|\|/g," "); //课程名称
150 | course_info.course_price = courseVo.price; //课程价格
151 | var chapter = courseVo.chapterDtos; //课程章节
152 | var allSeconds = 0; //课程总时长,单位为秒
153 | chapter.forEach(function(val,index){
154 | var chapter = {'chapter_id': val.id,'chapter_name': val.name.replace(/:|\?|\*|"|<|>|\|/g," "),'lesson_info': []}; //章节信息
155 | var lessonDtos = val.lessonDtos;
156 | lessonDtos.forEach(function(val,index){
157 | var lesson = {'keshi':val.ksstr,'lesson_id':val.id,'lesson_name':val.lessonName.replace(/:|\?|\*|"|<|>|\|/g," "),'lesson_type':val.lessonType}; //课时信息
158 | if(val.videoTime){
159 | var videoTime = val.videoTime.split(':');
160 | if(videoTime.length == 3){
161 | allSeconds += parseInt(videoTime[0]) * 3600 + parseInt(videoTime[1]) * 60 + parseInt(videoTime[2]);
162 | }else if(videoTime.length == 2){
163 | allSeconds += parseInt(videoTime[0]) * 60 + parseInt(videoTime[1]);
164 | }else if(videoTime.length == 1){
165 | allSeconds += parseInt(videoTime[0]);
166 | }
167 | }
168 | chapter.lesson_info.push(lesson);
169 | });
170 | course_info.chapter_info.push(chapter);
171 | });
172 | var formatTime = {'hour':0,'minute':0,'second':0};
173 | formatTime.hour = parseInt(allSeconds/3600);
174 | formatTime.minute = parseInt(allSeconds%3600/60);
175 | formatTime.second = parseInt(allSeconds%3600%60);
176 | if(formatTime.hour){
177 | course_info.course_duration = formatTime.hour + "小时" + formatTime.minute + "分" + formatTime.second + "秒";
178 | }else if(formatTime.minute){
179 | course_info.course_duration = formatTime.minute + "分" + formatTime.second + "秒";
180 | }else if(formatTime.second){
181 | course_info.course_duration = formatTime.second + "秒";
182 | }
183 | mylog(course_info);
184 | if(course_info.course_price > 0){
185 | return false;
186 | }else{
187 | return true;
188 | }
189 | }
190 |
191 | //批量下载
192 | function batchDownload(){
193 | course_info.chapter_info.forEach(function(chapter,index){
194 | chapter.lesson_info.forEach(function(lesson){
195 | var file_name = lesson.keshi + '_' + lesson.lesson_name;
196 | var save_path = course_save_path.replace(/\\/g,'\/') + '/' + course_info.course_name + '/章节' + (index + 1) + '_' + chapter.chapter_name;
197 | if(lesson.lesson_type=="3"){
198 | getTextLearnInfo(lesson,file_name,save_path);
199 | }else{
200 | getVideoLearnInfo(lesson,file_name,save_path);
201 | }
202 | });
203 | });
204 | }
205 |
206 | // 添加下载按钮
207 | function addDownloadButton(){
208 | var ksbtn = document.getElementsByClassName('ksbtn')[0];
209 | var ksbtn_style = 'display:' + getStyle(ksbtn,'display') + ';width:' + getStyle(ksbtn,'width') + ';background-position:' + getStyle(ksbtn,'background-position') + ';margin-top:' + getStyle(ksbtn,'margin-top') + ';';
210 | var ksbtn_span = ksbtn.firstChild;
211 | var ksbtn_span_style = 'display:' + getStyle(ksbtn_span,'display') + ';text-align:' + getStyle(ksbtn_span,'text-align') + ';background:' + getStyle(ksbtn_span,'background') +
212 | ';width:' + getStyle(ksbtn_span,'width') + ';font-size:' + getStyle(ksbtn_span,'font-size') + ';height:' + getStyle(ksbtn_span,'height') + ';line-height:' +
213 | getStyle(ksbtn_span,'line-height') + ';color:' + getStyle(ksbtn_span,'color') + ';background-position:' + getStyle(ksbtn_span,'background-position') + ';';
214 | var allNodes = document.getElementsByClassName("section");
215 | for (var i = 0;i < allNodes.length;i ++) {
216 | var download_button = document.createElement("a");
217 | var style = 'display:block;text-align:center;padding-left:10px;width:58px;font-size:12px;height:34px;line-height:33px;color:#fff;background-position:-40px 0px;';
218 | download_button.innerHTML = "下载";
219 | download_button.className = "f-fr j-hovershow download-button";
220 | download_button.style = ksbtn_style;
221 | download_button.lastChild.style = ksbtn_span_style;
222 | allNodes[i].appendChild(download_button);
223 | }
224 | $('.download-button').each(function(){ //下载按钮点击事件
225 | $(this).click(function(event){
226 | loadSetting();
227 | if(course_save_path==""){
228 | alert("请到下载助手的设置里面填写文件保存位置");
229 | }else if(aria2_url==""){
230 | alert("请到下载助手的设置里面填写 Aria2 地址");
231 | }else{
232 | var data_chapter = event.target.parentNode.parentNode.getAttribute("data-chapter");
233 | var data_lesson = event.target.parentNode.parentNode.getAttribute("data-lesson");
234 | var index = Number(data_lesson);
235 | for(var i = 0;i < Number(data_chapter); i ++){
236 | index = index - course_info.chapter_info[i].lesson_info.length;
237 | }
238 | var lesson = course_info.chapter_info[data_chapter].lesson_info[index];
239 | mylog("选择的课为【lesson_name: " + lesson.lesson_name + ",lesson_id: " + lesson.lesson_id + ",lesson_type: " + lesson.lesson_type + '】');
240 | var file_name = lesson.keshi + '_' + lesson.lesson_name;
241 | var save_path = course_save_path.replace(/\\/g,'\/') + '/' + course_info.course_name + '/章节' + (Number(data_chapter) + 1) + '_' + course_info.chapter_info[data_chapter].chapter_name;
242 | if(lesson.lesson_type=="3"){
243 | getTextLearnInfo(lesson,file_name,save_path);
244 | }else{
245 | getVideoLearnInfo(lesson,file_name,save_path);
246 | }
247 | }
248 | event.stopPropagation();
249 | });
250 | });
251 | }
252 |
253 | //获取文档下载地址
254 | function getTextLearnInfo(lesson,file_name,save_path){
255 | var timestamp = new Date().getTime();
256 | var params = {
257 | "callCount":"1",
258 | "scriptSessionId":"${scriptSessionId}190",
259 | "httpSessionId":match_cookie,
260 | "c0-scriptName":"LessonLearnBean",
261 | "c0-methodName":"getTextLearnInfo",
262 | "c0-id":"0",
263 | "c0-param0":"string:" + lesson.lesson_id,
264 | "c0-param1":"string:" + course_info.course_id,
265 | "batchId":timestamp
266 | };
267 | var url = "https://study.163.com/dwr/call/plaincall/LessonLearnBean.getTextLearnInfo.dwr?" + timestamp;
268 | $.ajax({
269 | url:url,
270 | method:'POST',
271 | async: true,
272 | data: params,
273 | success: function (response){
274 | var pdfUrl = response.match(/pdfUrl:"(.*?)"/)[1];
275 | sendDownloadTaskToAria2(pdfUrl,file_name + ".pdf",save_path);
276 | }
277 | });
278 | }
279 |
280 | //获取视频文件信息
281 | function getVideoLearnInfo(lesson,file_name,save_path){
282 | var timestamp = new Date().getTime();
283 | var params = {
284 | "callCount":"1",
285 | "scriptSessionId":"${scriptSessionId}190",
286 | "httpSessionId":match_cookie,
287 | "c0-scriptName":"LessonLearnBean",
288 | "c0-methodName":"getVideoLearnInfo",
289 | "c0-id":"0",
290 | "c0-param0":"string:" + lesson.lesson_id,
291 | "c0-param1":"string:" + course_info.course_id,
292 | "batchId":timestamp
293 | };
294 | var url = "https://study.163.com/dwr/call/plaincall/LessonLearnBean.getVideoLearnInfo.dwr?" + timestamp;
295 | $.ajax({
296 | url:url,
297 | method:'POST',
298 | async: true,
299 | data: params,
300 | success: function (response){
301 | var signature = response.match(/signature="(\w+?)";/)[1];
302 | var videoId = response.match(/videoId=(\w+?);/)[1];
303 | getVideoUrl(videoId,signature,file_name,save_path);
304 | }
305 | });
306 | }
307 |
308 | //获取视频下载地址
309 | function getVideoUrl(videoId,signature,file_name,save_path){
310 | var params = {
311 | 'videoId':videoId,
312 | 'signature':signature,
313 | 'clientType':'1'
314 | };
315 | $.ajax({
316 | url:"https://vod.study.163.com/eds/api/v1/vod/video",
317 | method:'POST',
318 | async:true,
319 | data:params,
320 | success:function(response){
321 | var videoUrls = response.result.videos;
322 | var video_url_list = [];
323 | videoUrls.forEach(function(video){
324 | if(video.format == video_format) {
325 | video_url_list.push({'video_format': video.format,'video_quality': video.quality,'video_url': video.videoUrl});
326 | }
327 | });
328 | if(video_url_list.length != 0){
329 | if(video_quality=="2"){
330 | video_download_url = video_url_list[video_url_list.length-1].video_url;
331 | }else{
332 | video_download_url = video_url_list[0].video_url;
333 | }
334 | }
335 | if(video_download_url != ""){
336 | //mylog(video_download_url);
337 | sendDownloadTaskToAria2(video_download_url,file_name + '.' + video_format,save_path);
338 | }
339 | }
340 | });
341 | }
342 |
343 | // 将下载链接发送到 Aria2 下载
344 | function sendDownloadTaskToAria2(download_url,file_name,save_path){
345 | var json_rpc = {
346 | id:'',
347 | jsonrpc:'2.0',
348 | method:'aria2.addUri',
349 | params:[
350 | [download_url],
351 | {
352 | dir:save_path,
353 | out:file_name
354 | }
355 | ]
356 | };
357 | GM_xmlhttpRequest({
358 | url:aria2_url,
359 | method:'POST',
360 | data:JSON.stringify(json_rpc),
361 | onerror:function(response){
362 | mylog(response);
363 | },
364 | onload:function(response){
365 | mylog(response);
366 | if (!hasOpenAriac2Tab){
367 | GM_openInTab('http://aria2c.com/',{active:true});
368 | hasOpenAriac2Tab = true;
369 | }
370 | }
371 | });
372 | }
373 |
374 | //获取元素样式
375 | function getStyle(element,cssPropertyName){
376 | if(window.getComputedStyle){ //如果支持getComputedStyle属性(IE9及以上,ie9以下不兼容)
377 | return window.getComputedStyle(element)[cssPropertyName];
378 | } else { //如果支持currentStyle(IE9以下使用),返回
379 | return element.currentStyle[cssPropertyName];
380 | }
381 | }
382 |
383 | })();
--------------------------------------------------------------------------------