Reddit Flair Linkifier

Turns the text in various subreddits' flair into links

  1. // ==UserScript==
  2. // @name Reddit Flair Linkifier
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 2.1.7
  5. // @description Turns the text in various subreddits' flair into links
  6. // @author Adrien Pyke
  7. // @match *://*.reddit.com/*
  8. // @require https://cdn.jsdelivr.net/gh/fuzetsu/userscripts@ec863aa92cea78a20431f92e80ac0e93262136df/wait-for-elements/wait-for-elements.js
  9. // @grant GM_addStyle
  10. // ==/UserScript==
  11.  
  12. (() => {
  13. 'use strict';
  14.  
  15. GM_addStyle(`
  16. .flair-link {
  17. text-decoration: none;
  18. }
  19. .flair-link:hover {
  20. text-decoration: underline;
  21. }
  22. `);
  23.  
  24. const newLayoutId = '#SHORTCUT_FOCUSABLE_DIV';
  25.  
  26. waitForElems({
  27. sel: [
  28. // old reddit
  29. 'span.flair',
  30. 'span.Comment__authorFlair',
  31.  
  32. // new reddit
  33. `${newLayoutId} span`
  34. ].join(','),
  35. onmatch(flair) {
  36. if (
  37. flair.childNodes.length !== 1 ||
  38. flair.childNodes[0].nodeType !== Node.TEXT_NODE ||
  39. flair.closest('.DraftEditor-root')
  40. )
  41. return;
  42. const newhtml = flair.textContent
  43. .split(' ')
  44. .map(segment =>
  45. segment.match(/^https?:\/\//u)
  46. ? `<a href="${segment}" class="flair-link" target="_blank" rel="noopener noreferrer">${segment}</a>`
  47. : segment
  48. )
  49. .join(' ');
  50. if (flair.innerHTML !== newhtml) flair.innerHTML = newhtml;
  51. }
  52. });
  53. })();