-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubmote.js
356 lines (299 loc) · 10.8 KB
/
submote.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
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
// Submote by Zarlach
console.log('Submote is running');
// Global vars
var subMotes = {}; // subMotes[emote] = source
var subEmotes = {}; // subEmotes[emote] = id
var bttvEmotes = {}; // bttvEmotes[emote] = source
/**
* Hotfix, problems loading emotes
*
*/
// Check if emotes are already stored locally
if (localStorage.getItem("subEmotes") === null || localStorage.getItem("bttvEmotes") === null){
reloadAllEmotes();
}else{
reloadAllEmotes();
//subEmotes = JSON.parse(localStorage.getItem("subEmotes"));
//var genDate = Date.parse(subEmotes.generated_at);
//var currentDate = new Date();
//var expirationDate = new Date(genDate.getTime() + 3600*1000/2); // 30 minutes
//// Check if expired
//if(currentDate.getTime() >= expirationDate.getTime())
// reloadAllEmotes();
}
/**
* Reloads the emote lists.
*
*/
function reloadAllEmotes(){
// Remove any remaining emotes
localStorage.setItem('subEmotes', null);
localStorage.setItem('bttvEmotes', null);
//Load all emotes
loadSubEmotes();
loadBetterTTVemotes();
loadCustomEmotes();
}
/**
* Observe for new messages
*
*/
$(document).ready(function(evt){
// An attempt to find a new and better way to detect new messages
var target = document;
// Observer intsance
var observer = new MutationObserver(function(mutations){
mutations.forEach(function(mutation){
if(mutation.addedNodes){
// Check for queued messages
if(mutation.addedNodes.length > 1){
for(var i = 0; i < mutation.addedNodes.length; i++){
// Make it readable to newMessage()
var message = [mutation.addedNodes[i]];
newMessage(message);
}
}else{
newMessage(mutation.addedNodes);
}
}
});
});
// Mutation config
var config = {
attributes: true,
childList: true,
subtree: true,
characterData: true,
};
/**
* This causes crashes on pages that does not have a chat
*/
// Start the observer
//while(!target || target === null)
// target = document.querySelector('.chat-lines');
observer.observe(target, config);
});
/**
* Handle new messages
*
*/
function newMessage(message){
var line = false;
var bttv = true; // Assume true
// Check if using vanilla or FrankerFaceZ
if($(message[1]).find('.chat-line').length){
line = $(message[1]).find('li.chat-line');
bttv = false;
}
// Check if using BetterTTV
if(bttv && !line && $(message[0]).length){
line = $(message[0]);
}
// No message line detection? Abort
if(!line) return false;
// Fetch message metadata
// var room = line.attr('data-room');
var sender = line.attr('data-sender');
var sender = line.find('.from').html();
var badges = line.find('.badges');
var text = line.find('.message');
// No message text? Abort
if(!text.length) return false;
// If room and sender is undefined, the user is using vanilla Twitch
if(sender === undefined && line.find('.from').length){
sender = line.find('.from').html().toLowerCase();
}
// Parse message text
parseMessage(text, subEmotes);
parseMessage(text, bttvEmotes);
// Developers
if(sender.toLowerCase() === 'zarlach' && !badges.find('.submote-dev').length){
// Glow
line.find('.from').css('text-shadow', 'rgb(82, 142, 205) 0px 0px 20px');
// Badge
$(badges).append('<div class="badge float-left tooltip submote-dev" original-title="Submote Dev"></div>');
$(badges).find('.submote-dev').css({
'width': '18px',
'height': '18px',
'background-image': 'url(https://cdn.rawgit.com/Zarlach/Submote/master/images/dev-badge.png)',
'background-repat': 'no-repeat',
'background-size': '18px 18px'
});
}
if (sender.toLowerCase() === 'vopo70') {
line.find('.from').css('text-shadow', 'rgb(82, 142, 205) 0px 0px 20px');
}
return true;
}
/**
* Load subscriber emotes from web
*
*/
function loadSubEmotes()
{
$.getJSON('https://twitchemotes.com/api_cache/v2/subscriber.json', function(data)
{
$.each(data.channels, function(channel, properties)
{
$.each(properties.emotes, function(list, emote){
subEmotes[emote.code] = emote.image_id;
});
});
$.each(data.meta, function(key, val){
subEmotes[key] = val;
});
})
.done(function()
{
subEmotes.provider = 'twitch';
localStorage.setItem("subEmotes", JSON.stringify(subEmotes));
})
.fail(function()
{
//sendError('Submote could not load sub emotes');
console.log('Could not load sub emotes');
});
}
/**
* Load BetterTTV emotes from web
*
*/
function loadBetterTTVemotes()
{
$.getJSON('https://api.betterttv.net/2/emotes', function(data){
$.each(data.emotes, function(key, value){
bttvEmotes[value.code] = "https://cdn.betterttv.net/emote/" + value.id + "/1x";
});
})
.done(function(){
bttvEmotes.provider = 'betterttv';
bttvEmotes.generated_at = new Date();
localStorage.setItem("bttvEmotes", JSON.stringify(bttvEmotes));
})
.fail(function(){
console.log('Submote could not load BetterTTV emotes');
});
}
/**
* Load Custom Emotes
*/
function loadCustomEmotes()
{
/**
* To-do: generalize bttvEmotes into customEmotes
*
*/
bttvEmotes.aaaDuhface = 'https://static-cdn.jtvnw.net/emoticons/v1/6988/1.0';
bttvEmotes.PepePls = 'https://cdn.betterttv.net/emote/55898e122612142e6aaa935b/1x';
bttvEmotes.DogePls = 'https://cdn.betterttv.net/emote/55c7eb723d8fd22f20ac9cc1/1x';
bttvEmotes.sodaGpls = 'https://cdn.betterttv.net/emote/55c7d01ae9d8d91f2087ee34/1x';
bttvEmotes.SnoopPls = 'https://cdn.betterttv.net/emote/55a05e85cc07004a41f8b1d7/1x';
bttvEmotes.Ditto = 'https://cdn.betterttv.net/emote/554da1a289d53f2d12781907/1x';
bttvEmotes.FeelsOhWait = 'https://cdn.betterttv.net/emote/55ab96ce9406e5482db53424/1x';
bttvEmotes.ShakeItOff = 'https://cdn.betterttv.net/emote/55a9875be80089ed0bf297a0/1x';
// FrankerFaceZ
bttvEmotes.CatBag = 'https://cdn.frankerfacez.com/emoticon/25927/1';
bttvEmotes.CoolCatBag = 'https://cdn.frankerfacez.com/emoticon/41091/1';
bttvEmotes.LilZ = 'https://cdn.frankerfacez.com/emoticon/28136/1';
bttvEmotes.ZreknarF = 'https://cdn.frankerfacez.com/emoticon/1/1';
bttvEmotes.BeanieHipster = 'https://cdn.frankerfacez.com/emoticon/3/1';
bttvEmotes.ManChicken = 'https://cdn.frankerfacez.com/emoticon/4/1';
bttvEmotes.YellowFever = 'https://cdn.frankerfacez.com/emoticon/5/1';
bttvEmotes.YooHoo = 'https://cdn.frankerfacez.com/emoticon/6/1';
// Twitch Turbo
bttvEmotes.duDudu = 'https://static-cdn.jtvnw.net/emoticons/v1/23139/1.0';
bttvEmotes.KappaHD = 'https://static-cdn.jtvnw.net/emoticons/v1/3286/1.0';
bttvEmotes.MiniK = 'https://static-cdn.jtvnw.net/emoticons/v1/3287/1.0';
bttvEmotes.riPepperonis = 'https://static-cdn.jtvnw.net/emoticons/v1/23141/1.0';
localStorage.setItem("bttvEmotes", JSON.stringify(bttvEmotes));
}
/**
* Scan message and replace words with emotes!
*
* @param {element} ele Message element
* @param {object} list List of emotes
* @param {string} provider Emote provider
*/
function parseMessage(ele, list)
{
var msg = ele.html();
var split = msg.replace(/(<([^>]+)>)/ig,'').trim().split(' ');
split = $.grep(split,function(n){ return(n) });
var words = [];
var provider = list.provider;
var regex;
var word;
//Remove duplicates
$.each(split, function(i, el){
if($.inArray(el, words) === -1) words.push(el);
});
//
for (var i = 0; i <= words.length; i++) {
word = words[i];
if(validateFilter(word, list, provider))
{
regex = new RegExp('\\b'+word+'\\b(?=[^"]*(?:"[^"]*"[^"]*)*$)', 'g');
msg = msg.replace(regex, generateEmoteImage(word, list[word], provider));
}
}
ele.html(msg.trim());
}
/**
* Generate emote image HTML tag.
*
* @param {int} id Emote ID
* @return {string} HTML img tag
*/
function generateEmoteImage(emote, source, provider)
{
switch(provider){
case 'twitch':
return '<img class="emoticon ttv-emo-'+source+'" src="https://static-cdn.jtvnw.net/emoticons/v1/'+source+'/1.0" srcset="https://static-cdn.jtvnw.net/emoticons/v1/'+source+'/2.0 2x" data-id="'+source+'" data-regex="'+emote+'" alt="'+emote+'" original-title>';
case 'betterttv':
return '<img class="emoticon tooltip" src="'+source+'" data-regex="'+emote+'" original-title="'+emote+'">';
}
}
/**
* Run selected word through a filter
*
* @param {string} word Selected word
* @param {object} list Emote list
* @param {string} provider Emote provider
* @return {boolean} Returns whether or not the word passed the filter
*/
function validateFilter(word, list, provider)
{
var regex = new RegExp('^(?=.*[a-z])(?=.*[A-Z]).+$');
//General filter
if(list[word] === undefined
|| !regex.test(word)
|| word === 'provider'
|| word === 'generated_at'
|| word.length === 0)
return false;
//Provider specific filter
switch(provider){
case 'twitch':
if (word[0] !== word[0].toLowerCase()
|| word === 'double'
|| word === 'triple'
|| word === 'quadra'
|| word === 'penta'
|| word === 'kill'
|| word === 'snap')
return false;
break;
case 'betterttv':
break;
}
return true;
}
/**
* In case something fails, let the user know!
*
* @param {string} message Error description
*/
function sendError(message)
{
$('body').append('<ul id="noty_bottomCenter_layout_container" class="i-am-new" style="bottom: 20px; position: fixed; width: 320px; height: auto; margin: 0px; padding: 0px; list-style-type: none; z-index: 10000; left: 800px;"><li class="noty_bar alert" style="width: 320px;"><div class="noty_bar noty_type_alert" id="noty_152298130185963840"><div class="noty_message"><div class="text-container"><div class="glitch"></div><div class="noty_text"><p>'+message+'<br><a href="javascript:history.go(0);">Please refresh</a></p></div></div><div class="noty_close"></div></div></div></li></ul>');
}