BGG Shortcuts

Keyboard shortcuts for the Geek

当前为 2014-09-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name BGG Shortcuts
  3. // @namespace BGG Shortcuts
  4. // @version 0.8.0
  5. // @description Keyboard shortcuts for the Geek
  6. // @include http://*.boardgamegeek.*/*
  7. // @include http://boardgamegeek.*/*
  8. // @copyright 2013+, JB McMichael
  9. // ==/UserScript==
  10.  
  11. /*
  12. * CHANGLOG::
  13. * ============================================
  14. * 0.8.0 - Forum links pop up in a modal so that you don't lose your place on a page
  15. * 0.7.0 - Changed the links to just be J for next item, H for home, and / for search, but disabled shortcuts in form elements
  16. * 0.6.1 - Give the page some time to load its scripts before changing links
  17. * 0.6.0 - Added a shortcut to go to the searchbox Ctrl + /
  18. * 0.5.0 - Added homepage links opening in new tab
  19. * 0.4.0 - If search returns one result just go to that result
  20. * 0.3.0 - Added homepage shortcut; Ctrl + Shift + H
  21. * 0.2.0 - Cleaned up the subscription jump link
  22. * 0.1.0 - First version, shortcut for subscriptions; Ctrl + M
  23. *
  24. */
  25.  
  26. (function () {
  27. "use strict";
  28. console.log('Loaded BGG Shortcuts');
  29. document.body.addEventListener('keydown', function(e) {
  30. var active = document.activeElement.tagName.toLowerCase(),
  31. badElements = ['input', 'textarea', 'select'];
  32.  
  33. // ignore shortcuts if we are in some form of input
  34. if (badElements.indexOf(active) === -1) {
  35. // Next subscription item J
  36. if (e.keyCode === 74) {
  37. [].slice.call(document.querySelectorAll("img:not(dn).nextsubcol"))[0].parentNode.parentNode.click();
  38. }
  39. // Home page H
  40. if (e.keyCode === 72 && window.location.href !== window.location.origin) {
  41. window.location.href = window.location.origin;
  42. }
  43. // Search box jump /
  44. if (e.keyCode === 191) {
  45. var searchbox = document.getElementById('sitesearch');
  46. document.body.scrollTop = 0;
  47. searchbox.focus();
  48. window.setTimeout(function() { searchbox.value = ''; }, 10);
  49. }
  50. }
  51. }, false);
  52. //check for one result on the search page
  53. if (window.location.pathname === '/geeksearch.php' && window.location.search.search(/action=search/)) {
  54. var results = document.querySelectorAll('#collectionitems tbody tr');
  55. console.log('We are searching');
  56. if (results.length === 2) {
  57. console.log('Found just one result, redirect');
  58. var link = results[1].querySelectorAll('#results_objectname1 a'),
  59. href = link[0].getAttribute('href');
  60. window.location.href = window.location.origin + href;
  61. }
  62. }
  63. // set all homepage module links to open in new tab
  64. if (window.location.pathname === '/') {
  65. window.setTimeout(function(){
  66. console.log('Changing homepage links');
  67. var links = document.querySelectorAll('.innermoduletable tbody td a.ng-binding'),
  68. linkArray = [].slice.call(links);
  69. linkArray.forEach(function(item, index){
  70. item.setAttribute('target', '_blank');
  71. });
  72. },500);
  73. }
  74.  
  75. // popup forum links in a dialog
  76. if (window.location.pathname.split('/')[1] === 'boardgame') {
  77. // grab all forum link clicks
  78. document.addEventListener('click', forumClick, false);
  79. }
  80. function forumClick(e) {
  81. if (e.target.tagName === 'A' && e.target.href.split('/')[3] === 'thread') {
  82. e.preventDefault();
  83. // use the BGG API to get the thread
  84. var req = new XMLHttpRequest(),
  85. apiUrl = window.location.protocol + '//' + window.location.host + '/xmlapi2/thread?id=',
  86. diag = document.createElement('dialog'),
  87. content = document.createElement('div'),
  88. close = document.createElement('button'),
  89. thread = e.target.href.split('/')[4];
  90. diag.style.width = '80%';
  91. diag.style.height = '80%';
  92. diag.style.border = '2px solid rgba(0, 0, 0, 0.3)';
  93. diag.style.borderRadius = '6px';
  94. diag.style.boxShadow = '0 3px 7px rgba(0, 0, 0, 0.3)';
  95. content.style.overflowY = 'auto';
  96. content.style.height = '95%';
  97. content.style.margin = '5px 0px';
  98. close.innerText = 'Close';
  99. req.onreadystatechange = showContents;
  100. req.open('GET', apiUrl + thread, true);
  101. req.send();
  102. function showContents(e) {
  103. if (req.readyState === 4 && req.status === 200) {
  104. var subject = req.responseXML.documentElement.children[0].firstChild.nodeValue,
  105. articles = req.responseXML.documentElement.children[1].children;
  106. for (var i = 0; i < articles.length; i++) {
  107. var article = articles[i];
  108. var user = article.getAttribute('username');
  109. var title = article.children[0].firstChild.nodeValue;
  110. var body = article.children[1].firstChild.nodeValue;
  111. var postdate = article.getAttribute('postdate');
  112. var articleDiv = document.createElement('div');
  113. var dl = document.createElement('dl');
  114. var ddLeft = document.createElement('dd');
  115. var ddRight = document.createElement('dd');
  116. var userDiv = document.createElement('div');
  117. var bottomDl =document.createElement('dl');
  118. var ddLeft2 = document.createElement('dd');
  119. var ddCommands = document.createElement('dd');
  120. var ul = document.createElement('ul');
  121. var li = document.createElement('li');
  122. var ulInfo = document.createElement('ul');
  123. var liInfo = document.createElement('li');
  124. var postLink = document.createElement('a');
  125. var clearDiv = document.createElement('div');
  126. var rollsDiv = document.createElement('div');
  127. var userInfo = getUser(user);
  128. articleDiv.addClass('article');
  129. rollsDiv.addClass('rollsblock');
  130. ddLeft.addClass('left');
  131. ddRight.addClass('right');
  132. userDiv.addClass('username');
  133. userDiv.innerHTML = user;
  134. ddLeft.appendChild(userDiv);
  135. ddRight.innerHTML = body;
  136. dl.appendChild(ddLeft);
  137. dl.appendChild(ddRight);
  138. articleDiv.appendChild(dl);
  139. ddLeft2.addClass('left');
  140. ddCommands.addClass('commands');
  141. ul.appendChild(li);
  142. ddCommands.appendChild(ul);
  143. clearDiv.addClass('clear');
  144. ulInfo.addClass('information');
  145. postLink.innerHTML = postdate;
  146. liInfo.appendChild(postLink);
  147. ulInfo.appendChild(liInfo);
  148. ddCommands.appendChild(ulInfo);
  149. bottomDl.appendChild(ddLeft2);
  150. bottomDl.appendChild(ddCommands);
  151. articleDiv.appendChild(bottomDl);
  152. articleDiv.appendChild(clearDiv);
  153. articleDiv.appendChild(rollsDiv);
  154. content.appendChild(articleDiv);
  155. }
  156. diag.insertBefore(close, diag.childNodes[0]);
  157. diag.insertBefore(content, diag.childNodes[0]);
  158. close.addEventListener('click', function(e) { diag.close(); });
  159. document.body.insertBefore(diag,document.body.childNodes[0]);
  160. diag.showModal();
  161. }
  162. }
  163. }
  164. }
  165. function getUser(name) {
  166. var req = new XMLHttpRequest(),
  167. apiUrl = window.location.protocol + '//' + window.location.host + '/xmlapi2/user?name=' + name;
  168. req.onreadystatechange = function(e) {
  169. if (req.readyState === 4 && req.status === 200) {
  170. console.log(req.responseXML);
  171. }
  172. };
  173. req.open('GET', apiUrl, true);
  174. req.send();
  175. }
  176. }());