Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. /*
  2. * tabcompletion, emotes and usernames
  3. * also overwrite native tabcompletion
  4. */
  5.  
  6. $('#chatline').unbind('keydown',tabSuggestions || function(){});
  7. var tabSuggestions = (function(){
  8.  
  9. var chatline = document.getElementById('chatline'),
  10. suggestionsDiv = document.getElementById('suggestionsDiv') || document.createElement('div'),
  11. lastMatchNum = 0,
  12. lastMatch;
  13. suggestionsDiv.id = 'suggestionsDiv',
  14. suggestionsDiv.style.position = 'absolute',
  15. suggestionsDiv.style.zIndex = '999',
  16. suggestionsDiv.style.backgroundColor = '#161a20',
  17. chatline.parentNode.appendChild(suggestionsDiv)
  18.  
  19. function emoteImg(emote){
  20. var i = document.createElement('img')
  21. i.className = 'channel-emote',
  22. i.src = emote.image,
  23. i.title = emote.name,
  24. i.onclick = function(e){
  25. clearBorders();
  26. chatline.value = chatline.value.replace(/\/[a-z0-9?!&]*$/i, emote.name),
  27. // als er geen spatie of dubbele punt voor de /emote staat, voeg een spatie toe (anders werkt de emote niet)
  28. chatline.value.lastIndexOf('/') !== 0 && // check eerst of de / niet het eerste teken van de string is
  29. [' ',':'].indexOf(chatline.value[chatline.value.lastIndexOf('/')-1]) === -1 && // staat er iets anders dan een spatie of : voor de /emote ?
  30. (chatline.value = chatline.value.replace(/\/[a-z0-9?!&]*$/i, ' ' + emote.name)), // zet een spatie voor de /emote
  31. this.style.border = '2px solid white',
  32. chatline.focus();
  33. var i = 0, a = this;
  34. while (a = a.previousSibling) i++;
  35. lastMatchNum = i
  36. }
  37. return i
  38. }
  39.  
  40. function clearBorders(){
  41. for (var i = 0; i < suggestionsDiv.children.length; i++)
  42. suggestionsDiv.children[i].style.border = 'none'
  43. }
  44.  
  45. function levenshtein(a, b) {
  46. if (a.length === 0) return b.length
  47. if (b.length === 0) return a.length
  48. let tmp, i, j, prev, val, row
  49. // swap to save some memory O(min(a,b)) instead of O(a)
  50. if (a.length > b.length) {
  51. tmp = a
  52. a = b
  53. b = tmp
  54. }
  55.  
  56. row = Array(a.length + 1)
  57. // init the row
  58. for (i = 0; i <= a.length; i++) {
  59. row[i] = i
  60. }
  61.  
  62. // fill in the rest
  63. for (i = 1; i <= b.length; i++) {
  64. prev = i
  65. for (j = 1; j <= a.length; j++) {
  66. if (b[i - 1] === a[j - 1]) {
  67. val = row[j - 1] // match
  68. } else {
  69. val = Math.min(row[j - 1] + 1, // substitution
  70. Math.min(prev + 1, // insertion
  71. row[j] + 1)) // deletion
  72. }
  73. row[j - 1] = prev
  74. prev = val
  75. }
  76. row[a.length] = prev
  77. }
  78. return row[a.length]
  79. }
  80.  
  81. return function(e){
  82. if (e.keyCode != 9 && !e.shiftKey)
  83. suggestionsDiv.innerHTML = '',
  84. lastMatch = null
  85. else if (e.keyCode == 9){
  86. //emotes
  87. if (lastMatch) {
  88. //toggle matches
  89. if (Object.prototype.toString.call(lastMatch) === '[object Array]')
  90. //usernames
  91. chatline.value = chatline.value.replace(/[a-z0-9-]+$/i, lastMatch[++lastMatchNum % lastMatch.length])
  92. else
  93. //emotes
  94. e.shiftKey ? lastMatchNum = (--lastMatchNum + suggestionsDiv.children.length) % suggestionsDiv.children.length :
  95. lastMatchNum = ++lastMatchNum % suggestionsDiv.children.length,
  96. clearBorders(),suggestionsDiv.children[lastMatchNum] && suggestionsDiv.children[lastMatchNum].onclick()
  97. } else {
  98.  
  99. var emoteStr = chatline.value.match(/\/[a-z0-9?!&]*$/i)
  100. if (emoteStr){
  101. //show emotesuggestions
  102. var emoteList = CHANNEL.emotes.slice(),
  103. suggestions = []
  104. //find matches based on levenshtein distance
  105. emoteStr = emoteStr[0].replace(/\?/g,'\\?')
  106. for (var i = 0, emoteStr_withoutslash = emoteStr.replace('/',''); i < emoteList.length; i++)
  107. if (emoteList[i].name.match(emoteStr_withoutslash) || levenshtein(emoteStr, emoteList[i].name) < 3)
  108. suggestions.push(new emoteImg(emoteList[i]))
  109. //sort suggestions
  110. suggestions.sort(function(a, b){
  111. var indexofa = a.title.indexOf(emoteStr),
  112. indexofb = b.title.indexOf(emoteStr)
  113.  
  114. if (indexofa === 0 && indexofb !== 0)
  115. return -1
  116. else if (indexofb === 0 && indexofa !== 0)
  117. return 1
  118. else {
  119. //sort aphabetically with respect for numbers at the end
  120. var i = 0;
  121. while (a.title.charAt(i) === b.title.charAt(i) && i < a.title.length && i < b.title.length)
  122. i++;
  123. if (a.title.length !== b.title.length &&
  124. (i == a.title.length - 1 || i == b.title.length - 1) &&
  125. !isNaN(parseInt(a.title.charAt(i))) && !isNaN(parseInt(b.title.charAt(i))))
  126. return a.title.charAt(i) < b.title.charAt(i) ? 1 : -1
  127. return a.title.charAt(i) < b.title.charAt(i) ? -1 : 1
  128. }
  129. })
  130. //populate suggestionsDiv
  131. suggestionsDiv.innerHTML = ''
  132. for (var i=0; i<suggestions.length; i++)
  133. suggestionsDiv.appendChild(suggestions[i])
  134. lastMatchNum = 0,
  135. lastMatch = emoteStr,
  136. suggestionsDiv.children[0] && (suggestionsDiv.children[0].style.border = '2px solid white') && suggestionsDiv.children[lastMatchNum].onclick()
  137.  
  138. } else {
  139. //usernames
  140. var userStr = chatline.value.match(/[a-z0-9-]+$/i),
  141. userlistElems = $('#userlist .userlist_item');//document.getElementById("userlist").children
  142. lastMatch = [],
  143. lastMatchNum = 0
  144. for (var i = 0; i < userlistElems.length; i++)
  145. if (userlistElems[i].children[1].textContent != CLIENT.name &&
  146. userlistElems[i].children[1].textContent.match(new RegExp('^' + userStr, 'i')))
  147. lastMatch.push(userlistElems[i].children[1].textContent);
  148. if (lastMatch.length != 0)
  149. chatline.value = chatline.value.replace(/[a-z0-9-]+$/i, lastMatch[0])
  150. else
  151. lastMatch = null
  152. }
  153. }
  154. }
  155. }
  156. })()
  157.  
  158. chatTabComplete = function(){return false},
  159. $("#chatline").keydown(tabSuggestions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement