Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String.prototype.emotify = function(args)
- {
- var bttv_api_instance = args ? args.bttv_api_instance || (typeof bttvApi !== "undefined" ? bttvApi : null) : (typeof bttvApi !== "undefined" ? bttvApi : null);
- var ttv_api_instance = args ? args.ttv_api_instance || (typeof ttvApi !== "undefined" ? ttvApi : null) : (typeof ttvApi !== "undefined" ? ttvApi : null);
- var bttv_emote_parsing = args ? args.bttv_emote_parsing : true;
- //var bttv_pro_emote_parsing = args ? args.bttv_pro_emote_parsing : true;
- var bttv_allow_gif = args ? args.bttv_allow_gif : false;
- var ttv_emote_parsing = args ? args.ttv_emote_parsing : true;
- var msg = this;
- var return_msg = this;
- var word_arr = return_msg.split(" ");
- //get references to emote objects
- var twitch_global_emotes = ttv_api_instance ? ttv_api_instance.getGlobalEmotes() : {};
- var bttv_global_emotes = bttv_api_instance ? bttv_api_instance.getGlobalEmotes() : {};
- //twitch clientside parsing
- for(var i = 0; i < word_arr.length; ++i)
- {
- if(ttv_emote_parsing && (word_arr[i] in twitch_global_emotes))
- {
- var emote = twitch_global_emotes[word_arr[i]] || {};
- word_arr[i] = "<img class=\"emoticonImg\" src=\"https://static-cdn.jtvnw.net/emoticons/v1/" + emote.id + "/1.0\" data-id=\"" + emote.id + "\" data-regex=\"" + emote.code + "\" title=\"" + emote.code + "\">";
- }
- else if(bttv_emote_parsing && (word_arr[i] in bttv_global_emotes))
- {
- var emote = bttv_global_emotes[word_arr[i]] || {};
- if((emote.imageType == "png") || ((emote.imageType == "gif") && bttv_allow_gif))
- {
- word_arr[i] = "<img class=\"emoticonImg\" src=\"https://cdn.betterttv.net/emote/" + emote.id + "/1x\" data-id=\"" + emote.id + "\" data-regex=\"" + emote.code + "\" title=\"" + emote.code + "\nBttv Global Emote\">";
- }
- }
- }
- return_msg = word_arr.join(" ");
- return word_arr.join(" ");
- };
- function hnlBttvApi (args)
- {
- this.args_ = args || {};
- this.global_emotes_ = {};
- this.channel_emotes_ = {};
- this.pro_emotes_ = {};
- }
- hnlBttvApi.prototype = {
- constructor:hnlBttvApi,
- downloadGlobalEmotes:function()
- {
- httpGetRequest({
- url:"https://api.betterttv.net/2/emotes",
- onfinished:function(data){
- try
- {
- var response_obj = JSON.parse(data.response);
- var emotes = response_obj.emotes || [];
- for(var i = 0; i < emotes.length; ++i)
- {
- data.args.self.global_emotes_[emotes[i].code] = emotes[i];
- }
- data.args.self.onglobalemotesloaded();
- }
- catch(e)
- {
- }
- },
- onerror:function(data){},
- args: {self: this}
- });
- },
- downloadChannelEmotes:function(channel)
- {
- //not implemented
- return null;
- httpGetRequest({
- url:"https://api.betterttv.net/2/channels/" + channel,
- onfinished:function(data){
- try
- {
- var response_obj = JSON.parse(data.response);
- data.args.self.channel_emotes_[data.args.channel] = response_obj.emotes || [];
- data.args.self.channelemotesloaded(data.args.channel);
- }
- catch(e)
- {
- }
- },
- onerror:function(data){},
- args: {self: this, channel: channel}
- });
- },
- getGlobalEmotes:function()
- {
- return this.global_emotes_;
- },
- getChannelEmotes:function(channel)
- {
- return this.channel_emotes_[channel] || [];
- },
- setProData:function(data)
- {
- if(typeof data !== "object")
- {
- return false;
- }
- if(data.name && (typeof data.name === "string"))
- {
- this.pro_emotes_[data.name] = data;
- }
- },
- getProData:function(user_name)
- {
- return this.pro_emotes_[user_name] ? this.pro_emotes_[user_name] : null;
- },
- //EVENTS
- onglobalemotesloaded:function(){},
- onchannelemotesloaded:function(channel){}
- };
- function hnlTtvApi(args)
- {
- this.args_ = args || {};
- this.global_emotes_ = {};
- this.channel_emotes_ = {};
- }
- hnlTtvApi.prototype = {
- constructor:hnlTtvApi,
- downloadGlobalEmotes:function()
- {
- httpGetRequest({
- url:"https://api.twitch.tv/kraken/chat/emoticon_images",
- onfinished:function(data){
- try
- {
- var response_obj = JSON.parse(data.response);
- var emotes = response_obj.emoticons || [];
- for(var i = 0; i < emotes.length; ++i)
- {
- if(emotes[i].emoticon_set)
- {
- //sub emote
- if(!(emotes[i].emoticon_set in data.args.self.channel_emotes_))
- {
- data.args.self.channel_emotes_[emotes[i].emoticon_set] = {};
- }
- data.args.self.channel_emotes_[emotes[i].emoticon_set][emotes[i].code] = emotes[i];
- }
- else
- {
- //global emote
- data.args.self.global_emotes_[emotes[i].code] = emotes[i];
- }
- }
- data.args.self.onglobalemotesloaded();
- }
- catch(e)
- {
- console.log(e);
- }
- },
- onerror:function(data){},
- args: {self: this}
- });
- },
- getGlobalEmotes:function()
- {
- return this.global_emotes_;
- },
- getChannelEmotes:function(channel)
- {
- return this.channel_emotes_[channel] || {};
- },
- //EVENTS
- onglobalemotesloaded:function(){}
- };
- //functions
- function httpGetRequest(args)
- {
- args.onfinished = args.onfinished || function(data){};
- args.onerror = args.onerror || function(err){};
- args.url = args.url || "";
- var xhttp = new XMLHttpRequest();
- xhttp.args = args;
- xhttp.onreadystatechange = function()
- {
- if (xhttp.readyState == 4 && xhttp.status == 200)
- {
- try
- {
- this.args.onfinished({response: xhttp.response, args: this.args.args || {}});
- }
- catch(e)
- {
- this.args.onerror({error: e, args: this.args.args || {}});
- }
- }
- else if(xhttp.status == 404)
- {
- this.args.onerror({error: "request timed out", args: this.args.args || {}}); //maybe add channels fetched
- }
- };
- xhttp.open("GET", args.url, true);
- xhttp.send();
- }
- ttvApi = new hnlTtvApi;
- bttvApi = new hnlBttvApi;
- window.onload = function()
- {
- ttvApi.onglobalemotesloaded = function ()
- {
- console.log("emotes loaded!");
- console.log(ttvApi.getGlobalEmotes());
- //27
- console.log(ttvApi.getChannelEmotes(27));
- var test_msg = "hello what up Kappa Kappa 123 Kappaxd Kappa LUL";
- for(var i = 0; i < 10000; ++i)
- {
- //console.log(i);
- }
- console.log(test_msg.emotify());
- };
- ttvApi.downloadGlobalEmotes();
- bttvApi.onglobalemotesloaded = function ()
- {
- console.log("bttv emotes loaded!");
- console.log(bttvApi.getGlobalEmotes());
- //27
- var test_msg = "hello what up Kappa Kappa 123 Kappaxd Kappa LUL SourPls";
- console.log(test_msg.emotify());
- };
- bttvApi.downloadGlobalEmotes();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement