tftv keyboard shortcuts

Adds keyboard shortcuts for navigating forum pages on teamfortress.tv

当前为 2015-06-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name tftv keyboard shortcuts
  3. // @namespace Lense
  4. // @description Adds keyboard shortcuts for navigating forum pages on teamfortress.tv
  5. // @include http://teamfortress.tv/thread/*
  6. // @include http://www.teamfortress.tv/thread/*
  7. // @include https://teamfortress.tv/thread/*
  8. // @include https://www.teamfortress.tv/thread/*
  9. // @version 0.2
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. /*
  14. * Helper function to scroll to post by id
  15. */
  16. function goto_post(id) {
  17. document.getElementById(id).scrollIntoView();
  18. window.scrollBy(0,-40)
  19. }
  20.  
  21. /*
  22. * Find the number of pages in the thread
  23. */
  24. var lastPage = 1;
  25. var nodeList = document.querySelectorAll(".page-btn");
  26. for(var i=0; i<nodeList.length; i++) {
  27. if(parseInt(nodeList[i].textContent) > lastPage) {
  28. lastPage = parseInt(nodeList[i].textContent);
  29. }
  30. }
  31.  
  32. /*
  33. * Find the post number of the top post on the page
  34. */
  35. var posts = document.querySelectorAll(".post");
  36. var cur_post = 0;
  37.  
  38. /*
  39. * Get and process keypresses
  40. */
  41. document.onkeypress = function (e) {
  42. // Not entirely sure why this is useful, but it can't break anything that isn't already broken
  43. e = e || window.event;
  44. // Ignore keypresses in text boxes
  45. if(e.target.nodeName != "BODY") {
  46. // got keypress, but it's probably in a textbox, so ignore it
  47. return;
  48. }
  49. // Current page number
  50. var page = window.location.search == "" || parseInt(window.location.search.match("page=([0-9]*)")[1]);
  51. // and yes, I am proud of the logic in that statement.
  52.  
  53. switch(e["key"]) {
  54. /*
  55. * Pages (left/right)
  56. */
  57. case "d":
  58. if(page != lastPage) {
  59. page = (page+1).toString();
  60. window.location.search = "?page=" + page;
  61. }
  62. break;
  63. case "a":
  64. if(page != 1) {
  65. page = (page-1).toString();
  66. window.location.search = "?page=" + page;
  67. }
  68. break;
  69. case "D":
  70. if(page != lastPage) {
  71. window.location.search = "?page=" + lastPage.toString();
  72. }
  73. break;
  74. case "A":
  75. if(page != 1) {
  76. window.location.search = "?page=1";
  77. }
  78. break;
  79. /*
  80. * Posts (up/down)
  81. */
  82. case "w":
  83. if(cur_post > 0) {
  84. cur_post--;
  85. }
  86. goto_post(posts[cur_post].id);
  87. break;
  88. case "s":
  89. if(cur_post < posts.length - 1) {
  90. cur_post++;
  91. }
  92. goto_post(posts[cur_post].id);
  93. break;
  94. case "W":
  95. cur_post = 0;
  96. goto_post(posts[cur_post].id);
  97. break;
  98. case "S":
  99. cur_post = posts.length - 1;
  100. goto_post(posts[cur_post].id);
  101. break;
  102. /*
  103. * Frags (+/-)
  104. */
  105. case "q":
  106. $.ajax({
  107. type: 'post',
  108. data: {},
  109. url: '/post/frag/' + posts[cur_post].id.slice(8) + '/plus',
  110. });
  111. break;
  112. case "e":
  113. $.ajax({
  114. type: 'post',
  115. data: {},
  116. url: '/post/frag/' + posts[cur_post].id.slice(8) + '/minus',
  117. });
  118. break;
  119. }};