BGG Shortcuts

Keyboard shortcuts for the Geek

当前为 2014-10-27 提交的版本,查看 最新版本

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