-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
142 lines (122 loc) · 3.69 KB
/
main.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
async function fetchUserId(login, clientid) {
try {
const response = await fetch(
`https://api.twitch.tv/helix/users?login=${login}`,
{
headers: { "Client-ID": clientid },
}
);
const json = await response.json();
return json.data[0].id;
} catch (err) {
console.error(err);
}
}
async function fetchFollowerIds(userId, clientId) {
try {
const response = await fetch(
`https://api.twitch.tv/helix/users/follows?to_id=${userId}&first=100`,
{
headers: { "Client-ID": clientId },
}
);
const json = await response.json();
return json.data.map((x) => x.from_id);
} catch (err) {
console.error(err);
}
}
async function fetchFollowerDetails(followerIds, clientid) {
try {
const followersString = followerIds.map((id) => `id=${id}`).join("&");
const response = await fetch(
`https://api.twitch.tv/helix/users?${followersString}`,
{
headers: { "Client-ID": clientid },
}
);
const json = await response.json();
return json.data;
} catch (err) {
console.error(err);
}
}
function configureSound(name, volume) {
const sound = document.getElementById("alert-sound");
sound.src = `sounds/${name}.wav`;
sound.volume = volume / 100;
return sound;
}
function buildAlert(detail) {
return `
<div class="alert">
<img
class="profile-picture"
src="${detail.profile_image_url}"
/>
<div class="name-group">
<div class="followed-by">🤍 Followed By</div>
<div class="display-name">${detail.display_name}</div>
</div>
</div>`;
}
async function startOverlay() {
try {
const params = new URLSearchParams(location.search);
const login = params.get("login") || "joekombo";
const sound = params.get("sound") || "guitar";
const volume = Number(params.get("volume") || "50");
const clientId = params.get("clientid");
const gif = params.get("gif") || "guitarkitty";
const soundClip = configureSound(sound, volume);
const userId = await fetchUserId(login, clientId);
const initialFollowerIds = await fetchFollowerIds(userId, clientId);
if (!clientId) {
document.body.innerHTML = `
<div class='instructions'>
<h1>MISSING CLIENT ID</h1>
<p>Go to <a href="https://github.com/joekombo/twitch-alert-overlay">
https://github.com/joekombo/twitch-alert-overlay</a> for detailed instructions.</p>
</div>
`;
return;
}
let knownFollowersIds = initialFollowerIds;
setInterval(async () => {
const followerIds = await fetchFollowerIds(userId, clientId);
let newFollowersIds = [];
followerIds.forEach((id) => {
if (!knownFollowersIds.includes(id)) {
knownFollowersIds.push(id);
newFollowersIds.push(id);
}
});
if (newFollowersIds.length) {
const followerDetails = await fetchFollowerDetails(
newFollowersIds,
clientId
);
let remainingDetails = followerDetails;
let alertLoopInterval = setInterval(() => {
let first15Alerts = remainingDetails
.splice(0, 15)
.map((detail) => buildAlert(detail));
soundClip.play();
document.getElementById("alerts").innerHTML = `
<div id='alerts-frame'>
${first15Alerts.join("")}
</div>
<img id='gif' src='images/${gif}.gif' />
`;
if (remainingDetails.length <= 0) {
clearInterval(alertLoopInterval);
}
console.log(`Remaining alerts: ${remainingDetails.length}`);
}, 5000);
}
}, 15000);
} catch (err) {
console.error(err);
}
}
startOverlay();