-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPanopto-Video-DL.js
284 lines (251 loc) · 11.1 KB
/
Panopto-Video-DL.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// ==UserScript==
// @name Panopto-Video-DL
// @namespace https://github.com/Panopto-Video-DL
// @description Video downloader for Panopto
// @icon https://t0.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://panopto.com&size=96
// @author Panopto-Video-DL
// @version 3.5.1
// @copyright 2021, Panopto-Video-DL
// @license MIT
// @homepage https://github.com/Panopto-Video-DL/Panopto-Video-DL-browser
// @homepageURL https://github.com/Panopto-Video-DL/Panopto-Video-DL-browser
// @supportURL https://github.com/Panopto-Video-DL/Panopto-Video-DL-browser/issues
// @require https://greasyfork.org/scripts/401626-notify-library/code/Notify%20Library.js
// @match https://*.panopto.com/Panopto/Pages/Viewer.aspx?*id=*
// @match https://*.panopto.eu/Panopto/Pages/Viewer.aspx?*id=*
// @match https://*.panopto.com/Panopto/Pages/Embed.aspx?*id=*
// @match https://*.panopto.eu/Panopto/Pages/Embed.aspx?*id=*
// @match https://*.panopto.com/Panopto/Pages/Sessions/List.aspx*
// @match https://*.panopto.eu/Panopto/Pages/Sessions/List.aspx*
// @connect panopto.com
// @connect panopto.eu
// @grant GM_addStyle
// @grant GM_setClipboard
// @grant GM_openInTab
// @grant GM_registerMenuCommand
// @noframes
// ==/UserScript==
/* globals Notify */
(function () {
'use strict';
addStyle('#Panopto-Video-DL{position:fixed;top:10%;left:50%;width:70%;padding:2em 3em 1em;background-color:#2d3436;transform:translateX(-50%);z-index:1050}#Panopto-Video-DL *{margin-bottom:10px;color:#fff!important;font-size:18px;}#Panopto-Video-DL > div {margin-top: 1em;}#Panopto-Video-DL ul,#Panopto-Video-DL ol,#Panopto-Video-DL li{margin:0 .5em;padding:0 .5em;list-style:decimal}#Panopto-Video-DL button{margin-left:5px;margin-right:5px;color:#000!important;font-size:16px;}#Panopto-Video-DL p{margin-top:0.5em;}#Panopto-Video-DL input{color:black!important;}#Panopto-Video-DL textarea{width:100%;color:black!important;resize:vertical;white-space:nowrap;}')
if (location.pathname.includes('/List.aspx')) {
log('Service started');
const button = document.createElement('button');
button.className = 'css-t83cx2 css-tr3oo4 css-coghg4';
button.role = 'button';
button.style.marginLeft = '0.5rem';
button.innerHTML = '<span class="material-icons css-6xugel" style="font-size: 18px;margin-bottom:-0.25rem;">file_download</span>Download';
button.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();
let _t;
const list = (_t = document.querySelectorAll('#listViewContainer tbody > tr a.detail-title')).length ?
_t : (_t = document.querySelectorAll('#detailsTable tbody > tr a.detail-title')).length ?
_t : (_t = document.querySelectorAll('#thumbnailGrid > li a.detail-title')).length ?
_t : null;
if (!list) {
log('No videos found', 'error');
new Notify({
text: 'No videos found',
type: 'error'
}).show();
return;
}
const n = new Notify({
text: 'Getting links. Please wait',
type: 'info',
timeout: 2000
});
n.show();
const requestsList = [...list].map(item => {
let videoId = new URL(item.getAttribute('href')).searchParams.get('id');
const videoTitle = item.textContent.trim();
return requestDeliveryInfo(videoId)
.catch(error => {
new Notify({
text: 'Failed to get lesson link for "' + videoTitle + '"',
type: 'error',
timeout: null
}).show();
});
});
Promise.allSettled(requestsList)
.then(responses => {
// log(responses)
n.close();
let copyText = '';
responses.forEach(response => {
if (response.status == 'fulfilled' && response.value) {
const streamUrl = response.value?.[0];
if (streamUrl)
copyText += streamUrl + '\n';
}
});
if (copyText !== '')
copyToClipboard(copyText);
});
});
document.querySelector('#actionHeader button')?.parentElement.appendChild(button);
}
else if (location.pathname.includes('/Viewer.aspx')) {
log('Service started');
const button = document.createElement('a');
button.href = '#';
button.innerHTML = '<span class="material-icons" style="font-size:15px;margin-bottom:-0.25rem;">file_download</span> Download';
button.classList = 'event-tab-header';
button.style = 'display:inline-flex;align-items:center;position:absolute;bottom:30px;padding:5px 10px;text-decoration:none;cursor:pointer;';
button.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();
getVideoDownloadLink();
});
document.querySelector('#eventTabControl').appendChild(button);
if (typeof GM_registerMenuCommand !== 'undefined')
GM_registerMenuCommand('Download', () => getVideoDownloadLink());
}
else if (location.pathname.includes('/Embed.aspx')) {
const button = document.createElement('div');
button.role = 'button';
button.title = 'Download';
button.classList = 'button-control material-icons';
button.innerHTML = '<span class="material-icons">file_download</span>';
button.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();
getVideoDownloadLink();
});
// document.querySelector('#navigationControls')?.appendChild(button);
const searcher = () => setTimeout(() => {
const nav = document.querySelector('#navigationControls');
if (!nav) return searcher();
nav.appendChild(button);
}, 1_000);
searcher();
if (typeof GM_registerMenuCommand !== 'undefined')
GM_registerMenuCommand('Download', () => getVideoDownloadLink());
}
// Functions
function getVideoDownloadLink() {
const url = new URL(location.href)
const videoId = url.searchParams.get('id');
if (!videoId) {
new Notify({
text: 'Failed to get Lesson ID.',
type: 'error',
timeout: null
}).show();
return;
}
const n = new Notify({
text: 'Getting links. Please wait',
type: 'info',
timeout: 2000
});
n.show();
requestDeliveryInfo(videoId)
.then(_streams => {
const streamUrl = _streams[0];
const streams = _streams[1];
if (streamUrl.endsWith('master.m3u8') || streamUrl.match(/\.panobf\d+/)) {
if (localStorage.getItem('popup-viewed') != 'true')
showModal('<h1 style="text-align:center;font-size:30px;">READ ME</h1> <p>To download the video follow these steps:</p> <ol><li>Download this program from <a href="https://github.com/Panopto-Video-DL/Panopto-Video-DL" target="_blank">GitHub</a> (No installation needed) and open it</li> <li>Paste the automatically copied link</li> <li>Set the destination folder</li> <li>Wait for the download to finish</li> </ol> <p style="text-align:center;"> <button onclick="this.parentElement.parentElement.remove();">Close</button> <button onclick="localStorage.setItem(\'popup-viewed\', true);this.parentElement.parentElement.remove();">Close and don\'t show again</button> </p>');
copyToClipboard(streamUrl);
}
else {
if (typeof GM_openInTab !== 'undefined')
GM_openInTab(streamUrl, false);
else
window.open(streamUrl);
}
if (streams.length && localStorage.getItem('other-source-viewed') != 'true') {
const modal = showModal('<h2 style="font-size:20px;">Download another source video</h2><ul></ul><p style="text-align:center;"><button onclick="this.parentElement.parentElement.remove();">Close</button><button onclick="localStorage.setItem(\'other-source-viewed\', true);this.parentElement.parentElement.remove();">Close and don\'t show again</button></p>');
streams.forEach((value, index) => {
const li = document.createElement('li');
li.innerHTML = (value.Name?.replace(/-?(\d{8}T\d+Z)+((.)?(\w+))?/g, '').replace(/_/g, ' ') || 'Stream ' + (index + 1)) + '<button>Copy</button>';
li.querySelector('button').addEventListener('click', (e) => { copyToClipboard(value.StreamUrl); })
modal.querySelector('ul').appendChild(li);
});
}
})
.catch(error => {
new Notify({
text: 'Failed to get lesson link',
type: 'error',
timeout: null
}).show();
})
.finally(() => n.close());
}
function requestDeliveryInfo(videoId) {
return fetch(
location.origin + '/Panopto/Pages/Viewer/DeliveryInfo.aspx', {
method: 'POST',
headers: {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
body: 'deliveryId=' + videoId + '&isEmbed=true&responseType=json',
})
.then(respose => respose.json())
.then(data => {
const errorCode = data.ErrorCode;
if (errorCode)
throw new Error(data.ErrorMessage ?? '', { code: errorCode ?? -1 });
const streamUrl = data.Delivery?.PodcastStreams[0]?.StreamUrl;
const streams = (data.Delivery?.Streams || []).filter(x => x.StreamUrl != streamUrl);
if (!streamUrl)
throw new Error('Stream URL not ready yet');
return [streamUrl, streams];
})
.catch(error => {
log(error);
throw error;
});
}
function copyToClipboard(text) {
if (typeof GM_setClipboard !== 'undefined') {
GM_setClipboard(text, 'text');
new Notify({
text: 'Copied!',
type: 'success'
}).show();
} else {
navigator.clipboard.writeText(text).then(() => {
new Notify({
text: 'Copied!',
type: 'success'
}).show();
}).catch(e => {
log(e);
const modal = showModal('<h3>There was an error when copying the download link</h3> <p>Copy it manually:</p><textarea type="text" value="" rows="3" onclick="this.select();"></textarea><p style="text-align:center;"><button onclick="this.parentElement.parentElement.remove();">Close</button></p>');
modal.querySelector('textarea').value = text;
});
}
}
function showModal(html) {
const div = document.createElement('div');
div.innerHTML = html;
if (document.querySelector('#Panopto-Video-DL')) {
const hr = document.createElement('hr');
div.prepend(hr);
document.querySelector('#Panopto-Video-DL').append(div);
} else {
div.id = 'Panopto-Video-DL';
document.querySelector('body').appendChild(div);
}
return div;
}
function addStyle(CSS) {
if (typeof GM_addStyle != 'undefined') {
GM_addStyle(CSS);
} else {
const style = document.createElement('style');
style.innerText = CSS;
document.head.appendChild(style);
}
}
function log(message, level = 'log') {
console[level]('%c Panopto-Video-DL ->', 'color:red;font-size:14px;', message);
}
})();