-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.js
150 lines (118 loc) · 4.07 KB
/
irc.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
143
144
145
146
147
148
149
150
const client_id = '4ofh8m0706jqpholgk00u3xvb4spct';
const scopes = 'chat:read chat:edit';
const redirectURI = 'https://xemdo.github.io/twitch-websocket-tester/irc.html';
const websocketUrl = 'wss://irc-ws.chat.twitch.tv:443';
var access_token;
var socket;
var user_id;
var username;
window.addEventListener('load', () => {
linkAuthUrl();
document.getElementById('btnClose').disabled = true;
// Check for authorization
const urlParams = new URLSearchParams(window.location.search);
if (document.location.hash != '') {
document.getElementById('connect').style.visibility = 'visible';
let r = document.location.hash.match(/.*access_token=(.[^&]+).*scope=(.[^&]+).*/);
access_token = r[1];
}
});
function linkAuthUrl() {
let url =
'https://id.twitch.tv/oauth2/authorize' +
'?response_type=token' +
'&client_id=' + client_id +
'&redirect_uri=' + encodeURIComponent(redirectURI) +
'&scope=' + encodeURIComponent(scopes);
document.getElementById('authorize').href = url;
}
function connectWebsocket() {
socket = new WebSocket(websocketUrl);
socket.addEventListener('open', async (event) => {
writeLog('Connected to ' + websocketUrl);
document.getElementById("btnConnect").disabled = true;
document.getElementById("btnClose").disabled = false;
await getUserID();
if (document.getElementById('chkSendReq').checked) {
sendMessage('CAP REQ :twitch.tv/membership twitch.tv/tags twitch.tv/commands');
}
if (document.getElementById('chkSendPassNick').checked) {
sendMessage('PASS oauth:' + access_token);
sendMessage('NICK ' + username);
}
});
socket.addEventListener('close', (event) => {
socket = undefined;
writeLog('Websocket closed; wasClean[' + event.wasClean + ']; code[' + event.code + ']; Reason: ' + event.reason);
document.getElementById("btnConnect").disabled = false;
document.getElementById("btnClose").disabled = true;
});
socket.addEventListener('message', (event) => {
data = event.data;
if (document.getElementById("chkNewlinePlaintext").checked) {
data = data.replaceAll("\r", "\\r").replaceAll("\n", "\\n");
}
writeLog(data, "RECEIVE")
// Handle ping/pong
if (data.startsWith("PING :tmi.twitch.tv")) {
sendMessage("PONG :tmi.twitch.tv");
}
//data = JSON.parse(event.data);
});
}
function disconnectWebsocket() {
console.log("Socket already closed");
if (socket == undefined) {
return;
}
socket.close();
socket = undefined;
session_id = undefined;
writeLog('Socket manually closed');
}
function sendPayload() {
var lines = document.getElementById("payload").value.split('\n');
for (var i = 0; i < lines.length; i++) {
sendMessage(lines[i]);
}
document.getElementById("payload") = '';
}
function sendMessage(message) {
socket.send(message);
writeLog(message, "SEND");
}
function timestamp() {
return new Date(Date.now()).toISOString();
}
function writeLog(msg, direction) {
let log = document.getElementById('log');
let msgNode = document.createElement('span');
let brNode = document.createElement('br');
if (direction == undefined) {
msgNode.innerText = '[' + timestamp() + '] ' + msg;
} else {
msgNode.innerText = '[' + timestamp() + ' // ' + direction + '] ' + msg;
}
log.appendChild(msgNode);
log.appendChild(brNode);
}
function clearLog() {
let log = document.getElementById('log');
log.innerHTML = '';
}
async function getUserID() {
await fetch('https://id.twitch.tv/oauth2/validate', {
method: "GET",
mode: "cors",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
"Authorization": "OAuth " + access_token
}
})
.then(async (response) => {
data = await response.json();
user_id = data.user_id;
username = data.login;
});
}