-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigurator.js
More file actions
385 lines (360 loc) · 19.4 KB
/
configurator.js
File metadata and controls
385 lines (360 loc) · 19.4 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
var chatParams = new ChatParameters();
chatParams.possibleColors = chatParams.possibleColors.filter(function(value, index, arr){ return value !== "green"; });
chatParams.globalBadges = {"badge_sets":
{"broadcaster":
{"versions":
{"1":
{
"image_url_1x": "https://static-cdn.jtvnw.net/badges/v1/5527c58c-fb7d-422d-b71b-f309dcb85cc1/1",
"image_url_2x": "https://static-cdn.jtvnw.net/badges/v1/5527c58c-fb7d-422d-b71b-f309dcb85cc1/2",
"image_url_4x": "https://static-cdn.jtvnw.net/badges/v1/5527c58c-fb7d-422d-b71b-f309dcb85cc1/4"
}
}
},
"moderator":
{"versions":
{"1":
{
"image_url_1x": "https://static-cdn.jtvnw.net/badges/v1/3267646d-33f0-4b17-b3df-f923a41db1d0/1",
"image_url_2x": "https://static-cdn.jtvnw.net/badges/v1/3267646d-33f0-4b17-b3df-f923a41db1d0/2",
"image_url_4x": "https://static-cdn.jtvnw.net/badges/v1/3267646d-33f0-4b17-b3df-f923a41db1d0/4"
}
}
},
"premium":
{"versions":
{"1":
{
"image_url_1x": "https://static-cdn.jtvnw.net/badges/v1/a1dd5073-19c3-4911-8cb4-c464a7bc1510/1",
"image_url_2x": "https://static-cdn.jtvnw.net/badges/v1/a1dd5073-19c3-4911-8cb4-c464a7bc1510/2",
"image_url_4x": "https://static-cdn.jtvnw.net/badges/v1/a1dd5073-19c3-4911-8cb4-c464a7bc1510/4"
}
}
}
}
};
chatParams.channelInfo = {
"zensmann" : {
"badges": { "badge_sets": {} },
"display": "Zensmann",
"id": "123456789",
"image": "https://static-cdn.jtvnw.net/jtv_user_pictures/f3a6e5dd-903c-4be7-899c-48de3f12319b-profile_image-300x300.png"
},
"otherchannel" : {
"badges": { "badge_sets": {} },
"display": "OtherChannel",
"id": "123456789",
"image": "https://static-cdn.jtvnw.net/user-default-pictures-uv/41780b5a-def8-11e9-94d9-784f43822e80-profile_image-300x300.png"
},
"anotherchannel" : {
"badges": { "badge_sets": {} },
"display": "AnotherChannel",
"id": "123456789",
"image": "https://static-cdn.jtvnw.net/user-default-pictures-uv/de130ab0-def7-11e9-b668-784f43822e80-profile_image-300x300.png"
}
};
var lastAnimationTimestamp;
var currentAnimationState;
function resizeCallback() {
var sizeField = document.getElementById("previewSize");
var prev = document.getElementById("preview");
var width = parseInt(window.getComputedStyle(prev, null).getPropertyValue("width"));
var height = parseInt(window.getComputedStyle(prev, null).getPropertyValue("height"));
sizeField.innerText = width + " x " + height;
}
function ParseInputs() {
var splits = document.getElementById("channelsInput").value.toLowerCase().split(/[;,]/)
if (splits[0] !== "") {
chatParams.passedChannelNames = splits;
}
else {
chatParams.passedChannelNames = [];
}
// general
//chatParams.channels = document.getElementById("channelsInput").value;
chatParams.maxmessages = parseInt(document.getElementById("maxmessagesInput").value);
chatParams.background = document.getElementById("usebackgroundcolorInput").checked ? document.getElementById("previewColorInput").value : null;
splits = document.getElementById("ignoreInput").value.toLowerCase().split(/[;,]/)
if (splits[0] !== "") {
chatParams.hideList = splits;
}
else {
chatParams.hideList = null;
}
chatParams.hideBotCommands = document.getElementById("hideCommandsInput").checked;
chatParams.hideURL = document.getElementById("hideUrlsInput").checked;
// messages
chatParams.fontfamily = document.getElementById("fontSelector").value;
chatParams.fontsize = parseInt(document.getElementById("fontsizeInput").value);
chatParams.fontcolor = document.getElementById("textColorInput").value;
chatParams.textAlign = document.getElementById("textalign").value;
chatParams.coloredactions = document.getElementById("coloredActions").checked;
chatParams.emotescale = parseFloat(document.getElementById("emotescaleInput").value);
chatParams.space = parseInt(document.getElementById("spaceInput").value);
chatParams.spaceAround = parseInt(document.getElementById("spaceAroundInput").value);
chatParams.wraparoundname = document.getElementById("wrapText").checked;
chatParams.channelboxspaceright = parseInt(document.getElementById("horizSpaceInput").value);
chatParams.channelboxspacebottom = parseInt(document.getElementById("verticSpaceInput").value);
chatParams.namespace = parseInt(document.getElementById("namespaceInput").value);
// sender
chatParams.showbadges = document.getElementById("showbadgesInput").checked;
chatParams.badgespace = parseInt(document.getElementById("badgespaceInput").value);
var usesenderstyle = document.getElementById("senderstyle").checked;
chatParams.senderfamily = usesenderstyle ? document.getElementById("senderFontSelector").value : null;
chatParams.sendersize = usesenderstyle ? parseInt(document.getElementById("sendersizeInput").value) : null;
chatParams.senderAlign = document.getElementById("senderalign").value;
// channel
chatParams.showrecipient = document.getElementById("showrecipientInput").checked;
chatParams.showrecipientimage = document.getElementById("showchannelImage").checked;
var userecipientstyle = document.getElementById("recipientstyle").checked;
chatParams.recipientfamily = userecipientstyle ? document.getElementById("recipientFontSelector").value : null;
chatParams.recipientsize = userecipientstyle ? parseInt(document.getElementById("recipientsizeInput").value) : null;
chatParams.recipientcolor = userecipientstyle ? document.getElementById("recipientcolorInput").value : null;
chatParams.channelAlign = document.getElementById("channelalign").value;
// highlighted messages
var highlightmessages = document.getElementById("highlight").checked;
chatParams.highlightMessageColor = highlightmessages ? document.getElementById("highlightfontcolorInput").value : null;
if (highlightmessages) {
splits = document.getElementById("highlightOnlyInput").value.toLowerCase().split(/[;,]/)
if (splits[0] !== "") {
chatParams.highlightOnly = splits;
}
else {
chatParams.highlightOnly = null;
}
}
else {
chatParams.highlightOnly = null;
}
console.log(chatParams.highlightOnly);
chatParams.highlightMessageBackground = highlightmessages ? document.getElementById("highlightbackgroundcolorInput").value : null;
chatParams.highlightTransparency = 255 - Math.round(parseInt(document.getElementById("highlighttransparencyInput").value) / 100. * 255);
// animation
chatParams.fadetime = parseFloat(document.getElementById("fadetime").value);
chatParams.showtime = parseFloat(document.getElementById("showtime").value);
}
function GenerateMessageInfo(channel, color, display_name, emotes, message_type, mod, msg_id, subscriber, turbo, premium, message) {
var username = display_name.toLowerCase();
var broadcaster = (channel.substring(1) === username);
var badges = {};
if (broadcaster) {
badges["broadcaster"] = "1";
}
else if (mod) {
badges["moderator"] = "1";
}
if (subscriber > 0) {
badges["subscriber"] = subscriber.toString();
}
if (premium) {
badges["premium"] = "1";
}
return {"channel": channel,
"userstate": {"badge-info": null, "badge-info-raw": null, "badges": badges, "badges-raw": "", "color": color, "display-name": display_name, "emotes": emotes, "emotes-raw": "", "flags": null, "id": "", "message-type": message_type, "mod": (broadcaster ? false : mod), "msg-id": msg_id, "room-id": "", "subscriber": (subscriber > 0), "tmi-sent-ts": "", "turbo": turbo, "user-id": "", "user-type": (broadcaster ? "broadcaster" : (mod ? "mod" : "")), "username": username},
"message": message,
"self": broadcaster};
}
function GeneratePreview() {
var content = document.getElementById("preview");
while (content.firstChild) {
content.removeChild(content.firstChild);
}
content.style = "background:" + document.getElementById("previewColorInput").value + ";font-size:" + chatParams.fontsize + "px";
var msgs =[];
for (var i = chatParams.maxmessages; i > 9; --i) {
msgs.push(["chat", GenerateMessageInfo("#zensmann", "#DAA520", "Zensmann", null, "chat", false, null, 0, false, false, "This is text message #" + i + ".")]);
}
msgs.push(["action", GenerateMessageInfo("#zensmann", "#1E90FF", "Zens_bot", null, "action", false, null, 0, false, false, "This is a response from the bot")]);
msgs.push(["chat", GenerateMessageInfo("#zensmann", "#DAA520", "Zensmann", null, "chat", false, null, 0, false, false, "!botcommand")]);
msgs.push(["chat", GenerateMessageInfo("#zensmann", "#DAA520", "Zensmann", null, "chat", false, null, 0, false, false, "This is a test message with a URL: https://twitch.tv/zensmann")]);
msgs.push(["action", GenerateMessageInfo("#zensmann", "#DAA520", "Zensmann", null, "action", false, null, 0, false, false, "shows a test action starting with /me")]);
msgs.push(["chat", GenerateMessageInfo("#anotherchannel", "#DAA520", "Zensmann", null, "chat", true, "highlighted-message", 0, false, false, "This is a highlighted test message.")]);
msgs.push(["chat", GenerateMessageInfo("#otherchannel", null, "AnotherChatter", null, "chat", true, null, 0, false, true, "This is a test message by another chatter in another channel chat, where he or she is a mod.")]);
msgs.push(["chat", GenerateMessageInfo("#otherchannel", "#DAA520", "Zensmann", null, "chat", false, "highlighted-message", 0, false, false, "This is a very very very long test message that does not contain any emotes. Line wrapping will occur normally and the lines will not spread any further, since there are no emotes included in this text. However this message does come from myself in a channel chat, where I do not have mod rights.")]);
msgs.push(["chat", GenerateMessageInfo("#zensmann", null, "OtherChatter", {"25": ["36-40"], "425618": ["93-95"]}, "chat", true, null, 0, false, false, "This is a long text message with an Kappa emote included. Furthermore I included another one LUL just for the lols. It comes from another chatter in my own chatroom. For demonstration purposes I assigned him both badges, so you can see how multiple badges are behaving.")]);
msgs.push(["chat", GenerateMessageInfo("#zensmann", "#DAA520", "Zensmann", null, "chat", false, null, 0, false, false, "This is a test message by myself in my own chat.")]);
for (var i = Math.max(0, msgs.length - chatParams.maxmessages); i < msgs.length; ++i) {
if (msgs[i][0] === "chat") {
var msgBox = chatParams.createMessageContainer(msgs[i][1]["channel"], msgs[i][1]["userstate"], msgs[i][1]["message"]);
if (msgBox !== null) {
content.appendChild(msgBox);
}
}
else if (msgs[i][0] === "action") {
var msgBox = chatParams.createMessageContainer(msgs[i][1]["channel"], msgs[i][1]["userstate"], msgs[i][1]["message"]);
if (msgBox !== null) {
content.appendChild(msgBox);
}
}
else {
console.log("Unrecognized event format: " + msgs[i][0]);
}
}
}
function UpdateView() {
ParseInputs();
var userecipientstyle = document.getElementById("recipientstyle").checked;
var usesenderstyle = document.getElementById("senderstyle").checked;
var highlightmessages = document.getElementById("highlight").checked;
document.getElementById("showchannelImage").disabled = !chatParams.showrecipient;
document.getElementById("recipientstyle").disabled = !chatParams.showrecipient;
document.getElementById("recipientcolorInput").disabled = !chatParams.showrecipient || !userecipientstyle;
document.getElementById("recipientsizeInput").disabled = !chatParams.showrecipient || !userecipientstyle;
document.getElementById("channelalign").disabled = !chatParams.showrecipient;
document.getElementById("recipientFontSelector").disabled = !chatParams.showrecipient || !userecipientstyle;
document.getElementById("highlightfontcolorInput").disabled = !highlightmessages;
document.getElementById("highlightbackgroundcolorInput").disabled = !highlightmessages;
document.getElementById("highlighttransparencyInput").disabled = !highlightmessages;
document.getElementById("highlightOnlyInput").disabled = !highlightmessages;
document.getElementById("badgespaceInput").disabled = !chatParams.showbadges;
document.getElementById("sendersizeInput").disabled = !usesenderstyle;
document.getElementById("senderFontSelector").disabled = !usesenderstyle;
document.getElementById("namespaceInput").disabled = chatParams.wraparoundname;
document.getElementById("verticSpaceInput").disabled = !chatParams.wraparoundname;
var selectedFontIndex = document.getElementById("fontSelector").selectedIndex;
if (!usesenderstyle) {
document.getElementById("sendersizeInput").value = chatParams.fontsize;
document.getElementById("senderFontSelector").selectedIndex = selectedFontIndex;
}
if (!userecipientstyle) {
document.getElementById("recipientsizeInput").value = chatParams.fontsize;
document.getElementById("recipientFontSelector").selectedIndex = selectedFontIndex;
document.getElementById("recipientcolorInput").value = chatParams.fontcolor;
}
var url = window.location.href.replace("configurator.html", "");
url = window.location.href.replace("configurator", "");
if (url.startsWith("file:///")) {
url += "index.html"
}
var stateString = chatParams.createStyleString();
if (stateString !== "") {
url += "?state=" + stateString;
}
document.getElementById("url").innerText = url;
if (chatParams.passedChannelNames.length == 0) {
document.getElementById("urllink").href = "#";
document.getElementById("urllink").removeAttribute("target");
}
else {
document.getElementById("urllink").href = url;
document.getElementById("urllink").target = "_blank";
}
GeneratePreview();
resizeCallback();
var content = document.getElementById("preview");
for (var i = 0; i < content.children.length - 1; ++i) {
content.children[i].style.opacity = "1.0";
}
lastAnimationTimestamp = Date.now();
currentAnimationState = 1;
}
function CopyButton() {
var tempText = document.createElement("textarea");
var url = document.getElementById("url").innerText;
tempText.value = url;
tempText.setAttribute("readonly", "");
tempText.style = "position:absolute;left:-9999px";
document.body.appendChild(tempText);
tempText.select();
document.execCommand("copy");
document.body.removeChild(tempText);
alert("Copied following link to the clipboard:\n" + url);
}
function animateLastMessage() {
var now = Date.now();
var showtimeSet = chatParams.showtime != 0;
var fadetimeSet = chatParams.fadetime != 0;
var content = document.getElementById("preview");
var lastChild = content.children[content.children.length - 1];
if (currentAnimationState == 0 && (now - lastAnimationTimestamp) >= 3000) { // object is hidden
lastChild.style.opacity = "1.0";
lastAnimationTimestamp = now;
if (fadetimeSet) {
currentAnimationState = 1;
}
else {
currentAnimationState = 2;
}
}
if (currentAnimationState == 1 && (now - lastAnimationTimestamp) >= chatParams.fadetime * 1000) { // object is fading in
lastAnimationTimestamp = now;
currentAnimationState = 2;
}
else if (currentAnimationState == 2 && (now - lastAnimationTimestamp) >= (showtimeSet ? chatParams.showtime : 5) * 1000) { // object is visible
if (fadetimeSet) {
lastChild.style.opacity = "0.0";
currentAnimationState = 3;
}
else {
lastChild.classList.add("notransition");
lastChild.style.opacity = "0.0";
lastChild.offsetHeight;
lastChild.classList.remove("notransition");
currentAnimationState = 0;
}
lastAnimationTimestamp = now;
}
else if (currentAnimationState == 3 && (now - lastAnimationTimestamp) >= chatParams.fadetime * 1000) { // object is fading out
lastChild.style.opacity = "0.0";
currentAnimationState = 0;
}
setTimeout(animateLastMessage, 10);
}
function InitFonts() {
var fontRequest = new XMLHttpRequest();
fontRequest.onreadystatechange = function() {
if (fontRequest.readyState == 4) {
if (fontRequest.status == 200) {
var fonts = JSON.parse(fontRequest.responseText);
var cssLinkBase = "https://fonts.googleapis.com/css2?";
var generalFont = document.getElementById("fontSelector");
var senderFont = document.getElementById("senderFontSelector");
var receiverFont = document.getElementById("recipientFontSelector");
var fontArray = fonts["items"];
var currentLink = cssLinkBase;
var fontCount = 0;
var maxFonts = 30;
for (var i = 0; i < fontArray.length; ++i) {
var displayname = fontArray[i]["family"];
var cat = fontArray[i]["category"];
var key = "'" + displayname + "', " + (cat === "display" || cat === "handwriting" ? "cursive" : cat);
var cssKey = displayname.replace(/ /g, "+")
currentLink += "family=" + cssKey + "&";
++fontCount;
if (fontCount >= maxFonts) {
currentLink += "display=swap";
var head = document.head;
var link = document.createElement("link");
link.rel = "stylesheet";
link.href = currentLink;
head.appendChild(link);
currentLink = cssLinkBase;
fontCount = 0;
}
var selected = false;
if (key === chatParams.defaultFont) {
selected = true;
}
generalFont.options[generalFont.options.length] = new Option(displayname, key, null, selected);
senderFont.options[senderFont.options.length] = new Option(displayname, key, null, selected);
receiverFont.options[receiverFont.options.length] = new Option(displayname, key, null, selected);
}
if (fontCount > 0) {
currentLink += "display=swap";
var head = document.head;
var link = document.createElement("link");
link.rel = "stylesheet";
link.href = currentLink;
head.appendChild(link);
}
}
}
}
fontRequest.open("GET", "https://www.googleapis.com/webfonts/v1/webfonts?key=API_KEY", true);
fontRequest.send();
}
InitFonts();
UpdateView();
currentAnimationState = 0;
lastAnimationTimestamp = Date.now();
setTimeout(animateLastMessage, 10);