BGG Shortcuts

Keyboard shortcuts for the Geek

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

  1. // ==UserScript==
  2. // @name BGG Shortcuts
  3. // @namespace BGG Shortcuts
  4. // @version 0.7.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.7.0 - Changed the links to just be J for next item, H for home, and / for search, but disabled shortcuts in form elements
  15. * 0.6.1 - Give the page some time to load its scripts before changing links
  16. * 0.6 - Added a shortcut to go to the searchbox Ctrl + /
  17. * 0.5 - Added homepage links opening in new tab
  18. * 0.4 - If search returns one result just go to that result
  19. * 0.3 - Added homepage shortcut; Ctrl + Shift + H
  20. * 0.2 - Cleaned up the subscription jump link
  21. * 0.1 - First version, shortcut for subscriptions; Ctrl + M
  22. *
  23. */
  24.  
  25. (function () {
  26. "use strict";
  27. console.log('Loaded BGG Shortcuts');
  28. document.body.addEventListener('keydown', function(e) {
  29. var active = document.activeElement.tagName.toLowerCase(),
  30. badElements = ['input', 'textarea', 'select'];
  31.  
  32. // ignore shortcuts if we are in some form of input
  33. if (badElements.indexOf(active) === -1) {
  34. // Next subscription item J
  35. if (e.keyCode === 74) {
  36. [].slice.call(document.querySelectorAll("img:not(dn).nextsubcol"))[0].parentNode.parentNode.click();
  37. }
  38. // Home page H
  39. if (e.keyCode === 72 && window.location.href !== window.location.origin) {
  40. window.location.href = window.location.origin;
  41. }
  42. // Search box jump /
  43. if (e.keyCode === 191) {
  44. var searchbox = document.getElementById('sitesearch');
  45. document.body.scrollTop = 0;
  46. searchbox.focus();
  47. window.setTimeout(function() { searchbox.value = ''; }, 10);
  48. }
  49. }
  50. }, false);
  51. //check for one result on the search page
  52. if (window.location.pathname === '/geeksearch.php' && window.location.search.search(/action=search/)) {
  53. var results = document.querySelectorAll('#collectionitems tbody tr');
  54. console.log('We are searching');
  55. if (results.length === 2) {
  56. console.log('Found just one result, redirect');
  57. var link = results[1].querySelectorAll('#results_objectname1 a'),
  58. href = link[0].getAttribute('href');
  59. window.location.href = window.location.origin + href;
  60. }
  61. }
  62. // set all homepage module links to open in new tab
  63. if (window.location.pathname === '/') {
  64. window.setTimeout(function(){
  65. console.log('Changing homepage links');
  66. var links = document.querySelectorAll('.innermoduletable tbody td a.ng-binding'),
  67. linkArray = [].slice.call(links);
  68. linkArray.forEach(function(item, index){
  69. item.setAttribute('target', '_blank');
  70. });
  71. },500);
  72. }
  73. }());