-
Notifications
You must be signed in to change notification settings - Fork 111
/
app.js
196 lines (160 loc) · 5.46 KB
/
app.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// This will use the demo backend if you open index.html locally via file://, otherwise your server will be used
let backendUrl = location.protocol === 'file:' ? "https://tiktok-chat-reader.zerody.one/" : undefined;
let connection = new TikTokIOConnection(backendUrl);
// Counter
let viewerCount = 0;
let likeCount = 0;
let diamondsCount = 0;
$(document).ready(() => {
$('#connectButton').click(connect);
$('#uniqueIdInput').on('keyup', function (e) {
if (e.key === 'Enter') {
connect();
}
});
})
function connect() {
let uniqueId = $('#uniqueIdInput').val();
if (uniqueId !== '') {
$('#stateText').text('Connecting...');
connection.connect(uniqueId, {
enableExtendedGiftInfo: true
}).then(state => {
$('#stateText').text(`Connected to roomId ${state.roomId}`);
// reset stats
viewerCount = 0;
likeCount = 0;
diamondsCount = 0;
updateRoomStats();
}).catch(errorMessage => {
$('#stateText').text(errorMessage);
})
} else {
alert('no username entered');
}
}
// Prevent Cross site scripting (XSS)
function sanitize(text) {
return text.replace(/</g, '<')
}
function updateRoomStats() {
$('#roomStats').html(`Viewers: <b>${viewerCount.toLocaleString()}</b> Likes: <b>${likeCount.toLocaleString()}</b> Earned Diamonds: <b>${diamondsCount.toLocaleString()}</b>`)
}
function generateUsernameLink(data) {
return `<a class="usernamelink" href="https://www.tiktok.com/@${data.uniqueId}" target="_blank">${data.uniqueId}</a>`;
}
function isPendingStreak(data) {
return data.giftType === 1 && !data.repeatEnd;
}
/**
* Add a new message to the chat container
*/
function addChatItem(color, data, text, summarize) {
let container = $('.chatcontainer');
if (container.find('div').length > 500) {
container.find('div').slice(0, 200).remove();
}
container.find('.temporary').remove();;
container.append(`
<div class=${summarize ? 'temporary' : 'static'}>
<img class="miniprofilepicture" src="${data.profilePictureUrl}">
<span>
<b>${generateUsernameLink(data)}:</b>
<span style="color:${color}">${sanitize(text)}</span>
</span>
</div>
`);
container.stop();
container.animate({
scrollTop: container[0].scrollHeight
}, 400);
}
/**
* Add a new gift to the gift container
*/
function addGiftItem(data) {
let container = $('.giftcontainer');
if (container.find('div').length > 200) {
container.find('div').slice(0, 100).remove();
}
let streakId = data.userId.toString() + '_' + data.giftId;
let html = `
<div data-streakid=${isPendingStreak(data) ? streakId : ''}>
<img class="miniprofilepicture" src="${data.profilePictureUrl}">
<span>
<b>${generateUsernameLink(data)}:</b> <span>${data.describe}</span><br>
<div>
<table>
<tr>
<td><img class="gifticon" src="${data.giftPictureUrl}"></td>
<td>
<span>Name: <b>${data.giftName}</b> (ID:${data.giftId})<span><br>
<span>Repeat: <b style="${isPendingStreak(data) ? 'color:red' : ''}">x${data.repeatCount.toLocaleString()}</b><span><br>
<span>Cost: <b>${(data.diamondCount * data.repeatCount).toLocaleString()} Diamonds</b><span>
</td>
</tr>
</tabl>
</div>
</span>
</div>
`;
let existingStreakItem = container.find(`[data-streakid='${streakId}']`);
if (existingStreakItem.length) {
existingStreakItem.replaceWith(html);
} else {
container.append(html);
}
container.stop();
container.animate({
scrollTop: container[0].scrollHeight
}, 800);
}
// viewer stats
connection.on('roomUser', (msg) => {
if (typeof msg.viewerCount === 'number') {
viewerCount = msg.viewerCount;
updateRoomStats();
}
})
// like stats
connection.on('like', (msg) => {
if (typeof msg.likeCount === 'number') {
addChatItem('#447dd4', msg, msg.label.replace('{0:user}', '').replace('likes', `${msg.likeCount} likes`))
}
if (typeof msg.totalLikeCount === 'number') {
likeCount = msg.totalLikeCount;
updateRoomStats();
}
})
// Member join
let joinMsgDelay = 0;
connection.on('member', (msg) => {
let addDelay = 250;
if (joinMsgDelay > 500) addDelay = 100;
if (joinMsgDelay > 1000) addDelay = 0;
joinMsgDelay += addDelay;
setTimeout(() => {
joinMsgDelay -= addDelay;
addChatItem('#21b2c2', msg, 'joined', true);
}, joinMsgDelay);
})
// New chat comment received
connection.on('chat', (msg) => {
addChatItem('', msg, msg.comment);
})
// New gift received
connection.on('gift', (data) => {
addGiftItem(data);
if (!isPendingStreak(data) && data.diamondCount > 0) {
diamondsCount += (data.diamondCount * data.repeatCount);
updateRoomStats();
}
})
// share, follow
connection.on('social', (data) => {
let color = data.displayType.includes('follow') ? '#ff005e' : '#2fb816';
addChatItem(color, data, data.label.replace('{0:user}', ''));
})
connection.on('streamEnd', () => {
$('#stateText').text('Stream ended.');
})