TorViet Shoutbox Enhancer

A small script to simplify the shoutbox

目前为 2015-05-31 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name TorViet Shoutbox Enhancer
  3. // @namespace http://torviet.com/userdetails.php?id=1662
  4. // @version 0.5.10
  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. windowHeight,
  29. remainingHeight,
  30. btnToggle;
  31.  
  32. // Remove unneeded elements.
  33. boxHead.parentNode.removeChild(boxHead);
  34. marquee.parentNode.removeChild(marquee);
  35. sltTheme.parentNode.removeChild(sltTheme);
  36. clock.parentNode.removeChild(clock);
  37.  
  38. // Alter existing element CSS.
  39. windowHeight = window.innerHeight;
  40. remainingHeight = inputSection.parentNode.offsetHeight + navigationPage.offsetHeight - 100;
  41.  
  42. allWrapper.setAttribute('style', 'background: none; margin: auto; height: ' + windowHeight + 'px');
  43. inputSection.parentNode.style.padding = '0px';
  44. navigationPage.style.width = 'auto';
  45. boxQuestion.style.height = windowHeight - remainingHeight + 2 + 'px';
  46. emoGroupDetail.parentNode.parentNode.style.height =
  47. emoGroupDetail.parentNode.style.height =
  48. emoGroupDetail.style.height = windowHeight - remainingHeight + 'px';
  49.  
  50. // Alter existing elements.
  51. emoGroupDetail.innerHTML = getEmoticons(524, 574) + getEmoticons(707) + getEmoticons(200, 234);
  52.  
  53. // Add elements.
  54. btnToggle = document.createElement('input');
  55. btnToggle.type = 'button';
  56. btnToggle.value = 'Toggle';
  57. btnToggle.onclick = toggleEmoSlt;
  58. idQuestion.parentNode.insertBefore(btnToggle, null);
  59.  
  60. // Add event listeners.
  61. // Firefox detection.
  62. typeof InstallTrigger !== 'undefined' ? document.addEventListener('keypress', keyEvent) : document.addEventListener('keydown', keyEvent);
  63.  
  64. // Custom functions.
  65. function toggleEmoSlt() {
  66. emoGroup.parentNode.style.display = emoGroup.offsetParent ? 'none' : 'block';
  67. }
  68.  
  69. function getEmoticons(start, end) {
  70. var emos = '';
  71. if (end === void 0)
  72. emos = '<div style="height:43px;width:43px;float:left;display:inline-block;margin:1px;"><a style="margin: 0;" class="btuEmotion" alt="[em' + start +
  73. ']"><img style="max-width: 43px; max-height: 43px" src="/pic/smilies/' + start +
  74. '.gif" alt=""></a></div>';
  75. else
  76. for (var i = start; i <= end; i++)
  77. emos += '<div style="height:43px;width:43px;float:left;display:inline-block;margin:1px;"><a style="margin: 0;" class="btuEmotion" alt="[em' + i +
  78. ']"><img style="max-width: 43px; max-height: 43px" src="/pic/smilies/' + i +
  79. '.gif" alt=""></a></div>';
  80.  
  81. return emos;
  82. }
  83.  
  84. function keyEvent(e) {
  85. switch (e.keyCode) {
  86. // Down arrow.
  87. case 40:
  88. (emoGroup !== document.activeElement) && (emoGroup.selectedIndex !== emoGroup.length - 1) && emoGroup.selectedIndex++;
  89. changeEmoGroup();
  90. break;
  91. // Up arrow.
  92. case 38:
  93. (emoGroup !== document.activeElement) && (emoGroup.selectedIndex !== 0) && emoGroup.selectedIndex--;
  94. changeEmoGroup();
  95. break;
  96. // Ctrl.
  97. case 17:
  98. // Ctrl + C.
  99. case 17 && 67:
  100. break;
  101. default:
  102. idQuestion.focus();
  103. }
  104. }
  105.  
  106. function changeEmoGroup() {
  107. var request = new XMLHttpRequest();
  108. request.open('POST', 'qa_smiley_ajax.php', 0);
  109. request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  110. request.onreadystatechange = function() {
  111. request.readyState == 4 && request.status == 200 &&
  112. (emoGroupDetail.innerHTML = JSON.parse(request.responseText).str, addEmoGroupEvent())
  113. };
  114. request.send('group=' + emoGroup.value);
  115. }
  116.  
  117. function addEmoGroupEvent() {
  118. for (var i = 0, emos = emoGroupDetail.childNodes, 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. })();