TorViet Shoutbox Enhancer

A small script to simplify the shoutbox

目前為 2015-05-16 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name TorViet Shoutbox Enhancer
  3. // @namespace http://torviet.com/userdetails.php?id=1662
  4. // @version 0.5
  5. // @license http://www.wtfpl.net/txt/copying/
  6. // @homepageURL https://github.com/S-a-l-a-d/TorViet-Shoutbox-Enhancer
  7. // @supportURL https://github.com/S-a-l-a-d/TorViet-Shoutbox-Enhancer/issues
  8. // @icon http://torviet.com/pic/salad.png
  9. // @description A small script to simplify the shoutbox
  10. // @author Salad
  11. // @match http://torviet.com/qa.php*
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. // Declare variables
  17. var boxHead = document.getElementById('boxHead'),
  18. marquee = document.getElementsByClassName('marquee')[0],
  19. sltTheme = document.getElementById('sltTheme'),
  20. clock = document.getElementById('clock'),
  21. allWrapper = document.getElementsByClassName('all-wrapper')[0],
  22. inputSection = document.getElementsByClassName('input-section')[0],
  23. idQuestion = document.getElementById('idQuestion'),
  24. navigationPage = document.getElementsByClassName('navigation_page')[0],
  25. boxQuestion = document.getElementById('boxQuestion'),
  26. emoGroup = document.getElementById('emogroup'),
  27. emoGroupDetail = document.getElementsByClassName('emo-group-detail')[0];
  28.  
  29. // Remove unneeded elements.
  30. boxHead.parentNode.removeChild(boxHead);
  31. marquee.parentNode.removeChild(marquee);
  32. sltTheme.parentNode.removeChild(sltTheme);
  33. clock.parentNode.removeChild(clock);
  34.  
  35. // Alter existing element CSS.
  36. var windowHeight = window.innerHeight;
  37. var remainingHeight = inputSection.parentNode.offsetHeight + navigationPage.offsetHeight - 100;
  38.  
  39. allWrapper.style.backgroundImage = 'none';
  40. allWrapper.style.margin = 'auto';
  41. allWrapper.style.height = windowHeight + 'px';
  42. inputSection.parentNode.style.padding = '0px';
  43. navigationPage.style.width = 'auto';
  44. boxQuestion.style.height = windowHeight - remainingHeight + 2 + 'px';
  45. emoGroupDetail.parentNode.parentNode.style.height = windowHeight - remainingHeight + 'px';
  46. emoGroupDetail.parentNode.style.height =
  47. emoGroupDetail.style.height = windowHeight - remainingHeight - 32 + 'px';
  48.  
  49. // Alter existing elements.
  50. emoGroupDetail.innerHTML = getEmoticons(524, 574) + getEmoticons(707) + getEmoticons(200, 234);
  51.  
  52. // Add elements.
  53. var btnToggle = document.createElement('input');
  54. btnToggle.type = 'button';
  55. btnToggle.value = 'Toggle';
  56. btnToggle.onclick = toggleEmoSlt;
  57. idQuestion.parentNode.insertBefore(btnToggle, null);
  58.  
  59. // Add event listeners
  60. // Firefox detection.
  61. if (typeof InstallTrigger !== 'undefined')
  62. document.addEventListener('keypress', keyEvent);
  63. else
  64. document.addEventListener('keydown', keyEvent);
  65.  
  66. // Custom functions.
  67. function toggleEmoSlt() {
  68. emoGroup.offsetParent ? emoGroup.parentNode.style.display = 'none' : emoGroup.parentNode.style.display = 'block';
  69. }
  70.  
  71. function getEmoticons(start, end) {
  72. var emos = '';
  73.  
  74. if (end === undefined)
  75. emos = '<div style="height:43px;width:43px;float:left;display:inline-block;margin:1px;"><a style="margin:0px 0px 0px 0px;" class="btuEmotion" alt="[em' + start +
  76. ']"><img style="max-width: 43px; max-height: 43px" src="/pic/smilies/' + start +
  77. '.gif" alt=""></a></div>';
  78. else
  79. for (i = start; i <= end; i++)
  80. emos += '<div style="height:43px;width:43px;float:left;display:inline-block;margin:1px;"><a style="margin:0px 0px 0px 0px;" class="btuEmotion" alt="[em' + i +
  81. ']"><img style="max-width: 43px; max-height: 43px" src="/pic/smilies/' + i +
  82. '.gif" alt=""></a></div>';
  83.  
  84. return emos;
  85. }
  86.  
  87. function keyEvent(e) {
  88. switch (e.keyCode) {
  89. case 40:
  90. if (emoGroup.selectedIndex !== emoGroup.length - 1)
  91. emoGroup.selectedIndex++;
  92. changeEmoGroup();
  93. break;
  94. case 38:
  95. if (emoGroup.selectedIndex !== 0)
  96. emoGroup.selectedIndex--;
  97. changeEmoGroup();
  98. break;
  99. default:
  100. }
  101. }
  102.  
  103. function changeEmoGroup() {
  104. var request = new XMLHttpRequest();
  105. request.open('POST', 'qa_smiley_ajax.php', true);
  106. request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  107. request.onreadystatechange = function() {
  108. if (request.readyState == 4 && request.status == 200) {
  109. emoGroupDetail.innerHTML = JSON.parse(request.responseText).str;
  110. addEmoGroupEvent();
  111. }
  112. };
  113. request.send('group=' + emoGroup.value);
  114. }
  115.  
  116. function addEmoGroupEvent() {
  117. var emos = emoGroupDetail.childNodes;
  118. for (i = 0, len = emos.length; i < len; i++)
  119. emos[i].addEventListener('click', function(e) {
  120. idQuestion.value += e.target.parentNode.getAttribute('alt');
  121. idQuestion.focus();
  122. });
  123. }
  124.  
  125. // Run at startup.
  126. toggleEmoSlt();
  127. addEmoGroupEvent();
  128. idQuestion.focus();
  129. })();