forked from kleanins/kickaway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1126 lines (914 loc) · 49.7 KB
/
script.js
File metadata and controls
1126 lines (914 loc) · 49.7 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
document.addEventListener('DOMContentLoaded', () => {
const translations = {
'tr': {
metaTitle: 'Kickaway - Kick Çekiliş Aracı',
metaDescription: 'Kick.com için açık kaynaklı, istemci taraflı bir çekiliş aracı',
loginTitle: 'kickaway.co',
loginPrompt: 'Çekiliş yapmak istediğin Kick kanalını giriniz',
loginPlaceholder: 'Örn: xqc',
connectButton: 'Bağlan',
connecting: 'Bağlanılıyor…',
connectedTo: 'Bağlı Kanal:',
settingsMenuAria: 'Ayarlar Menüsü',
changeChannel: 'Kanal Değiştir',
clearAllData: 'Tüm Veriyi Sil',
settingsTitle: 'Ayarlar',
keywordLabel: 'Anahtar Kelime (opsiyonel):',
winnerCountLabel: 'Kazanan Sayısı:',
subOnlyLabel: 'Sadece aboneler katılsın',
subMultiplierLabel: 'Abone Çarpanı:',
followLengthLabel: 'Takip Süresi:',
subLengthLabel: 'Abonelik Süresi:',
daysLabel: 'Gün',
monthsLabel: 'Ay',
followTooltip: 'Deneysel Özellik: Kazananın takip süresi çekilişin çekilmesi sırasında kontrol edilir. Bu yüzden Katılımcılar listesinde çekilişi takip süresi sebebiyle teknik olarak kazanamayacak kullanıcıların görünmesi normaldir.',
winnerClaimLabel: 'Kazanan onayı (tekli kazananda)',
claimDurationLabel: 'Onay Süresi (sn):',
animationLabel: 'Animasyon:',
animationWheel: 'Çark',
animationClassic: 'Klasik',
animationScramble: 'Karışık Harfler',
startGiveawayButton: 'Çekilişi Başlat',
statusWaiting: 'Sohbete bağlanmak için "Başlat" butonuna bas.',
statusConnected: (keyword) => keyword === '' ? 'Sohbete bağlanıldı! Katılımlar başladı!' : `Sohbete bağlanıldı! "${keyword}" bekleniyor.`,
statusConnectionLost: 'Bağlantı koptu!',
participantsTitle: 'Katılımcılar',
drawButton: 'Çekilişi Çek',
resetButton: 'Sıfırla',
winnersTitle: 'Kazananlar',
claimTitle: 'Onay Bekleniyor:',
claimCountdownText: (name, time) => `${name}, onayın için ${time} saniyen var...`,
claimConfirmed: 'Onaylandı!',
claimFailed: 'Süre Doldu!',
okButton: 'Tamam',
modalDrawing: 'Çekiliyor...',
modalChecking: 'Adaylar Kontrol Ediliyor:',
modalWinnerTitle: 'Kazanan:',
alertEnterChannel: 'Lütfen bir kanal adı girin.',
alertLoginError: (msg) => `Giriş hatası: ${msg}`,
alertChannelNotFound: 'Kanal bulunamadı veya API hatası.',
alertChatroomIDError: 'Chatroom ID alınamadı.',
alertNoParticipants: 'Çekilişe katılan kimse yok!',
alertApi: 'Takip süresi kontrol edilirken hata!',
confirmChangeChannel: 'Kanal seçim ekranına dönülecektir. Emin misiniz?',
confirmClearAllData: 'DİKKAT! Tüm kanallara ait ayarlar ve kazanan listeleri kalıcı olarak silinecektir. Emin misiniz?',
footerDisclaimer1: "Bu araç bağımsız bir geliştirici tarafından oluşturulmuştur ve Kick.com ile herhangi bir resmi ilişkisi yoktur.",
footerDisclaimer2: "Kullanımından doğabilecek herhangi bir sonuç tamamen kullanıcının sorumluluğundadır.",
footerDisclaimer3: "Kickaway tamamen istemci taraflı çalışır; çekiliş verileri yalnızca tarayıcınızın yerel depolama alanında tutulur.",
footerDisclaimer4: "Bu site anonim istatistikler toplar.",
subscriberTitle: (count) => `Abone (${count} Hak)`,
},
'en': {
metaTitle: 'Kickaway - Kick Giveaway Tool',
metaDescription: 'An open-source, client-side giveaway tool for Kick.com',
loginTitle: 'kickaway.co',
loginPrompt: 'Enter the Kick channel for the giveaway',
loginPlaceholder: 'E.g., xqc',
connectButton: 'Connect',
connecting: 'Connecting…',
connectedTo: 'Connected to:',
settingsMenuAria: 'Settings Menu',
changeChannel: 'Change Channel',
clearAllData: 'Clear All Data',
settingsTitle: 'Settings',
keywordLabel: 'Keyword (optional):',
winnerCountLabel: 'Number of Winners:',
subOnlyLabel: 'Subscribers only',
subMultiplierLabel: 'Subscriber Multiplier:',
followLengthLabel: 'Follow Duration:',
subLengthLabel: 'Subscription Duration:',
daysLabel: 'Days',
monthsLabel: 'Months',
followTooltip: 'Experimental feature, the winner\'s follow duration is checked during the draw. Because of this, it is normal for users who technically cannot win due to follow duration to appear in the participants list.',
winnerClaimLabel: 'Winner confirmation (for single winner)',
claimDurationLabel: 'Confirm Time (s):',
animationLabel: 'Animation:',
animationWheel: 'Wheel',
animationClassic: 'Classic',
animationScramble: 'Character Scramble',
startGiveawayButton: 'Start Giveaway',
statusWaiting: 'Press "Start" to connect to the chat.',
statusConnected: (keyword) => keyword === '' ? 'Connected to chat! Entries are open!' : `Connected to chat! Waiting for "${keyword}".`,
statusConnectionLost: 'Connection lost!',
participantsTitle: 'Participants',
drawButton: 'Draw Winner',
resetButton: 'Reset',
winnersTitle: 'Winners',
claimTitle: 'Awaiting Confirmation:',
claimCountdownText: (name, time) => `${name}, you have ${time} seconds to confirm...`,
claimConfirmed: 'Confirmed!',
claimFailed: 'Time is up!',
okButton: 'OK',
modalDrawing: 'Drawing...',
modalChecking: 'Checking Candidates:',
modalWinnerTitle: 'Winner:',
alertEnterChannel: 'Please enter a channel name.',
alertLoginError: (msg) => `Login error: ${msg}`,
alertChannelNotFound: 'Channel not found or API error.',
alertChatroomIDError: 'Could not get Chatroom ID.',
alertNoParticipants: 'There are no participants in the giveaway!',
alertApi: 'Error while checking Follow Duration!',
confirmChangeChannel: 'You will be returned to the channel selection screen. Are you sure?',
confirmClearAllData: 'WARNING! All settings and winner lists for all channels will be permanently deleted. Are you sure?',
footerDisclaimer1: "This tool was created by an independent developer and has no official affiliation with Kick.com",
footerDisclaimer2: "Any consequences arising from its use are the sole responsibility of the user.",
footerDisclaimer3: "Kickaway works entirely client-side; giveaway data lives only in your browser’s local storage.",
footerDisclaimer4: "This site collects anonymous statistics.",
subscriberTitle: (count) => `Subscriber (${count}x Entries)`,
}
};
let currentLang;
const languageSwitcher = document.getElementById('language-switcher');
function setLanguage(lang) {
if (!translations[lang]) lang = 'en';
currentLang = lang;
localStorage.setItem('kickawayLang', lang);
document.documentElement.lang = lang;
document.querySelectorAll('[data-lang-key]').forEach(el => {
const key = el.getAttribute('data-lang-key');
if (translations[lang][key]) {
el.textContent = translations[lang][key];
}
});
document.querySelectorAll('[data-lang-key-placeholder]').forEach(el => {
const key = el.getAttribute('data-lang-key-placeholder');
if (translations[lang][key]) {
el.placeholder = translations[lang][key];
}
});
document.querySelectorAll('[data-lang-key-aria-label]').forEach(el => {
const key = el.getAttribute('data-lang-key-aria-label');
if (translations[lang][key]) {
el.setAttribute('aria-label', translations[lang][key]);
}
});
document.querySelectorAll('[data-lang-key-title]').forEach(el => {
const key = el.getAttribute('data-lang-key-title');
if (translations[lang][key]) {
el.setAttribute('data-title', translations[lang][key]);
el.removeAttribute('title');
}
});
document.title = translations[lang].metaTitle;
document.querySelector('meta[name="description"]').setAttribute('content', translations[lang].metaDescription);
document.querySelector('meta[property="og:title"]').setAttribute('content', translations[lang].metaTitle);
document.querySelector('meta[property="og:description"]').setAttribute('content', translations[lang].metaDescription);
if(languageSwitcher.querySelector('#lang-tr-button')) {
languageSwitcher.querySelector('#lang-tr-button').classList.toggle('active-lang', lang === 'tr');
languageSwitcher.querySelector('#lang-en-button').classList.toggle('active-lang', lang === 'en');
}
if (isGiveawayRunning) {
const modeText = translations[currentLang].statusConnected(giveawayKeyword);
statusMessage.textContent = modeText;
} else {
statusMessage.textContent = translations[currentLang]?.statusWaiting || translations['en'].statusWaiting;
}
updateWinnersListUI();
}
const loginScreen = document.getElementById('login-screen'),
mainScreen = document.getElementById('main-screen'),
channelInput = document.getElementById('channel-input'),
connectButton = document.getElementById('connect-button'),
connectedChannelName = document.getElementById('connected-channel-name');
const headerMenuContainer = document.getElementById('header-menu-container'),
headerMenuToggle = document.getElementById('header-menu-toggle'),
headerMenuDropdown = document.getElementById('header-menu-dropdown'),
changeChannelButton = document.getElementById('change-channel-button'),
clearAllDataButton = document.getElementById('clear-all-data-button');
const settingsPanel = document.querySelector('.settings-panel'),
keywordInput = document.getElementById('keyword-input'),
winnerCountInput = document.getElementById('winner-count-input'),
subOnlyCheckbox = document.getElementById('sub-only-checkbox'),
subMultiplierSlider = document.getElementById('sub-multiplier-slider'),
subMultiplierValue = document.getElementById('sub-multiplier-value'),
winnerClaimCheckbox = document.getElementById('winner-claim-checkbox'),
claimDurationInput = document.getElementById('claim-duration-input'),
animationSelect = document.getElementById('animation-select'),
startGiveawayButton = document.getElementById('start-giveaway-button'),
statusMessage = document.getElementById('status-message');
const followLengthSlider = document.getElementById('follow-length-slider'),
followLengthValue = document.getElementById('follow-length-value'),
subLengthSlider = document.getElementById('sub-length-slider'),
subLengthValue = document.getElementById('sub-length-value');
const participantCount = document.getElementById('participant-count'),
participantList = document.getElementById('participant-list'),
drawButton = document.getElementById('draw-button'),
resetButton = document.getElementById('reset-button');
const winnersCount = document.getElementById('winners-count'),
winnersList = document.getElementById('winners-list');
const claimSection = document.getElementById('claim-section'),
claimWinnerName = document.getElementById('claim-winner-name'),
claimCountdown = document.getElementById('claim-countdown'),
claimChatLog = document.getElementById('claim-chat-log'),
claimConfirmButton = document.getElementById('claim-confirm-button');
const modalTemplate = document.getElementById('modal-template');
const multiWinnerModalContainer = document.getElementById('multi-winner-modal-container');
let kickChannelName = '', kickChannelSlug = '', kickChatroomId = null, channelCreationDate = null, ws = null;
let giveawayKeyword = '', isGiveawayRunning = false, isDrawInProgress = false;
let participants = new Set(), allEntries = [], winners = [];
let modalChatWinners = [], claimInterval = null, lastClaimWinner = null;
let pendingAnimationInterval = null;
// Remove characters in Unicode block U+E0000..U+E007F (supplementary special-purpose plane - tag characters)
const SPECIAL_TAGS_REGEX = /[\u{E0000}-\u{E007F}]/gu;
function sanitizeUnicodeTagCharacters(str) {
if (typeof str !== 'string') return str;
return str.replace(SPECIAL_TAGS_REGEX, '');
}
function showScreen(scr) {
loginScreen.style.display = 'none';
mainScreen.style.display = 'none';
if (scr === 'login') loginScreen.style.display = 'flex';
if (scr === 'main') mainScreen.style.display = 'block';
}
function setSettingsState(disabled) {
settingsPanel.querySelectorAll('input:not([type="checkbox"]), select, button#start-giveaway-button').forEach(el => el.disabled = disabled);
subOnlyCheckbox.disabled = disabled;
const rangeInputs = settingsPanel.querySelectorAll('input[type="range"]');
rangeInputs.forEach(input => input.disabled = disabled);
if (disabled) {
winnerClaimCheckbox.disabled = true;
claimDurationInput.disabled = true;
} else {
updateClaimSettingsState();
}
}
function updateDrawButtonState() {
if (isDrawInProgress) {
drawButton.disabled = true;
return;
}
const potentialWinnersLeft = [...new Set(allEntries)].length;
drawButton.disabled = potentialWinnersLeft === 0;
}
function getStoredData() { return JSON.parse(localStorage.getItem('kickGiveawayDB') || '{}'); }
function saveState() {
if (!kickChannelName) return;
const allData = getStoredData();
allData[kickChannelName] = {
settings: {
keyword: keywordInput.value,
winnerCount: winnerCountInput.value,
subOnly: subOnlyCheckbox.checked,
subMultiplier: subMultiplierSlider.value,
followLength: followLengthSlider.value,
subLength: subLengthSlider.value,
winnerClaim: winnerClaimCheckbox.checked,
claimDuration: claimDurationInput.value,
animation: animationSelect.value,
},
winners: winners,
};
localStorage.setItem('kickGiveawayDB', JSON.stringify(allData));
}
function loadStateForChannel(channelName) {
resetSession();
const allData = getStoredData();
const channelData = allData[channelName];
if (channelData) {
const { settings, winners: storedWinners } = channelData;
keywordInput.value = settings.keyword || '';
winnerCountInput.value = settings.winnerCount || '1';
subOnlyCheckbox.checked = settings.subOnly || false;
subMultiplierSlider.value = settings.subMultiplier || '1';
subMultiplierValue.textContent = settings.subMultiplier || '1';
followLengthSlider.value = settings.followLength || '0';
followLengthValue.textContent = settings.followLength || '0';
subLengthSlider.value = settings.subLength || '0';
subLengthValue.textContent = settings.subLength || '0';
winnerClaimCheckbox.checked = settings.winnerClaim || false;
claimDurationInput.value = settings.claimDuration || '30';
animationSelect.value = settings.animation || 'wheel';
winners = storedWinners || [];
} else {
keywordInput.value = ''; winnerCountInput.value = '1'; subOnlyCheckbox.checked = false;
subMultiplierSlider.value = '1'; subMultiplierValue.textContent = '1';
followLengthSlider.value = '0'; followLengthValue.textContent = '0';
subLengthSlider.value = '0'; subLengthValue.textContent = '0';
winnerClaimCheckbox.checked = false; claimDurationInput.value = '30';
animationSelect.value = 'wheel';
winners = [];
}
updateWinnersListUI();
updateClaimSettingsState();
}
function loadInitialState() {
const lastChannel = localStorage.getItem('kickGiveawayLastChannel');
if(lastChannel) channelInput.value = lastChannel;
}
async function handleLogin() {
const channelName = channelInput.value.trim().toLowerCase();
if (!channelName) { alert(translations[currentLang].alertEnterChannel); return; }
connectButton.disabled = true; connectButton.textContent = translations[currentLang].connecting;
try {
const r = await fetch(`https://kick.com/api/v2/channels/${channelName}`);
if (!r.ok) throw new Error(translations[currentLang].alertChannelNotFound);
const data = await r.json();
if (!data.chatroom?.id) throw new Error(translations[currentLang].alertChatroomIDError);
kickChannelName = data.slug;
kickChannelSlug = data.slug;
kickChatroomId = data.chatroom.id;
let startDate = new Date();
if (data.user && data.user.email_verified_at) {
startDate = new Date(data.user.email_verified_at);
} else if (data.created_at) {
startDate = new Date(data.created_at);
}
const now = new Date();
const diffTime = Math.abs(now - startDate);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
followLengthSlider.max = diffDays;
if (parseInt(followLengthSlider.value) > diffDays) {
followLengthSlider.value = diffDays;
followLengthValue.textContent = diffDays;
}
localStorage.setItem('kickGiveawayLastChannel', kickChannelName);
connectedChannelName.textContent = kickChannelName;
loadStateForChannel(kickChannelName);
showScreen('main');
} catch (err) {
alert(translations[currentLang].alertLoginError(err.message));
} finally {
connectButton.disabled = false; connectButton.textContent = translations[currentLang].connectButton;
}
}
const WS_URL = 'wss://ws-us2.pusher.com/app/32cbd69e4b950bf97679?protocol=7&client=js&version=8.4.0&flash=false';
function connectToKickChat() {
if (ws && ws.readyState === WebSocket.OPEN) ws.close();
ws = new WebSocket(WS_URL);
ws.onmessage = handleWebSocketMessage;
ws.onerror = (err) => console.error(`WebSocket Error: ${err.message}`);
ws.onclose = (ev) => {
console.warn(`WebSocket closed. Code: ${ev.code}`);
if (isGiveawayRunning) { statusMessage.textContent = translations[currentLang].statusConnectionLost; statusMessage.className = 'status-error'; }
};
}
function handleWebSocketMessage(e) {
let pkt; try { pkt = JSON.parse(e.data); } catch { return; }
const event = pkt.event;
const data = pkt.data ? JSON.parse(pkt.data) : {};
if (event === 'pusher:connection_established') {
ws.send(JSON.stringify({ event: 'pusher:subscribe', data: { auth: '', channel: `chatrooms.${kickChatroomId}.v2` } }));
} else if (event === 'pusher_internal:subscription_succeeded') {
const modeText = translations[currentLang].statusConnected(giveawayKeyword);
statusMessage.textContent = modeText;
statusMessage.className = 'status-connected';
} else if (event === 'App\\Events\\ChatMessageEvent') {
handleChatMessage(data);
}
}
function startGiveaway() {
resetSession();
giveawayKeyword = keywordInput.value.trim().toLowerCase();
isGiveawayRunning = true;
setSettingsState(true);
connectToKickChat();
}
function handleChatMessage(msg) {
msg.content = sanitizeUnicodeTagCharacters(msg.content || '');
if (lastClaimWinner && msg.sender.username.toLowerCase() === lastClaimWinner.name.toLowerCase()) {
const li = document.createElement('li');
li.textContent = `${msg.sender.username}: ${msg.content}`;
claimChatLog.append(li);
claimChatLog.scrollTop = claimChatLog.scrollHeight;
if (claimInterval) {
clearInterval(claimInterval);
claimInterval = null;
lastClaimWinner.confirmed = true;
claimCountdown.textContent = translations[currentLang].claimConfirmed;
claimCountdown.className = 'confirmed';
claimConfirmButton.style.display = 'block';
updateWinnersListUI();
saveState();
}
}
const modalWinner = modalChatWinners.find(w => w.name === msg.sender.username.toLowerCase());
if (modalWinner) {
if (modalWinner.mode === 'below') {
if (modalWinner.logElement.classList.contains('waiting')) {
modalWinner.logElement.classList.remove('waiting');
}
if (modalWinner.logElement.style.display !== 'block') {
modalWinner.logElement.style.display = 'block';
}
const li = document.createElement('li');
li.textContent = `${msg.sender.username}: ${msg.content}`;
modalWinner.logElement.append(li);
modalWinner.logElement.scrollTop = modalWinner.logElement.scrollHeight;
} else if (modalWinner.mode === 'replace') {
if (!modalWinner.hasChatStarted) {
modalWinner.hasChatStarted = true;
modalWinner.titleEl.textContent = `${translations[currentLang].modalWinnerTitle} ${msg.sender.username}`;
modalWinner.animationBoxEl.innerHTML = '';
modalWinner.animationBoxEl.style.display = 'block';
const newLog = document.createElement('ul');
newLog.className = 'modal-winner-chat-log';
newLog.style.display = 'block';
newLog.style.height = '100%';
newLog.addEventListener('wheel', preventPageScroll);
newLog.addEventListener('wheel', (e) => e.stopPropagation());
modalWinner.animationBoxEl.appendChild(newLog);
modalWinner.logElement = newLog;
}
const li = document.createElement('li');
li.textContent = `${msg.sender.username}: ${msg.content}`;
modalWinner.logElement.append(li);
modalWinner.logElement.scrollTop = modalWinner.logElement.scrollHeight;
}
}
if (!isGiveawayRunning || participants.has(msg.sender.username)) return;
const userMessage = msg.content.trim().toLowerCase();
if (giveawayKeyword === '' || userMessage === giveawayKeyword) {
addParticipant(msg.sender);
}
}
function addParticipant(sender) {
const existingLi = [...participantList.children].find(li => li.textContent === sender.username);
if (existingLi) return;
const subBadge = (sender.identity?.badges || []).find(b => b.type === 'subscriber' || b.type === 'founder');
const isSub = !!subBadge;
if (subOnlyCheckbox.checked && !isSub) return;
const minSubMonths = parseInt(subLengthSlider.value) || 0;
const userSubMonths = subBadge ? (subBadge.count || 0) : 0;
if (minSubMonths > 0 && userSubMonths < minSubMonths) return;
participants.add(sender.username);
const entryCount = isSub ? parseInt(subMultiplierSlider.value) : 1;
for (let i = 0; i < entryCount; i++) allEntries.push(sender.username);
const li = document.createElement('li');
li.textContent = sender.username;
if (isSub) {
li.classList.add('subscriber');
li.title = translations[currentLang].subscriberTitle(entryCount);
}
participantList.prepend(li);
participantCount.textContent = participants.size;
updateDrawButtonState();
}
async function checkFollowAge(channelSlug, username, minDays) {
if (minDays <= 0) return true;
try {
const r = await fetch(`https://kick.com/api/v2/channels/${channelSlug}/users/${username}`);
if (!r.ok) return false;
const data = await r.json();
if (!data.following_since) return false;
const followDate = new Date(data.following_since);
const now = new Date();
const diffTime = Math.abs(now - followDate);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
return diffDays >= minDays;
} catch (e) {
console.error("Follow check failed", e);
return null;
}
}
function startPendingAnimation(requestedCount) {
document.body.classList.add('modal-active');
multiWinnerModalContainer.style.display = 'flex';
multiWinnerModalContainer.innerHTML = '';
const count = requestedCount;
const topRowCount = (count <= 3) ? count : Math.ceil(count / 2);
let row1, row2;
row1 = document.createElement('div');
row1.className = 'modal-row';
multiWinnerModalContainer.appendChild(row1);
if (count > 3) {
row2 = document.createElement('div');
row2.className = 'modal-row';
multiWinnerModalContainer.appendChild(row2);
}
const selectedAnim = animationSelect.value;
if (pendingAnimationInterval) clearInterval(pendingAnimationInterval);
const animationBoxes = [];
for (let i = 0; i < count; i++) {
const modalInstance = modalTemplate.firstElementChild.cloneNode(true);
if (i < topRowCount) row1.appendChild(modalInstance);
else row2.appendChild(modalInstance);
const content = modalInstance.querySelector('.modal-content');
const titleEl = content.querySelector('.winner-title-text');
titleEl.textContent = translations[currentLang].modalDrawing;
const animationBox = content.querySelector('.winner-animation-box');
const btn = content.querySelector('.modal-confirm-button');
btn.style.visibility = 'hidden';
animationBoxes.push(animationBox);
if (selectedAnim === 'wheel') {
animationBox.innerHTML = `<div class="wheel-animation-container"><ul class="wheel-list" style="transform: none; transition: none;"></ul></div>`;
const ul = animationBox.querySelector('ul');
ul.style.animation = "infiniteSpin 0.15s linear infinite";
populateWheelList(ul);
} else if (selectedAnim === 'char-scramble') {
animationBox.innerHTML = '<h1 class="winner-name"></h1>';
} else {
animationBox.innerHTML = '<h1 class="winner-name"></h1>';
}
}
let pendingPool = allEntries.length > 0 ? allEntries : ["Wait...", "Loading...", "Drawing..."];
while(pendingPool.length < 50) pendingPool = pendingPool.concat(pendingPool);
pendingAnimationInterval = setInterval(() => {
animationBoxes.forEach(box => {
if (selectedAnim === 'wheel') {
} else if (selectedAnim === 'char-scramble') {
const el = box.querySelector('h1');
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?*&%';
let output = '';
for(let i=0; i<10; i++) output += chars[Math.floor(Math.random() * chars.length)];
el.textContent = output;
} else {
const el = box.querySelector('h1');
el.textContent = pendingPool[Math.floor(Math.random() * pendingPool.length)];
}
});
}, 60);
}
function populateWheelList(ul) {
let pendingPool = allEntries.length > 0 ? allEntries : ["Wait...", "Loading..."];
for(let i=0; i<50; i++) {
const li = document.createElement('li');
li.textContent = pendingPool[i % pendingPool.length];
ul.appendChild(li);
}
}
function stopPendingAnimation() {
if (pendingAnimationInterval) {
clearInterval(pendingAnimationInterval);
pendingAnimationInterval = null;
}
}
async function drawWinner() {
isDrawInProgress = true;
updateDrawButtonState();
resetButton.disabled = true;
const requestedWinnerCount = parseInt(winnerCountInput.value) || 1;
const useClaimFeature = winnerClaimCheckbox.checked && requestedWinnerCount === 1;
let drawPool = [...allEntries];
let potentialWinnersLeft = [...new Set(drawPool)].length;
if (potentialWinnersLeft === 0) {
alert(translations[currentLang].alertNoParticipants);
finishDrawCycle();
return;
}
const finalCountToDraw = Math.min(requestedWinnerCount, potentialWinnersLeft);
const winnersToDraw = [];
const minFollowDays = parseInt(followLengthSlider.value) || 0;
startPendingAnimation(finalCountToDraw);
let apiErrorOccurred = false;
while (winnersToDraw.length < finalCountToDraw && potentialWinnersLeft > 0) {
const winnerIndex = Math.floor(Math.random() * drawPool.length);
const candidateName = drawPool[winnerIndex];
let isValid = true;
if (minFollowDays > 0) {
const checkResult = await checkFollowAge(kickChannelSlug, candidateName, minFollowDays);
if (checkResult === null) {
apiErrorOccurred = true;
break;
}
isValid = checkResult;
await new Promise(r => setTimeout(r, 200));
}
if (isValid) {
const winnerData = { name: candidateName, timestamp: new Date().toISOString(), confirmed: !useClaimFeature };
winnersToDraw.push(winnerData);
drawPool = drawPool.filter(n => n !== candidateName);
potentialWinnersLeft = [...new Set(drawPool)].length;
} else {
drawPool = drawPool.filter(n => n !== candidateName);
potentialWinnersLeft = [...new Set(drawPool)].length;
}
}
stopPendingAnimation();
if (apiErrorOccurred) {
document.body.classList.remove('modal-active');
multiWinnerModalContainer.style.display = 'none';
alert(translations[currentLang].alertApi);
finishDrawCycle();
return;
}
if (winnersToDraw.length === 0) {
document.body.classList.remove('modal-active');
multiWinnerModalContainer.style.display = 'none';
alert(translations[currentLang].alertNoParticipants + " (Follow Duration too strict?)");
finishDrawCycle();
return;
}
winners.push(...winnersToDraw);
winnersToDraw.forEach(w => {
allEntries = allEntries.filter(n => n !== w.name);
participants.delete(w.name);
});
updateDrawButtonState();
updateWinnersListUI();
await displayMultiWinnerModals(winnersToDraw, allEntries.concat(winnersToDraw.map(w=>w.name)), useClaimFeature);
if (useClaimFeature) {
lastClaimWinner = winnersToDraw[0];
}
}
function displayMultiWinnerModals(drawnWinners, fullPool, useClaimFeature) {
return new Promise(resolve => {
multiWinnerModalContainer.style.display = 'flex';
const existingInstances = multiWinnerModalContainer.querySelectorAll('.modal-instance');
const canReuse = existingInstances.length === drawnWinners.length;
if (!canReuse) {
multiWinnerModalContainer.innerHTML = '';
}
let row1, row2;
if (!canReuse) {
const count = drawnWinners.length;
const topRowCount = (count <= 3) ? count : Math.ceil(count / 2);
row1 = document.createElement('div');
row1.className = 'modal-row';
multiWinnerModalContainer.appendChild(row1);
if (count > 3) {
row2 = document.createElement('div');
row2.className = 'modal-row';
multiWinnerModalContainer.appendChild(row2);
}
} else {
}
const animationPromises = drawnWinners.map((winner, index) => {
let modalWrapper;
if (canReuse) {
modalWrapper = existingInstances[index];
} else {
const count = drawnWinners.length;
const topRowCount = (count <= 3) ? count : Math.ceil(count / 2);
modalWrapper = modalTemplate.firstElementChild.cloneNode(true);
if (index < topRowCount) row1.appendChild(modalWrapper);
else row2.appendChild(modalWrapper);
}
const titleEl = modalWrapper.querySelector('.winner-title-text');
const animationBoxEl = modalWrapper.querySelector('.winner-animation-box');
const confirmButton = modalWrapper.querySelector('.modal-confirm-button');
const chatLogEl = modalWrapper.querySelector('.modal-winner-chat-log');
confirmButton.textContent = translations[currentLang].okButton;
confirmButton.style.visibility = 'hidden';
const newBtn = confirmButton.cloneNode(true);
confirmButton.parentNode.replaceChild(newBtn, confirmButton);
newBtn.addEventListener('click', () => {
modalWrapper.classList.add('is-hidden');
const showChatLogsBelow = drawnWinners.length <= 3;
const replaceAnimationBoxMode = drawnWinners.length > 3;
if (showChatLogsBelow || replaceAnimationBoxMode) {
modalChatWinners = modalChatWinners.filter(w => w.name !== winner.name.toLowerCase());
}
if (useClaimFeature && drawnWinners.length === 1) {
handleWinnerClaim();
}
});
modalWrapper.addEventListener('transitionend', (event) => {
if (event.propertyName === 'opacity' && modalWrapper.classList.contains('is-hidden')) {
const allInstances = multiWinnerModalContainer.querySelectorAll('.modal-instance');
const allHidden = [...allInstances].every(m => m.classList.contains('is-hidden'));
if (allHidden) {
multiWinnerModalContainer.style.display = 'none';
document.body.classList.remove('modal-active');
if (!useClaimFeature) {
finishDrawCycle();
}
}
}
});
return runDrawAnimation(fullPool, winner.name, animationBoxEl).then(() => {
titleEl.textContent = translations[currentLang].modalWinnerTitle;
newBtn.style.visibility = 'visible';
const showChatLogsBelow = drawnWinners.length <= 3;
const replaceAnimationBoxMode = drawnWinners.length > 3;
if (showChatLogsBelow) {
chatLogEl.style.display = 'block';
chatLogEl.classList.add('waiting');
chatLogEl.addEventListener('wheel', preventPageScroll);
chatLogEl.addEventListener('wheel', (e) => e.stopPropagation());
modalChatWinners.push({ name: winner.name.toLowerCase(), logElement: chatLogEl, mode: 'below' });
} else if (replaceAnimationBoxMode) {
modalChatWinners.push({
name: winner.name.toLowerCase(),
animationBoxEl: animationBoxEl,
titleEl: titleEl,
mode: 'replace',
hasChatStarted: false
});
}
});
});
Promise.all(animationPromises).then(resolve);
});
}
function runDrawAnimation(pool, finalWinner, animationBoxEl) {
return new Promise((resolve) => {
const animationType = animationSelect.value;
const animations = {
'slot-machine': animateSlotMachine,
'char-scramble': animateCharScramble,
'wheel': animateWheel,
};
animations[animationType](pool, finalWinner, animationBoxEl, resolve);
});
}
function animateSlotMachine(pool, finalWinner, animationBoxEl, resolve) {
animationBoxEl.innerHTML = '<h1 class="winner-name"></h1>';
const winnerNameEl = animationBoxEl.querySelector('.winner-name');
const localInterval = setInterval(() => {
winnerNameEl.textContent = pool.length > 0 ? pool[Math.floor(Math.random() * pool.length)] : '...';
}, 75);
setTimeout(() => {
clearInterval(localInterval);
winnerNameEl.textContent = finalWinner;
winnerNameEl.classList.add('winner');
resolve();
}, 3500);
}
function animateCharScramble(pool, finalWinner, animationBoxEl, resolve) {
animationBoxEl.innerHTML = '<h1 class="winner-name"></h1>';
const winnerNameEl = animationBoxEl.querySelector('.winner-name');
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?*&%';
let frame = 0; const totalFrames = 150;
const localInterval = setInterval(() => {
let output = ''; let completed = true;
for (let i = 0; i < finalWinner.length; i++) {
const from = finalWinner[i], to = chars[Math.floor(Math.random() * chars.length)];
const progress = Math.min(frame / (totalFrames * 0.5 + i * 5), 1);
if (progress < 1) completed = false;
output += (Math.random() < progress) ? from : to;
}
winnerNameEl.textContent = output;
if (completed) {
clearInterval(localInterval);
winnerNameEl.classList.add('winner');
resolve();
}
frame++;
}, 40);
}
function animateWheel(pool, finalWinner, animationBoxEl, resolve) {
animationBoxEl.innerHTML = `<div class="wheel-animation-container"><ul class="wheel-list"></ul></div>`;
const wheelContainer = animationBoxEl.querySelector('.wheel-animation-container');
const wheelList = animationBoxEl.querySelector('.wheel-list');
let uniquePool = [...new Set(pool)];
let noisePool = uniquePool.filter(n => n !== finalWinner);
if (noisePool.length === 0) {
noisePool = [finalWinner];
}
while (noisePool.length < 10) noisePool = noisePool.concat(noisePool);
const cycles = Math.ceil(200 / noisePool.length);
let repeatedPool = [];
for (let c = 0; c < cycles; c++) {
const shuffled = [...noisePool];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
repeatedPool.push(...shuffled);
}
const targetIndex = repeatedPool.length - Math.floor(noisePool.length / 2) - 1;
repeatedPool[targetIndex] = finalWinner;
repeatedPool.forEach((name, index) => {
const li = document.createElement('li');
li.textContent = name;
if (index === targetIndex) li.classList.add('winner');
wheelList.appendChild(li);
});
setTimeout(() => {
const containerStyle = window.getComputedStyle(wheelContainer);
const itemStyle = window.getComputedStyle(wheelList.querySelector('li'));
const containerHeight = parseFloat(containerStyle.height);
const itemHeight = parseFloat(itemStyle.height);
const finalY = (containerHeight / 2) - (targetIndex * itemHeight) - (itemHeight / 2);
wheelList.style.transform = `translateY(${finalY}px)`;
}, 100);
wheelList.addEventListener('transitionend', () => {
wheelList.classList.add('finished');
resolve();
}, { once: true });
}
function handleWinnerClaim() {
claimSection.style.display = 'flex';
claimWinnerName.textContent = lastClaimWinner.name;
claimCountdown.className = '';
claimConfirmButton.style.display = 'none';
if (claimChatLog.children.length > 0) {
lastClaimWinner.confirmed = true;
claimCountdown.textContent = translations[currentLang].claimConfirmed;
claimCountdown.className = 'confirmed';
claimConfirmButton.style.display = 'block';
updateWinnersListUI();
saveState();
claimInterval = null;
} else {
let t = parseInt(claimDurationInput.value);
claimCountdown.textContent = translations[currentLang].claimCountdownText(lastClaimWinner.name, t);
claimInterval = setInterval(() => {
t--;
claimCountdown.textContent = translations[currentLang].claimCountdownText(lastClaimWinner.name, t);
if (t <= 0) {
clearInterval(claimInterval);
claimInterval = null;
claimCountdown.textContent = translations[currentLang].claimFailed;
claimCountdown.className = 'failed';
claimConfirmButton.style.display = 'block';