BetterDiscordApp-rauenzi/js/emotemodule.js

213 lines
7.8 KiB
JavaScript
Raw Normal View History

2015-08-26 10:47:30 +02:00
/* BetterDiscordApp EmoteModule JavaScript
* Version: 1.5
2015-08-26 10:47:30 +02:00
* Author: Jiiks | http://jiiks.net
2015-08-27 15:28:43 +02:00
* Date: 26/08/2015 - 15:29
2015-10-14 08:50:34 +02:00
* Last Update: 14/10/2015 - 09:48
2015-08-26 10:47:30 +02:00
* https://github.com/Jiiks/BetterDiscordApp
2015-08-26 16:21:02 +02:00
* Note: Due to conflicts autocapitalize only supports global emotes
2015-08-26 10:47:30 +02:00
*/
2015-08-25 13:26:36 +02:00
/*
* =Changelog=
* -v1.5
* --Twitchemotes.com api
*/
2015-08-26 16:21:02 +02:00
var emotesFfz = {};
var emotesBTTV = {};
var emotesTwitch = { "emotes": { "emote": { "image_id": 0 } } }; //for ide
var subEmotesTwitch = {};
2015-08-25 13:26:36 +02:00
2015-12-16 12:21:46 +01:00
function EmoteModule() {
}
2015-08-26 14:52:48 +02:00
2015-12-16 12:21:46 +01:00
EmoteModule.prototype.init = function() {
};
2015-08-29 08:55:06 +02:00
2015-12-16 12:21:46 +01:00
EmoteModule.prototype.getBlacklist = function() {
$.getJSON("https://cdn.rawgit.com/Jiiks/betterDiscordApp/"+_hash+"/emotefilter.json", function(data) { bemotes = data.blacklist; });
};
2015-08-25 14:43:53 +02:00
2015-08-29 08:55:06 +02:00
EmoteModule.prototype.obsCallback = function(mutation) {
var self = this;
2015-12-16 12:21:46 +01:00
if(!settingsCookie["bda-es-7"]) return;
2015-08-29 08:55:06 +02:00
for(var i = 0 ; i < mutation.addedNodes.length ; ++i) {
var next = mutation.addedNodes.item(i);
if(next) {
var nodes = self.getNodes(next);
for(var node in nodes) {
if(nodes.hasOwnProperty(node)) {
self.injectEmote(nodes[node]);
}
2015-08-29 08:55:06 +02:00
}
}
}
};
2015-08-25 14:43:53 +02:00
2015-08-26 14:52:48 +02:00
EmoteModule.prototype.getNodes = function(node) {
2015-08-25 14:56:32 +02:00
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);
}
2015-12-16 12:21:46 +01:00
2015-08-25 14:56:32 +02:00
return nodes;
};
2015-08-26 14:52:48 +02:00
2015-12-16 12:21:46 +01:00
var bemotes = [];
var spoilered = [];
2015-08-26 14:52:48 +02:00
EmoteModule.prototype.injectEmote = function(node) {
2015-08-25 14:56:32 +02:00
if(typeof emotesTwitch === 'undefined') return;
if(!node.parentElement) return;
var parent = node.parentElement;
2015-12-16 12:21:46 +01:00
2015-08-25 14:56:32 +02:00
if(parent.tagName != "SPAN") return;
2015-12-16 12:21:46 +01:00
var edited = false;
if($(parent.parentElement).hasClass("edited")) {
parent = parent.parentElement.parentElement.firstChild; //:D
edited = true;
}
//if(!$(parent.parentElement).hasClass("markup") && !$(parent.parentElement).hasClass("message-content")) return;
2015-08-25 14:56:32 +02:00
2015-12-16 12:21:46 +01:00
function inject() {
if(!$(parent.parentElement).hasClass("markup") && !$(parent.parentElement).hasClass("message-content")) { return; }
2015-08-25 14:56:32 +02:00
2015-12-16 12:21:46 +01:00
var parentInnerHTML = parent.innerHTML;
var words = parentInnerHTML.split(/\s+/g);
2015-08-25 14:56:32 +02:00
2015-12-16 12:21:46 +01:00
if(!words) return;
2015-12-16 12:21:46 +01:00
words.some(function(word) {
if(word.slice(0, 4) == "[!s]" ) {
parentInnerHTML = parentInnerHTML.replace("[!s]", "");
var markup = $(parent).parent();
var reactId = markup.attr("data-reactid");
if(spoilered.indexOf(reactId) > -1) {
return;
}
markup.addClass("spoiler");
markup.on("click", function() {
$(this).removeClass("spoiler");
spoilered.push($(this).attr("data-reactid"));
});
return;
2015-08-25 14:56:32 +02:00
}
2015-12-16 12:21:46 +01:00
if(word.length < 4) {
return;
}
2015-12-16 12:21:46 +01:00
if($.inArray(word, bemotes) != -1) return;
2015-08-25 14:56:32 +02:00
2015-12-16 12:21:46 +01:00
if (emotesTwitch.emotes.hasOwnProperty(word)) {
var len = Math.round(word.length / 4);
parentInnerHTML = parentInnerHTML.replace(word, '<img class="emote" alt="' + word.substr(0, len) + "\uFDD9" + word.substr(len, len) + "\uFDD9" + word.substr(len * 2, len) + "\uFDD9" + word.substr(len * 3) + '" src="' + twitchEmoteUrlStart + emotesTwitch.emotes[word].image_id + twitchEmoteUrlEnd + '" />');
return;
}
2015-12-16 12:21:46 +01:00
if (typeof emotesFfz !== 'undefined' && settingsCookie["bda-es-1"]) {
if (emotesFfz.hasOwnProperty(word)) {
var len = Math.round(word.length / 4);
var name = word.substr(0, len) + "\uFDD9" + word.substr(len, len) + "\uFDD9" + word.substr(len * 2, len) + "\uFDD9" + word.substr(len * 3);
var url = ffzEmoteUrlStart + emotesFfz[word] + ffzEmoteUrlEnd;
parentInnerHTML = parentInnerHTML.replace(word, '<div class="emotewrapper"><img class="emote" alt="' + name + '" src="' + url + '" /><input onclick=\'quickEmoteMenu.favorite(\"'+name+'\", \"'+url+'\");\' class="fav" title="Favorite!" type="button"></div>');
return;
}
}
2015-10-14 08:50:34 +02:00
2015-12-16 12:21:46 +01:00
if (typeof emotesBTTV !== 'undefined' && settingsCookie["bda-es-2"]) {
if (emotesBTTV.hasOwnProperty(word)) {
var len = Math.round(word.length / 4);
var name = word.substr(0, len) + "\uFDD9" + word.substr(len, len) + "\uFDD9" + word.substr(len * 2, len) + "\uFDD9" + word.substr(len * 3);
var url = emotesBTTV[word];
parentInnerHTML = parentInnerHTML.replace(word, '<div class="emotewrapper"><img class="emote" alt="' + name + '" src="' + url + '" /><input onclick=\'quickEmoteMenu.favorite(\"'+name+'\", \"'+url+'\");\' class="fav" title="Favorite!" type="button"></div>');
return;
}
}
if(typeof emotesBTTV2 !== 'undefined' && settingsCookie["bda-es-2"]) {
if(emotesBTTV2.hasOwnProperty(word)) {
var len = Math.round(word.length / 4);
var name = word.substr(0, len) + "\uFDD9" + word.substr(len, len) + "\uFDD9" + word.substr(len * 2, len) + "\uFDD9" + word.substr(len * 3);
var url = bttvEmoteUrlStart + emotesBTTV2[word] + bttvEmoteUrlEnd;
parentInnerHTML = parentInnerHTML.replace(word, '<div class="emotewrapper"><img class="emote" alt="' + name + '" src="' + url + '" /><input onclick=\'quickEmoteMenu.favorite(\"'+name+'\", \"'+url+'\");\' class="fav" title="Favorite!" type="button"></div>');
return;
}
}
if (subEmotesTwitch.hasOwnProperty(word)) {
var len = Math.round(word.length / 4);
var name = word.substr(0, len) + "\uFDD9" + word.substr(len, len) + "\uFDD9" + word.substr(len * 2, len) + "\uFDD9" + word.substr(len * 3);
var url = twitchEmoteUrlStart + subEmotesTwitch[word] + twitchEmoteUrlEnd;
parentInnerHTML = parentInnerHTML.replace(word, '<div class="emotewrapper"><img class="emote" alt="' + name + '" src="' + url + '" /><input onclick=\'quickEmoteMenu.favorite(\"'+name+'\", \"'+url+'\");\' class="fav" title="Favorite!" type="button"></div>');
return;
}
});
if(parent.parentElement == null) return;
var oldHeight = parent.parentElement.offsetHeight;
parent.innerHTML = parentInnerHTML.replace(new RegExp("\uFDD9", "g"), "");
var newHeight = parent.parentElement.offsetHeight;
//Scrollfix
var scrollPane = $(".scroller.messages").first();
scrollPane.scrollTop(scrollPane.scrollTop() + (newHeight - oldHeight));
}
if(edited) {
setTimeout(inject, 250);
} else {
inject();
}
};
2015-08-26 16:21:02 +02:00
EmoteModule.prototype.autoCapitalize = function() {
2015-12-16 12:21:46 +01:00
2015-08-26 16:21:02 +02:00
var self = this;
2015-08-31 15:36:28 +02:00
2015-12-16 12:21:46 +01:00
$('body').delegate($(".channel-textarea-inner textarea"), 'keyup change paste', function() {
2015-08-29 10:48:20 +02:00
if(!settingsCookie["bda-es-4"]) return;
2015-08-26 18:39:04 +02:00
2015-12-16 12:21:46 +01:00
var text = $(".channel-textarea-inner textarea").val();
2015-08-29 10:48:20 +02:00
if(text == undefined) return;
2015-08-26 16:21:02 +02:00
var lastWord = text.split(" ").pop();
if(lastWord.length > 3) {
var ret = self.capitalize(lastWord.toLowerCase());
if(ret != null) {
2015-12-16 12:21:46 +01:00
$(".channel-textarea-inner textarea").val(text.replace(lastWord, ret));
2015-08-26 16:21:02 +02:00
}
}
});
};
2015-08-26 16:21:02 +02:00
EmoteModule.prototype.capitalize = function(value) {
2015-12-16 12:21:46 +01:00
var res = emotesTwitch.emotes;
for(var p in res){
if(res.hasOwnProperty(p) && value == (p+ '').toLowerCase()){
return p;
}
2015-08-26 16:21:02 +02:00
}
};