tftv keyboard shortcuts

Adds keyboard shortcuts for navigating forum pages on teamfortress.tv

目前為 2015-01-15 提交的版本,檢視 最新版本

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