reddit context menu options

Adds links for reddit-stream, snoopsnoo, and "copy link with context" to reddit

  1. // ==UserScript==
  2. // @name reddit context menu options
  3. // @description Adds links for reddit-stream, snoopsnoo, and "copy link with context" to reddit
  4. // @namespace org.stevenhoward
  5. // @include https://www.reddit.com/*
  6. // @version 1
  7. // @grant GM_setClipboard
  8. // ==/UserScript==
  9. // jshint esversion: 6
  10.  
  11. function createMenu (parent, actions) {
  12. function randomId() {
  13. return Math.random().toString(36).replace(/[^a-z]+/g, '');
  14. }
  15.  
  16. let id = randomId();
  17.  
  18. let menu = document.createElement('menu');
  19. menu.id = id;
  20. menu.type = 'context';
  21. for (let action in actions) {
  22. let item = document.createElement('menuItem');
  23. item.label = action;
  24. item.onclick = actions[action];
  25.  
  26. menu.appendChild(item);
  27. }
  28.  
  29. parent.parentNode.appendChild(menu);
  30. parent.setAttribute('contextMenu', id);
  31. }
  32.  
  33. function attachRedditStreamCommand(thing) {
  34. let streamLink = thing.href.replace(/reddit/, 'reddit-stream');
  35. createMenu(thing, {
  36. 'open in reddit-stream': () => window.location = streamLink,
  37. 'open in reddit-stream (new tab)': () => window.open(streamLink)
  38. });
  39. }
  40.  
  41. function attachCopyContextCommand(thing) {
  42. let contextLink = thing.href + '?context=3';
  43. createMenu(thing, { 'Copy link with context': () => GM_setClipboard(contextLink) });
  44. }
  45.  
  46. function attachSnoopSnooCommand(thing) {
  47. let user = thing.href.match('[^/]+$');
  48. if (user) {
  49. user = user[0];
  50. let snoopUrl = `http://snoopsnoo.com/u/${user}`;
  51. createMenu(thing, { 'SnoopSnoo': () => window.open(snoopUrl) });
  52. }
  53. }
  54.  
  55. function findAndAttach(attachFn, selector, childSelector) {
  56. for (let node of document.querySelectorAll(selector)) {
  57. attachFn(node);
  58. }
  59. }
  60.  
  61. findAndAttach(attachRedditStreamCommand, '.link a.title');
  62. findAndAttach(attachCopyContextCommand, '.comment .bylink');
  63. findAndAttach(attachSnoopSnooCommand, 'a.author, a[href^="/u/"]');