-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathviewer.js
76 lines (61 loc) · 1.88 KB
/
viewer.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
var token = "";
var tuid = "";
var ebs = "";
// because who wants to type this every time?
var twitch = window.Twitch.ext;
// create the request options for our Twitch API calls
var requests = {
set: createRequest('POST', 'cycle'),
get: createRequest('GET', 'query')
};
function createRequest(type, method) {
return {
type: type,
url: 'https://localhost:8081/color/' + method,
success: updateBlock,
error: logError
}
}
function setAuth(token) {
Object.keys(requests).forEach((req) => {
twitch.rig.log('Setting auth headers');
requests[req].headers = { 'Authorization': 'Bearer ' + token }
});
}
twitch.onContext(function(context) {
twitch.rig.log(context);
});
twitch.onAuthorized(function(auth) {
// save our credentials
token = auth.token;
tuid = auth.userId;
// enable the button
$('#cycle').removeAttr('disabled');
setAuth(token);
$.ajax(requests.get);
});
function updateBlock(hex) {
twitch.rig.log('Updating block color');
$('#color').css('background-color', hex);
}
function logError(_, error, status) {
twitch.rig.log('EBS request returned '+status+' ('+error+')');
}
function logSuccess(hex, status) {
// we could also use the output to update the block synchronously here,
// but we want all views to get the same broadcast response at the same time.
twitch.rig.log('EBS request returned '+hex+' ('+status+')');
}
$(function() {
// when we click the cycle button
$('#cycle').click(function() {
if(!token) { return twitch.rig.log('Not authorized'); }
twitch.rig.log('Requesting a color cycle');
$.ajax(requests.set);
});
// listen for incoming broadcast message from our EBS
twitch.listen('broadcast', function (target, contentType, color) {
twitch.rig.log('Received broadcast color');
updateBlock(color);
});
});