This commit is contained in:
Jiiks 2015-08-25 15:54:51 +03:00
parent d054daaaaf
commit 74fb0eb261
1 changed files with 25 additions and 56 deletions

View File

@ -1,62 +1,31 @@
var observer;
/* BetterDiscordApp Emote Module aka TwitchCord
* See https://github.com/Jiiks/BetterDiscordApp/blob/master/js/emotemodule.js
* Version: 1.0
* Author: Jiiks | http://jiiks.net
* Date: 25/08/2015 - 09:33
* https://github.com/Jiiks/BetterDiscordApp
*/
(function() {
var config = require("../config.json");
observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
for(var i = 0 ; i < mutation.addedNodes.length ; ++i) {
var next = mutation.addedNodes.item(i);
if(next) {
var nodes = getNodes(next);
for(var node in nodes) {
injectEmote(nodes[node]);
}
}
}
var _helper;
function EmoteModule(helper) {
_helper = helper;
_helper.log("Emotes Initialized");
_helper.execJs('var twitchEmoteUrlStart = "' + config.EmoteModule.Twitch.EmoteUrlStart + '", twitchEmoteUrlEnd = "' + config.EmoteModule.Twitch.EmoteUrlEnd + '";');
_helper.execJs('var ffzEmoteUrlStart = "' + config.EmoteModule.FrankerFaceZ.EmoteUrlStart + '", ffzEmoteUrlEnd = "' + config.EmoteModule.FrankerFaceZ.EmoteUrlEnd + '";');
_helper.execJs('var bttvEmoteUrlStart = "' + config.EmoteModule.BetterTTV.EmoteUrlStart + '", bttvEmoteUrlEnd = "' + config.EmoteModule.BetterTTV.EmoteUrlEnd + '";');
_helper.download(config.Urls.Cdn + "master/" + config.EmoteModule.Twitch.EmoteData, function(twitchEmoteData) {
_helper.execJs("var emotesTwitch = " + twitchEmoteData + ";");
_helper.download(config.Urls.Cdn + "master/" + config.EmoteModule.FrankerFaceZ.EmoteData, function(ffzEmoteData) {
_helper.execJs("var emotesFfz = " + ffzEmoteData + ";");
_helper.injectJavaScript(config.Cdn + config.js.EmoteModule);
});
});
observer.observe(document, {childList: true, subtree: true});
})();
function getNodes(node) {
var next;
var nodes = [];
var treeWalker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false);
while(next = treeWalker.nextNode()) {
nodes.push(next);
}
return nodes;
}
function injectEmote(node) {
if(typeof emotesTwitch === 'undefined') return;
if(!node.parentElement) return;
var parent = node.parentElement;
if(parent.tagName != "SPAN") return;
var parentInnerHTML = parent.innerHTML;
var words = parentInnerHTML.split(" ");
if(!words) return;
words.some(function(word) {
if (emotesTwitch.hasOwnProperty(word)) {
parentInnerHTML = parentInnerHTML.replace(word, "<img src=" + twitchEmoteUrlStart + emotesTwitch[word] + twitchEmoteUrlEnd + "><\/img>");
} else if(typeof emotesFfz !== 'undefined') {
if(emotesFfz.hasOwnProperty(word)) {
parentInnerHTML = parentInnerHTML.replace(word, "<img src=" + ffzEmoteUrlStart + emotesFfz[word] + ffzEmoteUrlEnd + "><\/img>");
}
}
});
parent.innerHTML = parentInnerHTML;
}
exports.EmoteModule = EmoteModule;