-
Notifications
You must be signed in to change notification settings - Fork 14
/
MSBookmarklet.js
300 lines (293 loc) · 12.1 KB
/
MSBookmarklet.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
javascript: (() => {
// In case a Mediasite Channel was detected (usually that is just the player embedded into another site),
// show an alert and redirect to the site with just the player
if (document.getElementById("player-iframe")) {
alert("MediasiteDownloader: A Mediasite Channel was detected. After you click \"OK\" you will be redirected to the video. Click Play and then again on the MediasiteDownloader bookmark.\nNote: Your browser may block the redirect. Make sure to allow pop-ups for this website.");
window.open(document.getElementById("player-iframe").src);
}
// If 'MediasitePlayer' does not exist, we are still running an older version of Mediasite
if(typeof MediasitePlayer !== 'object') {
// Grab service path and resource ID and make POST request to get data about current video
fetch(window.location.origin + document.getElementById('ServicePath').innerHTML + '/GetPlayerOptions', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
'getPlayerOptionsRequest': {
'ResourceId': document.getElementById('ResourceId').innerHTML,
'QueryString': '',
'UseScreenReader': 'false',
'UrlReferrer': ''
}
})
})
.then((response) => response.json())
.then((playerOpts) => {
// Insert custom CSS for modal
document.head.insertAdjacentHTML("beforeend", '<style type=\'text/css\'>\
.modal-window {\
position: fixed;\
display: flex;\
justify-content: center;\
align-items: center;\
background-color: rgba(0, 0, 0, 0.75);\
top: 0;\
right: 0;\
bottom: 0;\
left: 0;\
z-index: 999;\
opacity: 0;\
pointer-events: none;\
}\
.modal-window:target {\
opacity: 1;\
pointer-events: auto;\
}\
.modal-window > div {\
width: 450px;\
position: absolute;\
padding: 3em;\
background: #ffffff;\
color: #333333;\
border-radius: 5px;\
}\
.modal-window header {\
font-weight: bold;\
}\
.modal-window h1 {\
font-size: 150%;\
margin: 0 0 15px;\
color: #333333;\
}\
.modal-close {\
color: #4c4c4c;\
line-height: 35px;\
position: absolute;\
right: 5px;\
text-align: center;\
top: 5px;\
width: 70px;\
text-decoration: none;\
border: #4c4c4c;\
border-style: solid;\
border-radius: 5px;\
border-width: 1px;\
}\
.modal-window > div > ul > li {\
margin: 10px 0;\
}\
.MSDLthumbnail {\
width: 350px;\
vertical-align: middle;\
margin-bottom: 4px;\
}\
div#MSDLinfo {\
max-height: 70vh;\
min-height: 5vh;\
overflow-y: auto;\
}\
</style>');
// If modal was already inserted, remove it
if (document.contains(document.getElementById("open-modal"))) {
document.getElementById("open-modal").remove();
}
// If video is NOT on demand, it is probably a livestream that cannot be downloaded yet
if (playerOpts.d.Presentation.PlayStatus != "OnDemand") {
document.body.insertAdjacentHTML("beforeend",
'<div id="open-modal" class="modal-window"> \
<div>\
<a href="#" title="Close" class="modal-close">Close</a>\
<p style="font-size: 20px;">Lecture is currently not available on-demand and therefore cannot be downloaded.<br>Try again later.</p>\
</div>\
</div>');
} else {
// If video is available on demand, create modal that will be populated later on
document.body.insertAdjacentHTML("beforeend",
'<div id="open-modal" class="modal-window">\
<div>\
<div style="font-size: 20px;line-height: 45px;position: absolute;left: 20px;top: 5px;">MediasiteDownloader (<a href="https://github.com/KLVN/MediasiteDownloader" target="_blank">GitHub</a>)</div>\
<a href="#" title="Close" class="modal-close">Close</a>\
<div id="MSDLinfo">\
<ul style="list-style: outside; !important">\
<li>Copy the title: <input type="text" onClick="this.select();" value="' + document.title + '"></li>\
<li>Right-click on the thumbnail(s) and choose "Save <span style="font-weight: bold;">link</span> as..."</li>\
<div id="MSDLvideos"></div>\
<li>Paste in to rename file correctly and save it</li>\
<li>Click <a href="https://klvn.github.io/MediasiteDownloader/" target="_blank">here</a> for more detailed instructions</li>\
</ul>\
</div>\
</div>\
</div>');
// Grab all video streams
var allPresentations = playerOpts.d.Presentation.Streams;
// Just a flag to record if any video is available
var videoAvailable = false;
// Go through all video streams
for (var i = 0; i < allPresentations.length; i++) {
if (allPresentations[i].VideoUrls.length) {
for (var j = 0; j < allPresentations[i].VideoUrls.length; j++) {
// Only look for MP4 files
if (allPresentations[i].VideoUrls[j].MimeType == "video/mp4") {
// Set flag because there is at least one MP4 file
videoAvailable = true;
var thumbnail = window.location.origin + allPresentations[i].ThumbnailUrl;
var videoUrl = allPresentations[i].VideoUrls[j].Location;
// Populate modal that lists all downloadable video streams
document.getElementById("MSDLvideos").innerHTML += "<li><a href='" + videoUrl + "' target='_blank'><img class='MSDLthumbnail' src='" + thumbnail + "'></a></li>";
}
}
}
}
// Vodcasts are easier to deal with, so if it is one then just grab the URL for downloading
if (playerOpts.d.Presentation.VodcastUrl != null) {
videoAvailable = true;
var thumbnail = window.location.origin + playerOpts.d.Presentation.ThumbnailUrl;
var videoUrl = playerOpts.d.Presentation.VodcastUrl;
document.getElementById("MSDLvideos").innerHTML += "<li>Vodcast:<br><a href='" + videoUrl + "' target='_blank'><img class='MSDLthumbnail' src='" + thumbnail + "'></a></li>";
}
// If flag was not set, no downloadable MP4 file was found
if (!videoAvailable) document.getElementById("MSDLinfo").innerHTML = "<li>Sorry, no video(s) available.</li>";
}
// Pause player so it does not continue to run in the background
Mediasite.Player.API.pause();
// Open modal
location.href = "#open-modal";
})
// If 'MediasitePlayer' does exist, we are running a newer version (2022) of Mediasite
// and have to use a few different variables. But essentially it is the same process.
} else {
fetch(window.location.origin + SiteData.PlayerService + '/GetPlayerOptions', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
'getPlayerOptionsRequest': {
'ResourceId': SiteData.PresentationId,
'QueryString': '',
'UseScreenReader': 'false',
'UrlReferrer': ''
}
})
})
.then((response) => response.json())
.then((playerOpts) => {
document.head.insertAdjacentHTML("beforeend", '<style type=\'text/css\'>\
.modal-window {\
position: fixed;\
display: flex;\
justify-content: center;\
align-items: center;\
background-color: rgba(0, 0, 0, 0.75);\
top: 0;\
right: 0;\
bottom: 0;\
left: 0;\
z-index: 999;\
opacity: 0;\
pointer-events: none;\
}\
.modal-window:target {\
opacity: 1;\
pointer-events: auto;\
}\
.modal-window > div {\
width: 450px;\
position: absolute;\
padding: 3em;\
background: #ffffff;\
color: #333333;\
border-radius: 5px;\
}\
.modal-window header {\
font-weight: bold;\
}\
.modal-window h1 {\
font-size: 150%;\
margin: 0 0 15px;\
color: #333333;\
}\
.modal-close {\
color: #4c4c4c;\
line-height: 35px;\
position: absolute;\
right: 5px;\
text-align: center;\
top: 5px;\
width: 70px;\
text-decoration: none;\
border: #4c4c4c;\
border-style: solid;\
border-radius: 5px;\
border-width: 1px;\
}\
.modal-window > div > ul > li {\
margin: 10px 0;\
}\
.MSDLthumbnail {\
width: 350px;\
vertical-align: middle;\
margin-bottom: 4px;\
}\
div#MSDLinfo {\
max-height: 70vh;\
min-height: 5vh;\
overflow-y: auto;\
}\
</style>');
if (document.contains(document.getElementById("open-modal"))) {
document.getElementById("open-modal").remove();
}
if (playerOpts.d.Presentation.PlayStatus != "OnDemand") {
document.body.insertAdjacentHTML("beforeend",
'<div id="open-modal" class="modal-window"> \
<div>\
<a href="#" title="Close" class="modal-close">Close</a>\
<p style="font-size: 20px;">Lecture is currently not available on-demand and therefore cannot be downloaded.<br>Try again later.</p>\
</div>\
</div>');
} else {
document.body.insertAdjacentHTML("beforeend",
'<div id="open-modal" class="modal-window">\
<div>\
<div style="font-size: 20px;line-height: 45px;position: absolute;left: 20px;top: 5px;">MediasiteDownloader (<a href="https://github.com/KLVN/MediasiteDownloader" target="_blank">GitHub</a>)</div>\
<a href="#" title="Close" class="modal-close">Close</a>\
<div id="MSDLinfo">\
<ul style="list-style: outside; !important">\
<li>Copy the title: <input type="text" onClick="this.select();" value="' + document.title + '" style="background-color: #FFF"></li>\
<li>Right-click on the thumbnail(s) and choose "Save <span style="font-weight: bold;">link</span> as..."</li>\
<div id="MSDLvideos"></div>\
<li>Paste in to rename file correctly and save it</li>\
<li>Click <a href="https://klvn.github.io/MediasiteDownloader/" target="_blank">here</a> for more detailed instructions</li>\
</ul>\
</div>\
</div>\
</div>');
var allPresentations = playerOpts.d.Presentation.Streams;
var videoAvailable = false;
for (var i = 0; i < allPresentations.length; i++) {
if (allPresentations[i].VideoUrls.length) {
for (var j = 0; j < allPresentations[i].VideoUrls.length; j++) {
if (allPresentations[i].VideoUrls[j].MimeType == "video/mp4") {
videoAvailable = true;
var thumbnail = window.location.origin + allPresentations[i].ThumbnailUrl;
var videoUrl = allPresentations[i].VideoUrls[j].Location;
document.getElementById("MSDLvideos").innerHTML += "<li><a href='" + videoUrl + "' target='_blank'><img class='MSDLthumbnail' src='" + thumbnail + "'></a></li>";
}
}
}
}
if (playerOpts.d.Presentation.VodcastUrl != null) {
videoAvailable = true;
var thumbnail = window.location.origin + playerOpts.d.Presentation.ThumbnailUrl;
var videoUrl = playerOpts.d.Presentation.VodcastUrl;
document.getElementById("MSDLvideos").innerHTML += "<li>Vodcast:<br><a href='" + videoUrl + "' target='_blank'><img class='MSDLthumbnail' src='" + thumbnail + "'></a></li>";
}
if (!videoAvailable) document.getElementById("MSDLinfo").innerHTML = "<li>Sorry, no video(s) available.</li>";
}
MediasitePlayer.pause();
location.href = "#open-modal";
})
}
})()