TorViet Shoutbox Enhancer

A small script to simplify the shoutbox

目前为 2015-05-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TorViet Shoutbox Enhancer
  3. // @namespace http://torviet.com/userdetails.php?id=1662
  4. // @version 0.5.6
  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.background = '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 =
  46. emoGroupDetail.parentNode.style.height =
  47. emoGroupDetail.style.height = windowHeight - remainingHeight + '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. typeof InstallTrigger !== 'undefined' ? document.addEventListener('keypress', keyEvent) : document.addEventListener('keydown', keyEvent);
  62.  
  63. // Custom functions.
  64. function toggleEmoSlt() {
  65. emoGroup.offsetParent ? emoGroup.parentNode.style.display = 'none' : emoGroup.parentNode.style.display = 'block';
  66. }
  67.  
  68. function getEmoticons(start, end) {
  69. var emos = '';
  70.  
  71. if (end === void 0)
  72. 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 +
  73. ']"><img style="max-width: 43px; max-height: 43px" src="/pic/smilies/' + start +
  74. '.gif" alt=""></a></div>';
  75. else
  76. for (i = start; i <= end; i++)
  77. 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 +
  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.selectedIndex !== emoGroup.length - 1 && emoGroup.selectedIndex++;
  89. changeEmoGroup();
  90. break;
  91. // Up arrow.
  92. case 38:
  93. 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 && (emoGroupDetail.innerHTML = JSON.parse(request.responseText).str,
  112. addEmoGroupEvent())
  113. };
  114. request.send('group=' + emoGroup.value);
  115. }
  116.  
  117. function addEmoGroupEvent() {
  118. var emos = emoGroupDetail.childNodes;
  119. for (i = 0, len = emos.length; i < len; i++)
  120. emos[i].addEventListener('click', function(e) {
  121. idQuestion.value += e.target.parentNode.getAttribute('alt');
  122. idQuestion.focus();
  123. });
  124. }
  125.  
  126. // Run at startup.
  127. toggleEmoSlt();
  128. addEmoGroupEvent();
  129. idQuestion.focus();
  130. })();