Skip to content

Commit e95cb4f

Browse files
committed
get api and attach to window
1 parent f38e9ea commit e95cb4f

9 files changed

+178
-85
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
docs
1+
node_modules

.jshintrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"browser": true,
3+
"esnext": true,
4+
"predef": [
5+
"jQuery"
6+
]
7+
}

license.txt LINCENSE.md

File renamed without changes.

dist/kappa.js

+28-83
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,36 @@
1-
(function($) {
2-
$.fn.kappa = function (options) {
1+
'use strict';
32

4-
// Init global variables
5-
var target = $(this);
6-
var API, EMOTES;
7-
var TEMPLATE_SMALL, TEMPLATE_MEDIUM, TEMPLATE_LARGE;
3+
(function ($) {
84

9-
var settings = $.extend({
10-
emoteSize: 'small',
11-
customClass: null
12-
}, options);
5+
var previousStorage = window.localStorage.getItem('kappa-js');
6+
7+
function init() {
8+
if (typeof Storage !== 'undefined' && previousStorage !== null) return window.localStorage.getItem('kappa-js');
139

14-
// Check for local storage support
15-
if(typeof(Storage) !== 'undefined') {
16-
if(sessionStorage.getItem('twitchEmotesAPI') === null) {
17-
// GET list of Global Emotes from Twitch Emotes API v2
18-
$.ajax({
19-
url: 'http://twitchemotes.com/api_cache/v2/global.json',
20-
success: function(data) {
21-
sessionStorage.setItem('twitchEmotesAPI', JSON.stringify(data));
22-
define(sessionStorage.getItem('twitchEmotesAPI'));
23-
},
24-
error: function() {
25-
console.log('Connection to Twitch Emotes API v2 has failed.');
26-
}
27-
});
28-
} else {
29-
define(sessionStorage.getItem('twitchEmotesAPI'));
30-
}
31-
} else {
32-
// GET list of Global Emotes from Twitch Emotes API v2
33-
$.ajax({
34-
url: 'http://twitchemotes.com/api_cache/v2/global.json',
35-
success: function(data) {
36-
define(JSON.stringify(data));
37-
},
38-
error: function() {
39-
console.log('Connection to Twitch Emotes API v2 has failed.');
40-
}
41-
});
42-
}
10+
return new Promise(getTwitchEmotesPromise);
11+
}
4312

44-
// Define global variables Urls
45-
function define(data) {
46-
API = JSON.parse(data);
47-
EMOTES = API.emotes;
48-
TEMPLATES = API.template;
13+
function getTwitchEmotesPromise(res, rej) {
14+
$.get('https://twitchemotes.com/api_cache/v2/global.json', function (data) {
15+
if (previousStorage === null) window.localStorage.setItem('kappa-js', JSON.stringify(data));
4916

50-
insertEmotes();
51-
}
17+
res(data);
18+
});
19+
}
5220

53-
// Determine Template size from options
54-
function getSize() {
55-
switch(settings.emoteSize) {
56-
case 'small':
57-
return TEMPLATES.small;
58-
case 'medium':
59-
return TEMPLATES.medium;
60-
case 'large':
61-
return TEMPLATES.large;
62-
}
63-
}
21+
if (previousStorage === null) {
22+
init().then(function (data) {
23+
window.KappaJS = data;
24+
});
25+
} else window.KappaJS = window.localStorage.getItem('kappa-js');
6426

65-
// Determine if custom class is preset
66-
function getCustomClass() {
67-
if(settings.customClass !== null) {
68-
return ' class="' + settings.customClass + '" ';
69-
} else {
70-
return '';
71-
}
72-
}
27+
$.fn.kappa = function () {
28+
29+
var config = $.extend({
30+
emoteSize: 'small',
31+
customClass: null
32+
});
7333

74-
// Insert Emotes
75-
function insertEmotes() {
76-
var currentEmoteID;
77-
var currentTemplate = getSize();
78-
var customClass = getCustomClass();
79-
$.each(EMOTES, function(i, val) {
80-
currentEmoteID = val.image_id;
81-
currentEmoteUrl = currentTemplate.replace('{image_id}', currentEmoteID);
82-
target.each(function(index, element){
83-
$(element).html(function(index, text) {
84-
var re = new RegExp('\\b'+i+'\\b', 'g');
85-
this.innerHTML = text.replace(re, '<img src="' + currentEmoteUrl + '" alt="' + i + ' Emote"' + customClass + '>');
86-
});
87-
});
88-
});
89-
}
90-
};
91-
})(jQuery);
34+
return undefined;
35+
};
36+
})(jQuery);

dist/kappa.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Require dependencies
3+
*/
4+
const gulp = require('gulp'),
5+
babel = require('gulp-babel'),
6+
browserSync = require('browser-sync'),
7+
rename = require('gulp-rename'),
8+
uglify = require('gulp-uglify');
9+
10+
11+
/**
12+
* gulp:default
13+
* Calls gulp:watch
14+
*/
15+
gulp.task('default', ['watch']);
16+
17+
/**
18+
* gulp:watch
19+
* Start browserSync and watch for src changes
20+
*/
21+
gulp.task('watch', () => {
22+
browserSync.init({
23+
server: {
24+
baseDir: '.'
25+
}
26+
});
27+
28+
gulp.watch('src/kappa.js', ['build']).on('change', browserSync.reload);
29+
});
30+
31+
/**
32+
* gulp:build
33+
* Transpile src from es6 and save to dist
34+
*/
35+
gulp.task('build', () => {
36+
return gulp.src('src/kappa.js')
37+
.pipe(babel({
38+
presets: ['es2015']
39+
}))
40+
.pipe(gulp.dest('dist'))
41+
.pipe(uglify())
42+
.pipe(rename('kappa.min.js'))
43+
.pipe(gulp.dest('dist'));
44+
});

index.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Kappa.js</title>
6+
</head>
7+
<body>
8+
<span class="kappa">Kappa</span>
9+
10+
11+
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
12+
<script src="dist/kappa.js"></script>
13+
14+
<script>
15+
$(document).ready(function() {
16+
// $('.kappa').kappa();
17+
});
18+
</script>
19+
</body>
20+
</html>

package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "kappa.js",
3+
"version": "1.0.0",
4+
"description": "Kappa.js is a simple jQuery Plugin that can easily find text Twitch emotes and replace them with their respective emoticons. ",
5+
"main": "kappa.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/simulatedgreg/kappa.js.git"
12+
},
13+
"keywords": [
14+
"twitch",
15+
"kappa",
16+
"emote",
17+
"text",
18+
"replace",
19+
"stream",
20+
"emoticon"
21+
],
22+
"author": "SimulatedGREG <[email protected]>",
23+
"license": "ISC",
24+
"bugs": {
25+
"url": "https://github.com/simulatedgreg/kappa.js/issues"
26+
},
27+
"homepage": "https://github.com/simulatedgreg/kappa.js#readme",
28+
"devDependencies": {
29+
"babel-preset-es2015": "^6.5.0",
30+
"babel-preset-stage-0": "^6.5.0",
31+
"browser-sync": "^2.11.1",
32+
"gulp": "^3.9.1",
33+
"gulp-babel": "^6.1.2",
34+
"gulp-rename": "^1.2.2",
35+
"gulp-uglify": "^1.5.2"
36+
}
37+
}

src/kappa.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
(($) => {
2+
3+
let previousStorage = window.localStorage.getItem('kappa-js');
4+
5+
function init() {
6+
if(typeof Storage !== 'undefined' && previousStorage !== null)
7+
return window.localStorage.getItem('kappa-js');
8+
9+
return new Promise(getTwitchEmotesPromise);
10+
}
11+
12+
function getTwitchEmotesPromise(res, rej) {
13+
$.get('https://twitchemotes.com/api_cache/v2/global.json',
14+
(data) => {
15+
if(previousStorage === null)
16+
window.localStorage.setItem('kappa-js', JSON.stringify(data));
17+
18+
res(data);
19+
});
20+
}
21+
22+
if(previousStorage === null) {
23+
init().then((data) => {
24+
window.KappaJS = data;
25+
});
26+
} else window.KappaJS = window.localStorage.getItem('kappa-js');
27+
28+
29+
$.fn.kappa = () => {
30+
31+
let config = $.extend({
32+
emoteSize: 'small',
33+
customClass: null
34+
});
35+
36+
return this;
37+
38+
};
39+
40+
})(jQuery);

0 commit comments

Comments
 (0)