BetterRabbit

Improves the Rabbit experience

  1. // ==UserScript==
  2. // @name BetterRabbit
  3. // @namespace soulweaver
  4. // @description Improves the Rabbit experience
  5. // @include https://www.rabb.it/*
  6. // @version 1
  7. // @grant none
  8. // @require https://code.jquery.com/jquery-2.2.3.min.js
  9. // ==/UserScript==
  10.  
  11. var vObs;
  12. var twitchEmotes;
  13. var bttvEmotes;
  14. var gBttvEmotes = {};
  15.  
  16. $(document).ready(function() {
  17. //alert("Plese don't open chat until the 'Chat ready' alert appers, thanks! *click ok*");
  18. //MutationObserver helper
  19. var observeDOM = (function(){
  20. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
  21. eventListenerSupported = window.addEventListener;
  22.  
  23. return function(obj, callback){
  24. if( MutationObserver ){
  25. // define a new observer
  26. var obs = new MutationObserver(function(mutations, observer){
  27. if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
  28. callback(mutations, observer);
  29. });
  30. // have the observer observe foo for changes in children
  31. obs.observe( obj, { childList:true, subtree:true });
  32. }
  33. else if( eventListenerSupported ){I
  34. obj.addEventListener('DOMNodeInserted', callback, false);
  35. obj.addEventListener('DOMNodeRemoved', callback, false);
  36. }
  37. }
  38. })();
  39. // Download latest global emotes and store them in a variable
  40. $.getJSON('https://api.twitch.tv/kraken/chat/emoticon_images?emotesets=0', function(data) {
  41. twitchEmotes = data;
  42. });
  43. $.getJSON('https://cdn.rawgit.com/Jiiks/BetterDiscordApp/master/data/emotedata_bttv.json', function(data) {
  44. bttvEmotes = data;
  45. });
  46. $.getJSON('https://api.betterttv.net/2/emotes', function(data) {
  47. $.each(data.emotes, function(key, val) {
  48. gBttvEmotes[val.code] = val.id;
  49. });
  50. });
  51. // Apparently I suck at Javascript and $(document).on just doesn't work. kden. So hacky workaround
  52. setTimeout(function() {
  53. //alert('Chat ready');
  54. console.log('timedone');
  55. console.log(gBttvEmotes);
  56. $('.toolbarButton.chatButton > .noCounter > .icon').click(function() {
  57. if($(this).hasClass('on')) {
  58. console.log('Chat gone');
  59. vObs.disconnect();
  60. } else {
  61. console.log('chat here');
  62. setTimeout(function() {
  63. observeDOM(document.getElementsByClassName("messages")[0], function(mutation, observer){
  64. vObs = observer;
  65. //console.log(mutation);
  66. var message = $(mutation[0].addedNodes[0]).find('span')
  67. var messageContent = message[0].innerText;
  68. var messageWords = messageContent.split(/([^\s]+)([\s]|$)/g).filter(function(e){ return e});
  69. var scrollPos = $('.conversationMessageCollectionView').scrollTop();
  70. var scrollHeight = $('.conversationMessageCollectionView')[0].scrollHeight - $('.conversationMessageCollectionView')[0].clientHeight;
  71. function callback(array) {
  72. //console.log(array);
  73. var content = array.join('');
  74. message.replaceWith("<span class='messageBody'>" + content + "</span>");
  75. if (scrollPos >= scrollHeight) {
  76. $('.conversationMessageCollectionView').scrollTop(10000);
  77. }
  78. }
  79. var wordsDone = 0;
  80. messageWords.forEach(function(w, i, a) {
  81. //console.log('word: ' + w + " | index: " + i + " | array: " + a);
  82. twitchEmotes.emoticon_sets[0].forEach(function (emote, i2) {
  83. if (w == emote.code) {
  84. a[i] = "<img src='https://static-cdn.jtvnw.net/emoticons/v1/" + emote.id + "/1.0' alt='" + emote.code + "'>";
  85. }
  86. });
  87. if (gBttvEmotes.hasOwnProperty(w)) {
  88. a[i] = "<img src='https://cdn.betterttv.net/emote/" + gBttvEmotes[w] + "/1x'>";
  89. } else if (bttvEmotes.hasOwnProperty(w)) {
  90. a[i] = "<img src='https://cdn.betterttv.net/emote/" + bttvEmotes[w] + "/1x'>";
  91. }
  92. // if(twitchEmotes.emotes.hasOwnProperty(w)) {
  93. // a[i] = "<img src='https://static-cdn.jtvnw.net/emoticons/v1/" + twitchEmotes.emotes[w].image_id + "/1.0'>";
  94. // //console.log(twitchEmotes.emotes[w]);
  95. // };
  96. wordsDone++;
  97. if(wordsDone === a.length) {
  98. callback(a);
  99. }
  100. });
  101. });
  102. }, 1000)
  103. }
  104. });
  105. }, 6000);
  106. });
  107.  
  108.  
  109.  
  110. console.log('BetterRabbit loaded');