Open notification link to new tab by keyboard for GitHub

Add keyboard shortcuts to open focusing link of GitHub Notifications. 1) v to open link in new tab. 2) Shift+v to open links from same repository in new tabs.

As of 2018-01-14. See the latest version.

  1. // ==UserScript==
  2. // @name Open notification link to new tab by keyboard for GitHub
  3. // @namespace http://kyanny.me/
  4. // @version 1.0.0
  5. // @description Add keyboard shortcuts to open focusing link of GitHub Notifications. 1) v to open link in new tab. 2) Shift+v to open links from same repository in new tabs.
  6. // @author Kensuke Nagae
  7. // @match https://github.com/notifications/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var logger = {
  15. info: function(msg) {
  16. console.log(msg);
  17. },
  18. debug: function(msg) {
  19. // console.debug(msg);
  20. },
  21. };
  22.  
  23. var findTarget = function(item) {
  24. logger.debug('find target');
  25. return item.querySelector('.js-notification-target');
  26. };
  27.  
  28. var openLink = function(target) {
  29. var link = target.href;
  30. logger.info('opening ' + link);
  31. window.open(link, '_blank');
  32. };
  33.  
  34. var markAsRead = function(item) {
  35. logger.debug('mark as read');
  36. item.setAttribute('class', item.getAttribute('class') + ' read');
  37. };
  38.  
  39. document.addEventListener('keyup', function(ev) {
  40. var key = ev.key.toLowerCase();
  41. var focusingItem = document.querySelector('.navigation-focus');
  42. if (key === 'v' && !!focusingItem) {
  43. if (ev.shiftKey === true) { // Shift key is pressed
  44. var siblings = focusingItem.parentNode.querySelectorAll('.js-notification');
  45. [].forEach.call(siblings, function(sibling) {
  46. var target = findTarget(sibling);
  47. openLink(target);
  48. markAsRead(sibling);
  49. });
  50. } else {
  51. var target = findTarget(focusingItem);
  52. openLink(target);
  53. markAsRead(focusingItem);
  54. }
  55. }
  56. });
  57. })();