Advertisement
Guest User

Untitled

a guest
May 29th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. String.prototype.emotify = function(args)
  2. {
  3.     var bttv_api_instance = args ? args.bttv_api_instance || (typeof bttvApi !== "undefined" ? bttvApi : null) : (typeof bttvApi !== "undefined" ? bttvApi : null);
  4.     var ttv_api_instance = args ? args.ttv_api_instance || (typeof ttvApi !== "undefined" ? ttvApi : null) : (typeof ttvApi !== "undefined" ? ttvApi : null);
  5.    
  6.     var bttv_emote_parsing = args ? args.bttv_emote_parsing : true;
  7.     //var bttv_pro_emote_parsing = args ? args.bttv_pro_emote_parsing : true;
  8.     var bttv_allow_gif = args ? args.bttv_allow_gif : false;
  9.    
  10.     var ttv_emote_parsing = args ? args.ttv_emote_parsing : true;
  11.    
  12.  
  13.     var msg = this;
  14.     var return_msg = this;
  15.     var word_arr = return_msg.split(" ");
  16.  
  17.  
  18.     //get references to emote objects
  19.     var twitch_global_emotes =  ttv_api_instance ? ttv_api_instance.getGlobalEmotes() : {};
  20.     var bttv_global_emotes =  bttv_api_instance ? bttv_api_instance.getGlobalEmotes() : {};
  21.    
  22.    
  23.     //twitch clientside parsing
  24.     for(var i = 0; i < word_arr.length; ++i)
  25.     {
  26.         if(ttv_emote_parsing && (word_arr[i] in twitch_global_emotes))
  27.         {
  28.             var emote = twitch_global_emotes[word_arr[i]] || {};
  29.             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 + "\">";
  30.         }
  31.         else if(bttv_emote_parsing && (word_arr[i] in bttv_global_emotes))
  32.         {
  33.             var emote = bttv_global_emotes[word_arr[i]] || {};
  34.            
  35.             if((emote.imageType == "png") || ((emote.imageType == "gif") && bttv_allow_gif))
  36.             {
  37.                 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\">";
  38.             }
  39.         }
  40.     }
  41.  
  42.  
  43.     return_msg = word_arr.join(" ");
  44.    
  45.     return word_arr.join(" ");
  46. };
  47.  
  48.  
  49.  
  50. function hnlBttvApi (args)
  51. {
  52.     this.args_ = args || {};
  53.     this.global_emotes_ = {};
  54.     this.channel_emotes_ = {};
  55.     this.pro_emotes_ = {};
  56.  
  57. }
  58.  
  59. hnlBttvApi.prototype = {
  60.     constructor:hnlBttvApi,
  61.     downloadGlobalEmotes:function()
  62.     {
  63.         httpGetRequest({
  64.             url:"https://api.betterttv.net/2/emotes",
  65.             onfinished:function(data){
  66.                 try
  67.                 {
  68.                     var response_obj = JSON.parse(data.response);
  69.                     var emotes = response_obj.emotes || [];
  70.  
  71.                     for(var i = 0; i < emotes.length; ++i)
  72.                     {
  73.                         data.args.self.global_emotes_[emotes[i].code] = emotes[i];
  74.                     }
  75.  
  76.                     data.args.self.onglobalemotesloaded();
  77.                 }
  78.                 catch(e)
  79.                 {
  80.  
  81.                 }
  82.                
  83.             },
  84.             onerror:function(data){},
  85.             args: {self: this}
  86.         });
  87.     },
  88.     downloadChannelEmotes:function(channel)
  89.     {
  90.         //not implemented
  91.         return null;
  92.        
  93.         httpGetRequest({
  94.             url:"https://api.betterttv.net/2/channels/" + channel,
  95.             onfinished:function(data){
  96.                 try
  97.                 {
  98.                     var response_obj = JSON.parse(data.response);
  99.  
  100.                     data.args.self.channel_emotes_[data.args.channel] = response_obj.emotes || [];
  101.                     data.args.self.channelemotesloaded(data.args.channel);
  102.                 }
  103.                 catch(e)
  104.                 {
  105.  
  106.                 }
  107.                
  108.             },
  109.             onerror:function(data){},
  110.             args: {self: this, channel: channel}
  111.         });
  112.     },
  113.     getGlobalEmotes:function()
  114.     {
  115.         return this.global_emotes_;
  116.     },
  117.     getChannelEmotes:function(channel)
  118.     {
  119.         return this.channel_emotes_[channel] || [];
  120.     },
  121.     setProData:function(data)
  122.     {
  123.         if(typeof data !== "object")
  124.         {
  125.             return false;
  126.         }
  127.  
  128.         if(data.name && (typeof data.name === "string"))
  129.         {
  130.             this.pro_emotes_[data.name] = data;
  131.         }
  132.     },
  133.     getProData:function(user_name)
  134.     {
  135.         return this.pro_emotes_[user_name] ? this.pro_emotes_[user_name] : null;
  136.     },
  137.  
  138.  
  139.     //EVENTS
  140.     onglobalemotesloaded:function(){},
  141.     onchannelemotesloaded:function(channel){}
  142. };
  143.  
  144.  
  145.  
  146. function hnlTtvApi(args)
  147. {
  148.     this.args_ = args || {};
  149.     this.global_emotes_ = {};
  150.     this.channel_emotes_ = {};
  151. }
  152.  
  153. hnlTtvApi.prototype = {
  154.     constructor:hnlTtvApi,
  155.     downloadGlobalEmotes:function()
  156.     {
  157.         httpGetRequest({
  158.             url:"https://api.twitch.tv/kraken/chat/emoticon_images",
  159.             onfinished:function(data){
  160.                 try
  161.                 {
  162.                     var response_obj = JSON.parse(data.response);
  163.                     var emotes = response_obj.emoticons || [];
  164.  
  165.                     for(var i = 0; i < emotes.length; ++i)
  166.                     {
  167.                         if(emotes[i].emoticon_set)
  168.                         {
  169.                             //sub emote
  170.                             if(!(emotes[i].emoticon_set in data.args.self.channel_emotes_))
  171.                             {
  172.                                 data.args.self.channel_emotes_[emotes[i].emoticon_set] = {};
  173.                             }
  174.  
  175.                             data.args.self.channel_emotes_[emotes[i].emoticon_set][emotes[i].code] = emotes[i];
  176.                         }
  177.                         else
  178.                         {
  179.                             //global emote
  180.                             data.args.self.global_emotes_[emotes[i].code] = emotes[i];
  181.                         }
  182.                     }
  183.  
  184.                     data.args.self.onglobalemotesloaded();
  185.                 }
  186.                 catch(e)
  187.                 {
  188.                     console.log(e);
  189.                 }
  190.                
  191.             },
  192.             onerror:function(data){},
  193.             args: {self: this}
  194.         });
  195.  
  196.     },
  197.     getGlobalEmotes:function()
  198.     {
  199.         return this.global_emotes_;
  200.     },
  201.     getChannelEmotes:function(channel)
  202.     {
  203.         return this.channel_emotes_[channel] || {};
  204.     },
  205.  
  206.  
  207.     //EVENTS
  208.     onglobalemotesloaded:function(){}
  209. };
  210.  
  211. //functions
  212. function httpGetRequest(args)
  213. {
  214.     args.onfinished = args.onfinished || function(data){};
  215.     args.onerror = args.onerror || function(err){};
  216.     args.url = args.url || "";
  217.     var xhttp = new XMLHttpRequest();
  218.     xhttp.args = args;
  219.  
  220.     xhttp.onreadystatechange = function()
  221.     {
  222.         if (xhttp.readyState == 4 && xhttp.status == 200)
  223.         {
  224.             try
  225.             {
  226.                 this.args.onfinished({response: xhttp.response, args: this.args.args || {}});
  227.             }
  228.             catch(e)
  229.             {
  230.                 this.args.onerror({error: e, args: this.args.args || {}});
  231.             }
  232.         }
  233.         else if(xhttp.status == 404)
  234.         {
  235.             this.args.onerror({error: "request timed out", args: this.args.args || {}}); //maybe add channels fetched
  236.         }
  237.     };
  238.    
  239.     xhttp.open("GET", args.url, true);
  240.     xhttp.send();
  241. }
  242.  
  243.  
  244.  
  245.  
  246.  
  247. ttvApi = new hnlTtvApi;
  248. bttvApi = new hnlBttvApi;
  249.  
  250.  
  251. window.onload = function()
  252. {
  253.     ttvApi.onglobalemotesloaded = function ()
  254.     {
  255.         console.log("emotes loaded!");
  256.         console.log(ttvApi.getGlobalEmotes());
  257.         //27
  258.         console.log(ttvApi.getChannelEmotes(27));
  259.        
  260.        
  261.         var test_msg = "hello what up Kappa Kappa 123 Kappaxd Kappa LUL";
  262.        
  263.    
  264.         for(var i = 0; i < 10000; ++i)
  265.         {
  266.             //console.log(i);
  267.         }
  268.         console.log(test_msg.emotify());
  269.     };
  270.    
  271.     ttvApi.downloadGlobalEmotes();
  272.    
  273.    
  274.     bttvApi.onglobalemotesloaded = function ()
  275.     {
  276.         console.log("bttv emotes loaded!");
  277.         console.log(bttvApi.getGlobalEmotes());
  278.         //27
  279.    
  280.         var test_msg = "hello what up Kappa Kappa 123 Kappaxd Kappa LUL SourPls";
  281.        
  282.  
  283.         console.log(test_msg.emotify());
  284.     };
  285.    
  286.     bttvApi.downloadGlobalEmotes();
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement