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.

Από την 25/01/2018. Δείτε την τελευταία έκδοση.

  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. // @match https://github.com/notifications/participating
  9. // @match https://github.com/*/*/notifications*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. /*
  14. For Firefox, browser.tabs.loadDivertedInBackground to true is useful.
  15. See https://stackoverflow.com/a/1306963/374851
  16. */
  17.  
  18. (function() {
  19. 'use strict';
  20.  
  21. var logger = {
  22. info: function(msg) {
  23. console.log(msg);
  24. },
  25. debug: function(msg) {
  26. // console.debug(msg);
  27. },
  28. };
  29.  
  30. var findTarget = function(item) {
  31. logger.debug('find target');
  32. return item.querySelector('.js-notification-target');
  33. };
  34.  
  35. var openLink = function(target) {
  36. var link = target.href;
  37. logger.info('opening ' + link);
  38. window.open(link, '_blank');
  39. };
  40.  
  41. var markAsRead = function(item) {
  42. logger.debug('mark as read');
  43. item.setAttribute('class', item.getAttribute('class') + ' read');
  44. };
  45.  
  46. document.addEventListener('keyup', function(ev) {
  47. var key = ev.key.toLowerCase();
  48. var focusingItem = document.querySelector('.navigation-focus');
  49. if (key === 'v' && !!focusingItem) {
  50. if (ev.shiftKey === true) { // Shift key is pressed
  51. var siblings = focusingItem.parentNode.querySelectorAll('.js-notification');
  52. [].forEach.call(siblings, function(sibling) {
  53. var target = findTarget(sibling);
  54. openLink(target);
  55. markAsRead(sibling);
  56. });
  57. } else {
  58. var target = findTarget(focusingItem);
  59. openLink(target);
  60. markAsRead(focusingItem);
  61. }
  62. }
  63. });
  64. })();