HKEPC+

Enhance HKEPC trading forum

  1. // ==UserScript==
  2. // @name HKEPC+
  3. // @namespace http://www.hkepc.com/
  4. // @version 0.3
  5. // @description Enhance HKEPC trading forum
  6. // @author lacek
  7. // @match *://www.hkepc.com/forum/*
  8. // @grant none
  9. // ==/UserScript==
  10. /* jshint -W097 */
  11. 'use strict';
  12.  
  13. (function() {
  14. var HKEPC = {
  15.  
  16. init: function() {
  17. var page = this.determinePage();
  18. this.processPage(page);
  19. },
  20.  
  21. determinePage: function() {
  22. var page = null;
  23. var url = window.location.href.toLowerCase();
  24. var file = url.substring(url.lastIndexOf('/') + 1);
  25.  
  26. if (file === "index.php" || file === "") {
  27. page = 'index';
  28. }
  29. else if (file.indexOf("index.php?gid=") === 0) {
  30. page = 'group';
  31. }
  32. else if (file.indexOf("forumdisplay.php") === 0) {
  33. page = 'forum';
  34. }
  35. else if (file.indexOf("viewthread.php") === 0) {
  36. page = 'thread';
  37. }
  38. return page;
  39. },
  40.  
  41. processPage: function(page) {
  42. if (page !== null) {
  43. if (typeof (this[page + "PageProcessor"]) == 'function') {
  44. this[page + "PageProcessor"]();
  45. }
  46. }
  47. },
  48.  
  49. indexPageProcessor: function() {
  50. //add '&orderby=dateline' to links to trading forums
  51. var searchStart = 150; //((19*3-9)°Ï)*4+8
  52. var searchEnd = 250;
  53. var linksPerBlock = 2;
  54. var tradeLinks = ['?fid=14', '?fid=81', '?fid=119'];
  55. var aTag = document.getElementById('mainIndex').getElementsByTagName('a');
  56. var linksChanged = 0;
  57.  
  58. for (var i = 0; i < tradeLinks.length; i++) {
  59. for (var j = searchStart; j < searchEnd; j++) {
  60. if (aTag[j].href.indexOf(tradeLinks[i]) != -1) {
  61. aTag[j].href += '&orderby=dateline';
  62. linksChanged++;
  63. if (linksChanged >= linksPerBlock) {
  64. linksChanged = 0;
  65. break; //seach for next link
  66. }
  67. }
  68. }
  69. }
  70. },
  71.  
  72. groupPageProcessor: function() {
  73.  
  74. },
  75.  
  76. forumPageProcessor: function() {
  77. this.patchForumPageHotkey();
  78. this.patchThreadLinks();
  79. },
  80.  
  81. threadPageProcessor: function() {
  82. if(this.getQueryValue(window.location.href, "extra") !== "") this.patchThreadPageHotkey();
  83. },
  84.  
  85. //patch arrow hotkey links that contains '&amp;'
  86. patchForumPageHotkey: function() {
  87. var oldScript = document.querySelector('#wrap > script[type="text/javascript"]');
  88. var newScript = document.createElement('script');
  89. newScript.innerHTML = oldScript.innerHTML.replace(/\amp;/g, "");
  90. oldScript.parentNode.appendChild(newScript);
  91. oldScript.parentNode.removeChild(oldScript);
  92. },
  93.  
  94. //patch arrow hotkey link that misses parameters
  95. patchThreadPageHotkey: function() {
  96. var oldScript = document.getElementById('wrap').getElementsByTagName('script');
  97. var oldUrl = null;
  98. for (var i = 0; i < oldScript.length; i++) {
  99. if ((oldUrl = oldScript[i].innerHTML.match(/'viewthread.php\?tid=[0-9]+'/)) !== null) {
  100. var newScript = document.createElement('script');
  101. oldUrl = oldUrl[0].substring(1, oldUrl[0].length - 1);
  102. var newUrl = oldUrl + "&extra=" + this.getQueryValue(window.location.href, 'extra');
  103. newScript.innerHTML = oldScript[i].innerHTML.replace(oldUrl, newUrl);
  104. oldScript[i].parentNode.appendChild(newScript);
  105. oldScript[i].parentNode.removeChild(oldScript[i]);
  106. break;
  107. }
  108. }
  109. },
  110.  
  111. //patch forum incorrect links containing '&amp;'
  112. patchThreadLinks: function() {
  113. var spanTag = document.getElementById("threadlist").getElementsByTagName("span");
  114. var spanTagLen = spanTag.length;
  115. var aTag;
  116. for (var i = 0; i < spanTagLen; i++) {
  117. if (spanTag[i].id.indexOf('thread_') === 0) {
  118. aTag = spanTag[i].getElementsByTagName("a")[0];
  119. aTag.href = aTag.href.replace(/\amp%3B/g, "");
  120. }
  121. }
  122. },
  123.  
  124. //Get the value of parameter from a URL
  125. getQueryValue: function(url, name) {
  126. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  127. var regexS = "[\\?&]"+name+"=([^&#]*)";
  128. var regex = new RegExp(regexS);
  129. var results = regex.exec(url);
  130. return (results === null) ? "" : results[1];
  131. }
  132. };
  133.  
  134. HKEPC.init();
  135. })();