-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
88 lines (76 loc) · 2.49 KB
/
script.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
window.Twitch.ext.onAuthorized((auth) => {
if (window.Twitch.ext.viewer.isLinked) {
// user is logged in/ID Shared
document.body.classList.add('logged_in');
document.body.classList.remove('logged_out');
loadProfile();
} else {
document.body.classList.remove('logged_in');
document.body.classList.add('logged_out');
}
});
var last_theme = ''
window.Twitch.ext.onContext((ctx) => {
var new_theme = ctx.theme;
if (last_theme != new_theme) {
document.body.classList.remove('twitch_light');
document.body.classList.remove('twitch_dark');
document.body.classList.add('twitch_' + new_theme);
}
last_theme = new_theme;
});
document.getElementById('share').addEventListener('click', (e) => {
e.preventDefault();
window.Twitch.ext.actions.requestIdShare();
});
function loadProfile() {
document.getElementById('error').textContent = 'Loading';
fetch(
my_ebs,
{
method: 'POST',
headers: {
authorization: 'Bearer ' + window.Twitch.ext.viewer.sessionToken
}
}
)
.then(resp => {
return resp.json();
})
.then(resp => {
if (resp.error) {
document.getElementById('error').textContent = 'Got Error' + resp.message;
return;
}
document.getElementById('error').textContent = 'Got profile';
document.getElementById('logged_in').textContent = '';
var tbl = document.createElement('table');
document.getElementById('logged_in').append(tbl);
for (var key in resp.data) {
var r = document.createElement('tr');
tbl.append(r);
var d = document.createElement('th');
r.append(d);
d.textContent = key;
var d = document.createElement('td');
r.append(d);
switch (key) {
case 'profile_image_url':
case 'offline_image_url':
var i = document.createElement('img');
i.setAttribute('src', resp.data[key]);
d.append(i);
break;
default:
d.textContent = resp.data[key];
}
}
})
.catch(err => {
if (err.message) {
document.getElementById('error').textContent = err.message;
} else {
document.getElementById('error').textContent = 'An Error Occured';
}
});
}