-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitchEmbed.js
More file actions
92 lines (79 loc) · 2.59 KB
/
Copy pathtwitchEmbed.js
File metadata and controls
92 lines (79 loc) · 2.59 KB
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
document.addEventListener("DOMContentLoaded", function () {
const body = document.body;
const twitchEmbed = document.getElementById("twitchEmbedId");
if (!twitchEmbed) return;
const params = new URLSearchParams(window.location.search);
const clipId = params.get("clipId");
if (!clipId || clipId.trim() === "") {
console.warn("Kein 'clipId' Parameter in der URL gefunden.");
const divErrorElement = document.createElement("div");
Object.assign(divErrorElement.style, {
background: "#000000",
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
display: "flex",
alignItems: "center",
alignContent: "center",
justifyItems: "center",
justifyContent: "center",
border: "2px solid black",
borderRadius: "5px",
padding: "5px 5px",
margin: "10px 10px 10px 10px"
});
body.appendChild(divErrorElement);
const pErrorElement = document.createElement("p");
Object.assign(pErrorElement.style, {
fontFamily: "Arial, Helvetica, sans-serif",
background: "#000000",
display: "flex",
alignItems: "center",
alignContent: "center",
justifyItems: "center",
justifyContent: "center",
border: "1px solid transparent",
borderRadius: "5px",
padding: "5px 5px",
margin: "5px 5px 5px 5px",
textAlign: "center",
fontSize: "25px",
color: "#ffffff",
textDecoration: "none"
});
pErrorElement.innerHTML =
"Bitte gib eine Clip-ID an!<br>?clipId=1234&autoplay=true&muted=true";
divErrorElement.appendChild(pErrorElement);
return;
}
let autoplay = localStorage.getItem("autoplay");
let muted = localStorage.getItem("muted");
if (params.has("autoplay")) {
autoplay = params.get("autoplay") === "true";
localStorage.setItem("autoplay", autoplay);
} else if (autoplay !== null) {
autoplay = autoplay === "true";
} else {
autoplay = false;
localStorage.setItem("autoplay", autoplay);
}
if (params.has("muted")) {
muted = params.get("muted") === "true";
localStorage.setItem("muted", muted);
} else if (muted !== null) {
muted = muted === "true";
} else {
muted = true;
localStorage.setItem("muted", muted);
}
const domain = window.location.hostname;
const embedParams = new URLSearchParams({
clip: clipId,
parent: domain
});
if (autoplay) embedParams.set("autoplay", "true");
if (muted) embedParams.set("muted", "true");
const embedUrl = `https://clips.twitch.tv/embed/?${embedParams.toString()}`;
twitchEmbed.src = embedUrl;
});