-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkappa.js
134 lines (117 loc) · 3.32 KB
/
kappa.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
(($) => {
let twitchEmotesRegExp;
/**
* Check for previous data in localStorage
*/
try {
let previousStorage = localStorage.getItem('kappa-js');
} catch (e) {
let previousStorage = null;
}
/**
* Initialize KappaJS
* Check for Storage support & analyize previous data
* @return {true} [Storage support & previous data]
* @return {Promise} [No Storage support or no previous data]
*/
function init() {
if(typeof Storage !== 'undefined' && previousStorage !== null)
return true;
return new Promise(getTwitchEmotesPromise);
}
/**
* Get global.json from TwitchEmotes API using a Promise
* @param {resolve} res Promise resolver
* @param {reject} rej Promise rejector
*/
function getTwitchEmotesPromise(res, rej) {
$.get('https://twitchemotes.com/api_cache/v2/global.json',
(data) => {
if(typeof Storage !== 'undefined' && previousStorage !== null)
localStorage.setItem('kappa-js', JSON.stringify(data));
res(data);
});
}
/**
* Grab TwitchEmotes API or use previous Storage
* Attach KappaJS to browser window
*/
if(previousStorage === null) {
init().then((data) => {
window.KappaJS = data;
});
} else window.KappaJS = JSON.parse(localStorage.getItem('kappa-js'));
/**
* Initialize jQuery Plugin
* @return {this} DOM element
*/
$.fn.kappa = function(settings) {
/**
* Hoist `this`
* Needed if KappaJS is not ready
*/
let self = this;
/**
* Define default plugin configuration
* @param {String} {customClass} Custom class to added to generatated <img> tags
* @param {String} {emoteSize} Template size for emotes
*/
let config = $.extend({
customClass: null,
emoteSize: 'small'
}, settings);
/**
* Generator <img> tag
* @param {String} {image_id} Emote Id
* @return {String} Generated <img> tag
*/
function generateImgTag({image_id}, name) {
return [
'<img src="',
KappaJS.template[config.emoteSize].replace('{image_id}', image_id),
'" ',
(config.customClass === null) ? '' : `class="${config.customClass}" `,
'alt="',
name,
'">'
].join('');
}
/**
* Wait for KappaJS to be attached to window
* Replace text with emotes once attached
*/
function waitKappaJS() {
let watch = setInterval(() => {
if(typeof window.KappaJS !== 'undefined') {
replaceTextWithEmotes();
clearInterval(watch);
}
}, 500);
}
/**
* Loop through all emoteSize
* Find known emotes using RegExp
* Replace with generated <img> tag
*/
function replaceTextWithEmotes() {
if(typeof twitchEmotesRegExp === 'undefined')
twitchEmotesRegExp = new RegExp("\\b(" + Object.keys( KappaJS.emotes ).join("|") + ")\\b", "g");
$(self).each((i, el) => {
$(el).html(
$(el).html().replace(
twitchEmotesRegExp,
function(all, emote){ return generateImgTag(KappaJS.emotes[emote], emote) }
)
);
});
}
/**
* Check for KappaJS
* Start watcher if not ready
* Replace if ready
*/
if(typeof window.KappaJS === 'undefined') {
waitKappaJS();
} else replaceTextWithEmotes();
};
})(jQuery);