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.4
  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. case 40:
  87. emoGroup.selectedIndex !== emoGroup.length - 1 && emoGroup.selectedIndex++;
  88. changeEmoGroup();
  89. break;
  90. case 38:
  91. emoGroup.selectedIndex !== 0 && emoGroup.selectedIndex--,
  92. changeEmoGroup();
  93. break;
  94. default:
  95. idQuestion.focus();
  96. }
  97. }
  98.  
  99. function changeEmoGroup() {
  100. var request = new XMLHttpRequest();
  101. request.open('POST', 'qa_smiley_ajax.php', !0);
  102. request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  103. request.onreadystatechange = function() {
  104. request.readyState == 4 && request.status == 200 && (emoGroupDetail.innerHTML = JSON.parse(request.responseText).str,
  105. addEmoGroupEvent())
  106. };
  107. request.send('group=' + emoGroup.value);
  108. }
  109.  
  110. function addEmoGroupEvent() {
  111. var emos = emoGroupDetail.childNodes;
  112. for (i = 0, len = emos.length; i < len; i++)
  113. emos[i].addEventListener('click', function(e) {
  114. idQuestion.value += e.target.parentNode.getAttribute('alt');
  115. idQuestion.focus();
  116. });
  117. }
  118.  
  119. // Run at startup.
  120. toggleEmoSlt();
  121. addEmoGroupEvent();
  122. idQuestion.focus();
  123. })();