Advertisement
Guest User

Untitled

a guest
Nov 18th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.38 KB | None | 0 0
  1. var channels = [localStorage.chn], // Channels to initially join
  2. user = localStorage.usr, //Username Identity
  3. pass = localStorage.pwd, // password Identity
  4. fadeDelay = 5000, // Set to false to disable chat fade
  5. showChannel = true, // Show repespective channels if the channels is longer than 1
  6. useColor = true, // Use chatters' colors or to inherit
  7. showBadges = false, // Show chatters' badges
  8. showEmotes = true, // Show emotes in the chat
  9. doTimeouts = true, // Hide the messages of people who are timed-out
  10. doChatClears = true, // Hide the chat from an entire channel
  11. showHosting = true, // Show when the channel is hosting or not
  12. showConnectionNotices = false; // Show messages like "Connected" and "Disconnected"
  13.  
  14.  
  15.  
  16.  
  17.  
  18. var chat = document.getElementById('chat'),
  19. defaultColors = ['rgb(255, 0, 0)','rgb(0, 0, 255)','rgb(0, 128, 0)','rgb(178, 34, 34)','rgb(255, 127, 80)','rgb(154, 205, 50)','rgb(255, 69, 0)','rgb(46, 139, 87)','rgb(218, 165, 32)','rgb(210, 105, 30)','rgb(95, 158, 160)','rgb(30, 144, 255)','rgb(255, 105, 180)','rgb(138, 43, 226)','rgb(0, 255, 127)'],
  20. randomColorsChosen = {},
  21. clientOptions = {
  22. options: {
  23. debug: true
  24. },
  25. identity: {
  26. username: user,
  27. password: pass
  28. },
  29. connection: {
  30. reconnect: true
  31. },
  32. channels: channels
  33. },
  34. client = new tmi.client(clientOptions);
  35.  
  36. function dehash(channel) {
  37. return channel.replace(/^#/, '');
  38. }
  39.  
  40. function capitalize(n) {
  41. return n[0].toUpperCase() + n.substr(1);
  42. }
  43.  
  44. function htmlEntities(html) {
  45. function it() {
  46. return html.map(function(n, i, arr) {
  47. if(n.length == 1) {
  48. return n.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
  49. return '&#'+i.charCodeAt(0)+';';
  50. });
  51. }
  52. return n;
  53. });
  54. }
  55. var isArray = Array.isArray(html);
  56. if(!isArray) {
  57. html = html.split('');
  58. }
  59. html = it(html);
  60. if(!isArray) html = html.join('');
  61. return html;
  62. }
  63. // OLD Format Emotes
  64. //function formatEmotes(text, emotes) {
  65. // var splitText = text.split('');
  66. // for(var i in emotes) {
  67. // var e = emotes[i];
  68. // for(var j in e) {
  69. // var mote = e[j];
  70. // if(typeof mote == 'string') {
  71. // mote = mote.split('-');
  72. // mote = [parseInt(mote[0]), parseInt(mote[1])];
  73. // var length = mote[1] - mote[0],
  74. // empty = Array.apply(null, new Array(length + 1)).map(function() { return '' });
  75. // splitText = splitText.slice(0, mote[0]).concat(empty).concat(splitText.slice(mote[1] + 1, splitText.length));
  76. // splitText.splice(mote[0], 1, '<img class="emoticon" src="http://static-cdn.jtvnw.net/emoticons/v1/' + i + '/3.0">');
  77. // }
  78. // }
  79. // }
  80. // return htmlEntities(splitText).join('')
  81. //}
  82.  
  83. function formatEmotes(text, emotes) {
  84. var splitText = text.split('');
  85. for(var i in emotes) {
  86. var e = emotes[i];
  87. for(var j in e) {
  88. var mote = e[j];
  89. if(typeof mote == 'string') {
  90. mote = mote.split('-');
  91. mote = [parseInt(mote[0]), parseInt(mote[1])];
  92. var length = mote[1] - mote[0],
  93. empty = Array.apply(null, new Array(length + 1)).map(function() { return '' });
  94. splitText = splitText.slice(0, mote[0]).concat(empty).concat(splitText.slice(mote[1] + 1, splitText.length));
  95. splitText.splice(mote[0], 1, '<img class="emoticon" src="http://static-cdn.jtvnw.net/emoticons/v1/' + i + '/3.0">');
  96. }
  97. }
  98. }
  99. return splitText.join('');
  100. }
  101.  
  102. function badges(chan, user, isBot) {
  103.  
  104. function createBadge(name) {
  105. var badge = document.createElement('div');
  106. badge.className = 'chat-badge-' + name;
  107. return badge;
  108. }
  109.  
  110. var chatBadges = document.createElement('span');
  111. chatBadges.className = 'chat-badges';
  112.  
  113. if(!isBot) {
  114. if(user.username == chan) {
  115. chatBadges.appendChild(createBadge('broadcaster'));
  116. }
  117. if(user['user-type']) {
  118. chatBadges.appendChild(createBadge(user['user-type']));
  119. }
  120. if(user.turbo) {
  121. chatBadges.appendChild(createBadge('turbo'));
  122. }
  123. }
  124. else {
  125. chatBadges.appendChild(createBadge('bot'));
  126. }
  127.  
  128. return chatBadges;
  129. }
  130.  
  131. function handleChat(channel, user, message, self) {
  132.  
  133. var chan = dehash(channel),
  134. name = user.username,
  135. chatLine = document.createElement('div'),
  136. chatChannel = document.createElement('span'),
  137. chatName = document.createElement('span'),
  138. chatColon = document.createElement('span'),
  139. chatMessage = document.createElement('span');
  140.  
  141. var color = useColor ? user.color : 'inherit';
  142. if(color === null) {
  143. if(!randomColorsChosen.hasOwnProperty(chan)) {
  144. randomColorsChosen[chan] = {};
  145. }
  146. if(randomColorsChosen[chan].hasOwnProperty(name)) {
  147. color = randomColorsChosen[chan][name];
  148. }
  149. else {
  150. color = defaultColors[Math.floor(Math.random()*defaultColors.length)];
  151. randomColorsChosen[chan][name] = color;
  152. }
  153. }
  154.  
  155. chatLine.className = 'chat-line';
  156. chatLine.dataset.username = name;
  157. chatLine.dataset.channel = channel;
  158.  
  159. if(user['message-type'] == 'action') {
  160. chatLine.className += ' chat-action';
  161. }
  162.  
  163. chatChannel.className = 'chat-channel';
  164. chatChannel.innerHTML = chan;
  165.  
  166. chatName.className = 'chat-name';
  167. chatName.style.color = color;
  168. chatName.innerHTML = user['display-name'] || name;
  169.  
  170. chatColon.className = 'chat-colon';
  171.  
  172. chatMessage.className = 'chat-message';
  173.  
  174. chatMessage.style.color = color;
  175. chatMessage.innerHTML = showEmotes ? formatEmotes(message, user.emotes) : htmlEntities(message);
  176.  
  177. if(client.opts.channels.length > 1 && showChannel) chatLine.appendChild(chatChannel);
  178. if(showBadges) chatLine.appendChild(badges(chan, user, self));
  179. chatLine.appendChild(chatName);
  180. chatLine.appendChild(chatColon);
  181. chatLine.appendChild(chatMessage);
  182.  
  183. chat.appendChild(chatLine);
  184. chat.insertBefore(chatLine, chat.childNodes[0]);
  185.  
  186. if(typeof fadeDelay == 'number') {
  187. setTimeout(function() {
  188. chatLine.dataset.faded = '';
  189. }, fadeDelay);
  190. }
  191.  
  192. if(chat.children.length > 50) {
  193. var oldMessages = [].slice.call(chat.children).slice(0, 10);
  194. for(var i in oldMessages) oldMessages[i].remove();
  195. }
  196.  
  197. }
  198.  
  199. function chatNotice(information, noticeFadeDelay, level, additionalClasses) {
  200. ele = document.createElement('div');
  201.  
  202. ele.className = 'chat-line chat-notice';
  203. ele.innerHTML = information;
  204.  
  205. if(additionalClasses !== undefined) {
  206. if(Array.isArray(additionalClasses)) {
  207. additionalClasses = additionalClasses.join(' ');
  208. }
  209. ele.className += ' ' + additionalClasses;
  210. }
  211.  
  212. if(typeof level == 'number' && level != 0) {
  213. ele.dataset.level = level;
  214. }
  215.  
  216. chat.appendChild(ele);
  217.  
  218. if(typeof noticeFadeDelay == 'number') {
  219. setTimeout(function() {
  220. ele.dataset.faded = '';
  221. }, noticeFadeDelay || 500);
  222. }
  223.  
  224. return ele;
  225. }
  226.  
  227. var recentTimeouts = {};
  228.  
  229. function timeout(channel, username) {
  230. if(!doTimeouts) return false;
  231. if(!recentTimeouts.hasOwnProperty(channel)) {
  232. recentTimeouts[channel] = {};
  233. }
  234. if(!recentTimeouts[channel].hasOwnProperty(username) || recentTimeouts[channel][username] + 1000*10 < +new Date) {
  235. recentTimeouts[channel][username] = +new Date;
  236. chatNotice(capitalize(username) + ' was timed-out in ' + capitalize(dehash(channel)), 1000, 1, 'chat-delete-timeout')
  237. };
  238. var toHide = document.querySelectorAll('.chat-line[data-channel="' + channel + '"][data-username="' + username + '"]:not(.chat-timedout) .chat-message');
  239. for(var i in toHide) {
  240. var h = toHide[i];
  241. if(typeof h == 'object') {
  242. h.innerText = '<Message deleted>';
  243. h.parentElement.className += ' chat-timedout';
  244. }
  245. }
  246. }
  247. function clearChat(channel) {
  248. if(!doChatClears) return false;
  249. var toHide = document.querySelectorAll('.chat-line[data-channel="' + channel + '"]');
  250. for(var i in toHide) {
  251. var h = toHide[i];
  252. if(typeof h == 'object') {
  253. h.className += ' chat-cleared';
  254. }
  255. }
  256. chatNotice('Chat was cleared in ' + capitalize(dehash(channel)), 1000, 1, 'chat-delete-clear')
  257. }
  258. function hosting(channel, target, viewers, unhost) {
  259. if(!showHosting) return false;
  260. if(viewers == '-') viewers = 0;
  261. var chan = dehash(channel);
  262. chan = capitalize(chan);
  263. if(!unhost) {
  264. var targ = capitalize(target);
  265. chatNotice(chan + ' is now hosting ' + targ + ' for ' + viewers + ' viewer' + (viewers !== 1 ? 's' : '') + '.', null, null, 'chat-hosting-yes');
  266. }
  267. else {
  268. chatNotice(chan + ' is no longer hosting.', null, null, 'chat-hosting-no');
  269. }
  270. }
  271.  
  272. client.addListener('message', handleChat);
  273. client.addListener('timeout', timeout);
  274. client.addListener('clearchat', clearChat);
  275. client.addListener('hosting', hosting);
  276. client.addListener('unhost', function(channel, viewers) { hosting(channel, null, viewers, true) });
  277.  
  278. var joinAccounced = [];
  279.  
  280. client.addListener('connecting', function (address, port) {
  281. if(showConnectionNotices) chatNotice('Connecting', 1000, -4, 'chat-connection-good-connecting');
  282. });
  283. client.addListener('logon', function () {
  284. if(showConnectionNotices) chatNotice('Authenticating', 1000, -3, 'chat-connection-good-logon');
  285. });
  286. client.addListener('connectfail', function () {
  287. if(showConnectionNotices) chatNotice('Connection failed', 1000, 3, 'chat-connection-bad-fail');
  288. });
  289. client.addListener('connected', function (address, port) {
  290. if(showConnectionNotices) chatNotice('Connected', 1000, -2, 'chat-connection-good-connected');
  291. joinAccounced = [];
  292. });
  293. client.addListener('disconnected', function (reason) {
  294. if(showConnectionNotices) chatNotice('Disconnected: ' + (reason || ''), 3000, 2, 'chat-connection-bad-disconnected');
  295. });
  296. client.addListener('reconnect', function () {
  297. if(showConnectionNotices) chatNotice('Reconnected', 1000, 'chat-connection-good-reconnect');
  298. });
  299. client.addListener('join', function (channel, username) {
  300. if(joinAccounced.indexOf(channel) == -1) {
  301. if(showConnectionNotices) chatNotice('Joined ' + capitalize(dehash(channel)), 1000, -1, 'chat-room-join');
  302. joinAccounced.push(channel);
  303. }
  304. });
  305. client.addListener('part', function (channel, username) {
  306. var index = joinAccounced.indexOf(channel);
  307. if(index > -1) {
  308. if(showConnectionNotices) chatNotice('Parted ' + capitalize(dehash(channel)), 1000, -1, 'chat-room-part');
  309. joinAccounced.splice(joinAccounced.indexOf(channel), 1)
  310. }
  311. });
  312.  
  313. client.addListener('crash', function () {
  314. chatNotice('Crashed', 10000, 4, 'chat-crash');
  315. });
  316.  
  317. client.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement