-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathteachable-downloader.user.js
81 lines (71 loc) · 2.48 KB
/
teachable-downloader.user.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
// ==UserScript==
// @name Teachable video downloader
// @downloadURL https://github.com/Lambik/wistia-downloader/raw/master/teachable-downloader.user.js
// @namespace https://github.com/Lambik/
// @version 0.3
// @description Make all wistia videos downloadable on teachable courses
// @author You
// @match *://*.teachable.com/*
// @match *://courses.coursecreatorpro.com/*
// @match *://courses.fulltimefilmmaker.com/*
// @match *://courses.52kards.com/*
// @match *://howtofreedive.com/*
// @connect fast.wistia.net
// @grant GM_xmlhttpRequest
// @run-at document-end
// ==/UserScript==
(function () {
'use strict';
function parseVideos() {
let viddivs = document.getElementsByClassName('attachment-wistia-player');
for (let viddiv of viddivs) {
let wistiaId = viddiv.dataset.wistiaId;
GM_xmlhttpRequest(
{
method : 'GET',
url : 'https://fast.wistia.net/embed/iframe/' + wistiaId,
onerror: function (result) {
console.error('xmlrequest error', result);
},
onabort: function (result) {
console.error('xmlrequest abort', result);
},
onload : function (result) {
let body = result.responseText;
let assets = body.match(/"assets":(\[.*?\])/);
if (assets.length) {
let allvids = JSON.parse(assets[1]);
console.log(allvids);
let newNode = document.createElement('div');
let str = "<h4>Download options:</h4><ul>";
for (let vid of allvids) {
if (vid.type != 'hls_video' && vid.public) {
str += '<li>' + vid.display_name + ' (' + vid.width + 'x' + vid.height + ', ' + vid.ext + '): <a href="' + vid.url + '" target="_blank">' + formatBytes(vid.size) + '</a></li>';
}
}
str += "</ul>";
newNode.innerHTML = str;
viddiv.parentNode.parentNode.parentNode.appendChild(newNode)
}
else {
console.error('wista returned invalid data', body);
}
}
}
);
}
}
// thanks to https://stackoverflow.com/a/18650828/102720
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) {
return '0 Bytes';
}
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
parseVideos();
window.addEventListener('lecture:ajax_success', parseVideos);
})();