-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
469 lines (400 loc) · 17.8 KB
/
index.html
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!doctype HTML>
<!--
MIT License
Copyright (c) 2023 Jason Benoit, David Giesbrecht and Stephany Ceron
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<html>
<head>
<meta charset="UTF-8">
<title>Attribution Builder</title>
<meta author="Jason Benoit, David Giesbrecht, and Stephany Ceron">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="shortcut icon" href="images/oer_logo.ico" />
<script>
var pageURL;
var useCustomInput = false;
const parser = new DOMParser();
var doc;
// data type that holds necessary info for attribution
class Attribution {
constructor(pageURL, pageTitle, bookURL, bookTitle, author, licenseURL, licenseType) {
this.pageURL = pageURL;
this.pageTitle = pageTitle;
this.bookURL = bookURL;
this.bookTitle = bookTitle;
this.author = author;
this.licenseURL = licenseURL;
this.licenseType = licenseType;
}
}
class VideoAttribution {
constructor(videoURL, videoTitle, channelURL, channelTitle, videoDuration) {
this.videoURL = videoURL;
this.videoTitle = videoTitle;
this.channelURL = channelURL;
this.channelTitle = channelTitle;
this.videoDuration = videoDuration;
}
}
// get page from resource and load it
function loadTargetPage() {
if (useCustomInput) {
buildAttribution();
return;
}
let inputDiv = document.getElementById('customInputDiv');
const loadingDiv = document.getElementById('loading-dots');
const outputDiv = document.getElementById("outputDiv");
const errorDiv = document.getElementById("error");
loadingDiv.style.display = "block";
outputDiv.style.display = "none";
errorDiv.style.display = "none";
document.getElementById("resultTxtCopyRes").innerHTML = "";
pageURL = document.getElementById('targetURL').value;
console.log(pageURL);
// note that the page is being fetched through the allorigins proxy server API
// to get around CORS being disabled on the target server
fetch(`https://api.allorigins.win/get?url=${encodeURIComponent(pageURL)}`)
.then(response => {
if (response.ok) return response.json()
throw new Error('Network response was not ok.')
})
.then(data => {
doc = parser.parseFromString(data.contents, 'text/html');
buildAttribution();
})
.catch(error => {
errorDiv.innerText = 'Something went wrong. Please try again.'
errorDiv.style.display = 'block';
loadingDiv.style.display = "none";
});
}
function isPressbooks() {
var elements = doc.getElementsByTagName('*');
for (let i = 0; i < elements.length; i++) {
let classes = elements[i].getAttribute('class');
if (classes && classes.includes("pressbooks")) {
return true;
}
}
return false;
}
// Create and display attribution for a Pressbooks resource
function getPressbooksAttribution() {
console.log(pageURL);
let result = new Attribution(pageURL);
var anchorTags = doc.getElementsByTagName('a');
for (let i = 0; i < anchorTags.length; i++) {
if (anchorTags[i].getAttribute('rel') === "cc:attributionURL") {
result.bookURL = anchorTags[i].getAttribute('href');
result.bookTitle = anchorTags[i].innerHTML;
}
else if (anchorTags[i].getAttribute('rel') === "license") {
result.licenseURL = anchorTags[i].getAttribute('href');
result.licenseType = anchorTags[i].innerHTML;
}
}
var title = doc.getElementsByTagName('title')[0].innerHTML;
result.pageTitle = title.substr(0, title.indexOf(result.bookTitle) - 3);
var spans = doc.getElementsByTagName('span');
for (let i = 0; i < spans.length; i++) {
if (spans[i].getAttribute('property') === "cc:attributionName") {
result.author = spans[i].innerHTML;
}
}
return result;
}
// Create and display attribution for an OpenStax resource
function getOpenStaxAttribution() {
let result = new Attribution(pageURL);
var pageTitleElement = doc.querySelector("[class^=\"BookBanner__BookChapter\"]");
result.pageTitle = pageTitleElement.innerText;
console.log(result.pageTitle);
var bookTitleElement = doc.querySelector("[class^=\"BookBanner__BookTitleLink\"]");
result.bookTitle = bookTitleElement.innerText;
result.bookURL = 'https://openstax.org' + bookTitleElement.getAttribute('href');
console.log(result.bookTitle + " " + result.bookURL);
var licenseElement = doc.querySelector("[data-html=\"copyright\"] > a");
result.licenseType = licenseElement.innerText;
result.licenseURL = licenseElement.getAttribute('href');
console.log(result.licenseType + " " + result.licenseURL);
result.author = "<a href=\'https://openstax.org/\'>OpenStax - Rice University</a>";
return result;
}
function getCustomAttribution() {
let result = new Attribution(
document.getElementById('pageURL').value,
document.getElementById('pageTitle').value,
document.getElementById('bookURL').value,
document.getElementById('bookTitle').value,
document.getElementById('author').value,
document.getElementById('licenceURL').value,
document.getElementById('licenceType').value
);
useCustomInput = false;
return result;
}
/**
* Checks if the provided URL is a valid YouTube video URL.
* @return {string|null} - The YouTube video ID if the URL is valid, otherwise null.
*/
function isYouTubeVideo() {
const regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|watch\?.+&v=)([^#\&\?]*).*/;
const match = pageURL.match(regExp);
return (match && match[2].length === 11) ? match[2] : null;
}
function buildAttribution() {
let attribution;
let outTxt = document.getElementById('resultTxt');
let targetURL = document.getElementById('targetURL').value;
let inputDiv = document.getElementById('customInputDiv');
if (!useCustomInput && isPressbooks()) {
inputDiv.style.display = 'none';
attribution = getPressbooksAttribution();
}
else if (!useCustomInput && targetURL.includes('openstax')) {
inputDiv.style.display = 'none';
attribution = getOpenStaxAttribution();
}
else if (!useCustomInput && isYouTubeVideo()) {
const attribution = getVideoAttribution()
const output = `<p>Video: "<a href="${attribution.videoURL}">${attribution.videoTitle}</a>" by <a href="${attribution.channelURL}">${attribution.channelTitle}</a> [${attribution.duration}] is licensed under the <a href="https://www.youtube.com/static?template=terms">Standard YouTube License</a>.<em>Transcript and closed captions available on YouTube.</em></p>`;
document.getElementById('loading-dots').style.display = "none";
document.getElementById("error").style.display = "none";
document.getElementById("outputDiv").style.display = "block";
outTxt.value = output;
return;
}
else {
useCustomInput = true;
document.getElementById('loading-dots').style.display = "none";
document.getElementById("error").style.display = "none";
if (inputDiv.style.display == 'block') {
attribution = getCustomAttribution();
}
else {
inputDiv.style.display = 'block';
return;
}
}
outTxt.value = `"<a href='${attribution.pageURL}'>${attribution.pageTitle}</a>" from <a href='${attribution.bookURL}'>${attribution.bookTitle}</a> by ${attribution.author} is licensed under a <a href='${attribution.licenseURL}'>${attribution.licenseType}</a>, except where otherwise noted.`;
console.log(attribution);
document.getElementById('loading-dots').style.display = "none";
document.getElementById("error").style.display = "none";
document.getElementById("outputDiv").style.display = "block";
}
/**
* Extracts video details from the fetched data.
* @return {Object} - An object containing video attribution details.
*/
function getVideoAttribution() {
const title = doc.querySelector('meta[name="title"]').getAttribute('content');
const channel = doc.querySelector('link[itemprop="name"]').getAttribute('content');
const channelUrl = doc.querySelector('span[itemprop="author"] link[itemprop="url"]').getAttribute('href');
const durationMeta = doc.querySelector('meta[itemprop="duration"]').getAttribute('content');
const duration = formatDuration(durationMeta);
const attribution = new VideoAttribution();
attribution.videoURL = pageURL;
attribution.videoTitle = title;
attribution.channelTitle = channel;
attribution.channelURL = channelUrl;
attribution.duration = duration;
return attribution;
}
/**
* Formats the video duration from ISO 8601 duration format to "MM:SS" format.
* @param {string} duration - The ISO 8601 duration string (e.g., "PT5M19S").
* @return {string} - The formatted duration string (e.g., "5:19").
*/
function formatDuration(duration) {
const match = duration.match(/PT(\d+M)?(\d+S)?/);
const minutes = match[1] ? match[1].replace('M', '') : '0';
const seconds = match[2] ? match[2].replace('S', '') : '0';
return `${minutes}:${seconds.padStart(2, '0')}`;
}
// Copies contents of output textarea to the clipboard
function copy(id) {
let textarea = document.getElementById(id).value;
navigator.clipboard.writeText(textarea);
let resId = id + "CopyRes";
document.getElementById(resId).innerHTML = "Copied!";
};
function startup() {
document.getElementById("targetURL").addEventListener("keyup", (e) => {
if (e.key == "Enter") {
loadTargetPage();
}
})
}
</script>
<style>
body {
min-width: 250px;
background-image: url(images/fanshawe_wallpaper.jpg);
font-family: Arial, sans-serif;
}
h1 {
font-family: monospace, sans-serif;
}
.wrap {
width: 60%;
height: 77vh;
text-align: center;
background-color: #f7f5f5;
display: inline-block;
text-align: center;
margin-left: 20%;
margin-top: 1vh;
border-radius: 25px;
}
.child {
font-size: 1rem;
margin-bottom: 0.5rem;
}
button {
font-size: 1.5rem;
}
footer {
text-align: center;
color: #fff;
margin-top: 1rem;
}
footer a {
color: white;
}
#logo {
width: 74px;
}
#logo-div {
text-align: center;
margin-top: 2vh;
}
#resultTxtCopyRes {
margin-left: 1rem;
}
/* loading dots*/
@keyframes pulse {
from {
opacity: 1;
transform: scale(1);
}
to {
opacity: .25;
transform: scale(.75);
}
}
.spinner-box {
font-weight: bold;
color: #d42621;
margin-top: 40px;
}
.pulse-container {
width: 120px;
display: flex;
justify-content: space-between;
align-items: center;
margin: 0 auto;
}
.pulse-bubble {
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #df211c;
}
.pulse-bubble-1 {
animation: pulse .4s ease 0s infinite alternate;
}
.pulse-bubble-2 {
animation: pulse .4s ease .2s infinite alternate;
}
.pulse-bubble-3 {
animation: pulse .4s ease .4s infinite alternate;
}
</style>
</head>
<body onload="startup();">
<div class="wrap">
<h1>AttributionBuilder</h1>
<div class="child">
<input type="text" id="targetURL" size="50" onfocus="this.select();"
style="font-size: 1rem; max-width: 80%;" placeholder="Enter target URL" />
</div>
<div id='customInputDiv' style='display: none;'>
<div>
<input type="text" id="pageTitle" class="child" placeholder="page title" />
</div>
<div>
<input type="text" id="pageURL" class="child" placeholder="page URL" />
</div>
<div>
<input type="text" id="bookTitle" class="child" placeholder="book title" />
</div>
<div>
<input type="text" id="bookURL" class="child" placeholder="book URL" />
</div>
<div>
<input type="text" id="author" class="child" placeholder="author" />
</div>
<div>
<input type="text" id="licenceType" class="child" placeholder="licence type" />
</div>
<div>
<input type="text" id="licenceURL" class="child" placeholder="licence URL" />
</div>
</div>
<div class="child">
<button id="loadPageBtn" onClick="loadTargetPage();">Build Attribution</button>
</div>
<div id="loading-dots" class="spinner-box" style="display: none;">
<p>Work in progress</p>
<div class="pulse-container">
<div class="pulse-bubble pulse-bubble-1"></div>
<div class="pulse-bubble pulse-bubble-2"></div>
<div class="pulse-bubble pulse-bubble-3"></div>
</div>
<p>May take a few moments</p>
</div>
<div class="child">
<span id="error" style="display: none;"></span>
</div>
<div id="outputDiv" style="display: none;" class="child">
<textarea id="resultTxt" rows="4" cols="50"></textarea>
<div>
<button id="copyBtn" onclick="copy('resultTxt')">Copy to Clipboard</button>
</div>
<span id="resultTxtCopyRes"></span>
</div>
</div>
<div id="logo-div">
<img id="logo" src="images/oer_logo.png" />
</div>
<footer>
<div>AttributionBuilder by Jason Benoit, David Giesbrecht, Stephany Ceron and the Fanshawe OER Design Studio is
open source
software made available under the <a href="https://choosealicense.com/licenses/mit/">MIT License</a>.
</div>
<div>
Dots animation is based on <a href="https://codepen.io/AlexWarnes/pen/jXYYKL">work by Alex</a>
made available under the <a href="https://choosealicense.com/licenses/mit/">MIT License</a>.
</div>
</footer>
</body>
</html>