Reddit Flair Linkifier

Turns the text in various subreddits' flair into links

当前为 2018-07-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit Flair Linkifier
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 2.0.10
  5. // @description Turns the text in various subreddits' flair into links
  6. // @author Adrien Pyke
  7. // @match *://*.reddit.com/*
  8. // @require https://cdn.rawgit.com/fuzetsu/userscripts/477063e939b9658b64d2f91878da20a7f831d98b/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. const rightColSelector = `${newLayoutId} > div > div:nth-of-type(2) > div > div > div > div > div:last-of-type > div:last-of-type > div:last-of-type > div`;
  26.  
  27. waitForElems({
  28. sel: [
  29. // old reddit
  30. 'span.flair',
  31. 'span.Comment__authorFlair',
  32.  
  33. // card template
  34. `${newLayoutId} .Post > div:nth-of-type(2) > div:first-of-type > div > div:first-of-type > div:nth-of-type(2) > span`,
  35. `${newLayoutId} .Post > div:nth-of-type(2) > article > div:first-of-type > div:first-of-type > div > div:first-of-type > div:nth-of-type(2) > span`,
  36. // classic template
  37. `${newLayoutId} .Post > div:last-of-type > div > div:last-of-type > div:nth-of-type(2) > div:nth-of-type(2) > div:last-of-type > span`,
  38. // compact template
  39. `${newLayoutId} .Post > div > div:nth-of-type(2) > div > div:nth-of-type(2) > div:nth-of-type(3) > div:nth-of-type(2) > span`,
  40.  
  41. // comments
  42. `${newLayoutId} .Comment > div:nth-of-type(2) > div:first-of-type > div:nth-of-type(2) > span`,
  43.  
  44. // comments post title
  45. `${newLayoutId} [data-test-id="post-content"] > div:nth-of-type(2) > div > div:first-of-type > div:last-of-type > span`,
  46.  
  47. // comments post mods
  48. `${newLayoutId} > div > div:nth-of-type(2) > div:last-of-type > div:first-of-type > div > div:last-of-type > div > div > div > div:last-of-type > div > div > span`,
  49.  
  50. // user profile comments
  51. `${newLayoutId} .Comment > div > div:last-of-type > div > div:first-of-type > div:first-of-type > div:nth-of-type(2) > span`,
  52. `${newLayoutId} .Comment > div > div:last-of-type > div > div:first-of-type > div:nth-of-type(2) > span`,
  53.  
  54. // moderators
  55. `${rightColSelector} > div > div > div:last-of-type > div > div > span`,
  56.  
  57. // user flair preview
  58. `${rightColSelector} > div:first-of-type > div:last-of-type > div:last-of-type > div:last-of-type > div > span`,
  59.  
  60. // flair edit
  61. 'body > div:last-of-type > div > div > div:nth-of-type(2) > div > span'
  62. ].join(','),
  63. onmatch(flair) {
  64. flair.innerHTML = flair.textContent.split(' ').map(segment => {
  65. if (segment.match(/^https?:\/\//)) {
  66. return `<a href="${segment}" class="flair-link" target="_blank" rel="noopener noreferrer">${segment}</a>`;
  67. } else {
  68. return segment;
  69. }
  70. }).join(' ');
  71. }
  72. });
  73. })();