-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowImageScript.js
More file actions
81 lines (70 loc) · 2.51 KB
/
ShowImageScript.js
File metadata and controls
81 lines (70 loc) · 2.51 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
var image = null;
var container = null;
document.addEventListener("DOMContentLoaded", function() {
// Connect if API_Key is inserted
// Else show an error on the overlay
if (typeof API_Key === "undefined") {
document.body.innerHTML = "No API Key found or load!<br>Rightclick on the script in ChatBot and select \"Insert API Key\"";
}
else {
connectWebsocket();
}
})
// Connect to ChatBot websocket
// Automatically tries to reconnect on
// disconnection by recalling this method
function connectWebsocket() {
image = document.getElementById("image");
container = document.getElementById("container");
//-------------------------------------------
// Create WebSocket
//-------------------------------------------
var socket = new WebSocket(API_Socket);
//-------------------------------------------
// Websocket Event: OnOpen
//-------------------------------------------
socket.onopen = function() {
// AnkhBot Authentication Information
var auth = {
author: "Zensmann",
website: "https://twitch.tv/zensmann",
api_key: API_Key,
events: [
"SHOWIMAGE_IMAGE_IN",
"SHOWIMAGE_CONFIG"
]
};
// Send authentication data to ChatBot ws server
socket.send(JSON.stringify(auth));
};
//-------------------------------------------
// Websocket Event: OnMessage
//-------------------------------------------
socket.onmessage = function (message) {
// Parse message
var socketMessage = JSON.parse(message.data);
console.log(socketMessage);
if (socketMessage.event === "SHOWIMAGE_IMAGE_IN") {
var eventData = JSON.parse(socketMessage.data);
console.log(eventData);
image.src = eventData.url;
image.style.visibility = "visible";
container.style.backgroundColor = "#00000055";
}
};
//-------------------------------------------
// Websocket Event: OnError
//-------------------------------------------
socket.onerror = function(error) {
console.log("Error: " + error);
};
//-------------------------------------------
// Websocket Event: OnClose
//-------------------------------------------
socket.onclose = function() {
// Clear socket to avoid multiple ws objects and EventHandlings
socket = null;
// Try to reconnect every 5s
setTimeout(function(){connectWebsocket()}, 5000);
}
};