BetterDiscordApp-v2/js/emotemodule.js

77 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-08-26 10:47:30 +02:00
/* BetterDiscordApp EmoteModule JavaScript
* Version: 1.1
* Author: Jiiks | http://jiiks.net
* Date: 26/08/2015 - 11:46
* https://github.com/Jiiks/BetterDiscordApp
*/
2015-08-25 13:26:36 +02:00
2015-08-26 10:48:32 +02:00
var emoteObserver;
2015-08-26 10:47:30 +02:00
var emotesTwitch = {};
var ffzEnabled = false;
var bttvEnabled = false;
var emotesFfz = {};
var emotesBTTV = {};
2015-08-25 13:26:36 +02:00
2015-08-26 10:47:30 +02:00
function startEmoteModule() {
2015-08-26 10:48:32 +02:00
emoteObserver = new MutationObserver(function(mutations) {
2015-08-25 14:56:32 +02:00
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]);
}
}
}
});
});
2015-08-26 10:47:30 +02:00
}
2015-08-25 14:43:53 +02:00
2015-08-26 10:47:30 +02:00
function startEmoteObserver() {
2015-08-26 10:48:32 +02:00
emoteObserver.observe(document, {childList: true, subtree: true});
2015-08-26 10:47:30 +02:00
}
2015-08-25 14:43:53 +02:00
2015-08-25 14:56:32 +02:00
function getNodes(node) {
var next;
var nodes = [];
2015-08-25 14:43:53 +02:00
2015-08-25 14:56:32 +02:00
var treeWalker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false);
while(next = treeWalker.nextNode()) {
nodes.push(next);
}
return nodes;
2015-08-25 14:43:53 +02:00
}
2015-08-25 14:54:51 +02:00
2015-08-25 14:56:32 +02:00
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)) {
2015-08-26 10:47:30 +02:00
parentInnerHTML = parentInnerHTML.replace(word, "<img src=" + twitchEmoteUrlStart + emotesTwitch[word] + twitchEmoteUrlEnd + " title="+word+"><\/img>");
} else if(typeof emotesFfz !== 'undefined' && ffzEnabled) {
2015-08-25 14:56:32 +02:00
if(emotesFfz.hasOwnProperty(word)) {
2015-08-26 10:47:30 +02:00
parentInnerHTML = parentInnerHTML.replace(word, "<img src=" + ffzEmoteUrlStart + emotesFfz[word] + ffzEmoteUrlEnd + " title="+word+"><\/img>");
} else if(typeof emotesBTTV !== 'undefined' && bttvEnabled) {
if(emotesBTTV.hasOwnProperty(word)) {
parentInnerHTML = parentInnerHTML.replace(word, "<img src=" + bttvEmoteUrlStart + emotesBTTV[word] + bttvEmoteUrlEnd + " title="+word+"><\/img>");
}
2015-08-25 14:56:32 +02:00
}
}
});
parent.innerHTML = parentInnerHTML;
2015-08-26 10:47:30 +02:00
}