aboutsummaryrefslogtreecommitdiff
path: root/Info/9/3/Planet/templates/h5p-autoembed.js
blob: 8c5128e82cc218d5c7a6463e54bdf5dfbba461b3 (plain)
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
const autoEmbedLinksDivs = document.querySelectorAll(".h5p-autoembed");
let loadAutoResizerJs = false;
for (const div of autoEmbedLinksDivs) {
	const autoEmbedLink = div.querySelector("a.autolink");
	const autoEmbedUrl = new URL(autoEmbedLink.href);
	const autoEmbedId = autoEmbedUrl.searchParams.get("id");
	const iframe = document.createElement('iframe');
	// Concatenate the URL to prevent Moodle's auto link feature to mess with the url
	iframe.src = "https://" + "lernplattform.bycs.de/mod/hvp/embed.php?id=" + autoEmbedId;
	iframe.frameborder = div.frameborder ? div.frameborder : 0;
	iframe.allowfullscreen = div.allowfullscreen ? div.allowfullscreen : "allowfullscreen";
	// Lazily load the iframe
	iframe.loading = "lazy";
	iframe.title = autoEmbedLink.title;
	if (div.width) {
		iframe.width = div.width;
	} else {
		loadAutoResizerJs = true;
	}
	if (div.height) {
		iframe.height = div.height;
	} else {
		loadAutoResizerJs = true;
	}

	// Replace the div with the new iframe
	div.parentNode.replaceChild(iframe, div);
}

if (loadAutoResizerJs) {
	let head = document.getElementsByTagName('head')[0];
	let js = document.createElement("script");
	js.type = "text/javascript";
	js.src="https://lernplattform.bycs.de/mod/hvp/library/js/h5p-resizer.js";
	head.appendChild(js);
}

async function embedScorm() {
	for (const div of document.querySelectorAll(".scorm-autoembed")) {
		const autoEmbedLink = div.querySelector("a.autolink");
		const autoEmbedUrl = new URL(autoEmbedLink.href);

		const scormActivityRes = await fetch(autoEmbedUrl);
		const scormActivityDoc = new DOMParser().parseFromString(await scormActivityRes.text(), "text/html");

		const s = document.createElement("script");
		s.text = scormActivityDoc.scripts[4].text;
		s.setAttribute("type", "text/javascript");
		document.body.appendChild(s);

		const iframe = document.createElement('iframe');
		// Concatenate the URL to prevent Moodle's auto link feature to mess with the url
		iframe.src = `/mod/scorm/player.php?a=$${scormplayerdata.scorm}&currentorg=$${scormplayerdata.currentorg}&scoid=$${scormplayerdata.sco}&sesskey=$${M.cfg.sesskey}&display=popup&mode=normal`
		iframe.frameborder = div.frameborder ? div.frameborder : 0;
		iframe.allowfullscreen = div.allowfullscreen ? div.allowfullscreen : "allowfullscreen";
		iframe.title = autoEmbedLink.title;
		if (div.style.width) {
			iframe.width = div.style.width;
		}
		if (div.style.height) {
			iframe.height = div.style.height;
		}

		// Replace the div with the new iframe
		div.parentNode.replaceChild(iframe, div);
	}
}

embedScorm();