-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidget.js
173 lines (160 loc) · 6.03 KB
/
widget.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
let eventsLimit = 5,
userLocale = "en-US",
includeFollowers = true,
includeRedemptions = true,
includeHosts = true,
minHost = 0,
includeRaids = true,
minRaid = 0,
includeSubs = true,
includeGifters = true,
includeTips = true,
minTip = 0,
includeCheers = true,
direction = "top",
minCheer = 0,
giftCount = 0;
let userCurrency,
totalEvents = 0;
window.addEventListener('onEventReceived', function (obj) {
if (typeof obj.detail.event.itemId !== "undefined") {
obj.detail.listener = "redemption-latest"
}
if (obj.detail.listener.indexOf("-latest")<0) return;
const listener = obj.detail.listener.split("-")[0];
const event = obj.detail.event;
if (listener === 'follower') {
if (includeFollowers) {
addEvent('follower', 'Follower', event.name);
}
} else if (listener === 'redemption') {
if (includeRedemptions) {
addEvent('redemption', 'Redeemed', event.name);
}
} else if (listener === 'subscriber') {
if (includeSubs) {
if (event.amount === 'gift') {
addEvent('sub', `Sub gift`, event.name);
} else {
addEvent('sub', `Sub X${event.amount}`, event.name);
}
if (event.gifted && includeGifters) {
if (giftCount === 0) {
addEvent('sub', `Gift X${event.count}`, event.sender);
giftCount = event.count;
}
giftCount--;
}
}
} else if (listener === 'cheer') {
if (includeCheers && minCheer <= event.amount) {
addEvent('cheer', `${event.amount.toLocaleString()} Bits`, event.name);
}
} else if (listener === 'tip') {
if (includeTips && minTip <= event.amount) {
addEvent('tip', event.amount.toLocaleString(userLocale, {
style: 'currency',
minimumFractionDigits: 0,
currency: userCurrency.code
}), event.name);
}
} else if (listener === 'raid') {
if (includeRaids && minRaid <= event.amount) {
addEvent('raid', `Raid ${event.amount.toLocaleString()}`, event.name);
}
}
});
window.addEventListener('onWidgetLoad', function (obj) {
let recents = obj.detail.recents;
recents.sort(function (a, b) {
return Date.parse(a.createdAt) - Date.parse(b.createdAt);
});
userCurrency = obj.detail.currency;
const fieldData = obj.detail.fieldData;
eventsLimit = fieldData.eventsLimit;
includeFollowers = (fieldData.includeFollowers === "yes");
includeRedemptions = (fieldData.includeRedemptions === "yes");
includeRaids = (fieldData.includeRaids === "yes");
minRaid = fieldData.minRaid;
includeSubs = (fieldData.includeSubs === "yes");
includeGifters = (fieldData.includeGifters === "yes");
includeTips = (fieldData.includeTips === "yes");
minTip = fieldData.minTip;
includeCheers = (fieldData.includeCheers === "yes");
minCheer = fieldData.minCheer;
direction = fieldData.direction;
let eventIndex;
for (eventIndex = 0; eventIndex < recents.length; eventIndex++) {
const event = recents[eventIndex];
if (event.type === 'follower') {
if (includeFollowers) {
addEvent('follower', 'Follower', event.name);
}
} else if (event.type === 'redemption') {
if (includeRedemptions) {
addEvent('redemption', 'Redeemed', event.name);
}
} else if (event.type === 'subscriber') {
if (includeSubs) {
if (event.amount === 'gift') {
addEvent('sub', `Sub gift`, event.name);
} else {
if (event.gifted && includeGifters && event.amount === event.count) {
addEvent('sub', `Gift X${event.amount}`, event.sender);
}
addEvent('sub', `Sub X${event.amount}`, event.name);
}
}
if (event.gifted && includeGifters) {
if (giftCount === 0) {
addEvent('sub', `Gift X${event.count}`, event.sender);
giftCount = event.count;
}
giftCount--;
}
} else if (event.type === 'cheer') {
if (includeCheers && minCheer <= event.amount) {
addEvent('cheer', `${event.amount.toLocaleString()} Bits`, event.name);
}
} else if (event.type === 'tip') {
if (includeTips && minTip <= event.amount) {
addEvent('tip', event.amount.toLocaleString(userLocale, {
style: 'currency',
minimumFractionDigits: 0,
currency: userCurrency.code
}), event.name);
}
} else if (event.type === 'raid') {
if (includeRaids && minRaid <= event.amount) {
addEvent('raid', `Raid ${event.amount.toLocaleString()}`, event.name);
}
}
}
});
function addEvent(type, text, username) {
totalEvents += 1;
const element =
`
<div class="event-container" id="event-${totalEvents}">
<div class="backgroundsvg"></div>
<div class="event-image event-${type}"></div>
<div class="username-container">${username.toUpperCase()}</div>
<div class="details-container">${text.toUpperCase()}</div>
</div>`;
if (direction === "bottom") {
$('.main-container').append(element);
} else {
$('.main-container').prepend(element);
}
if (totalEvents > eventsLimit) {
removeEvent(totalEvents - eventsLimit);
}
}
function removeEvent(eventId) {
$(`#event-${eventId}`).animate({
height: 0,
opacity: 0
}, 'slow', function () {
$(`#event-${eventId}`).remove();
});
}