Reddit Flair Linkifier

Turns the text in various subreddits' flair into links

当前为 2018-05-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit Flair Linkifier
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 2.0.4
  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 = '#\\32x-container';
  25. const rightColSelector = `${newLayoutId} > div > div:last-of-type > div > div:first-of-type > div:last-of-type > 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.  
  32. // card template
  33. `${newLayoutId} .Post > div:nth-of-type(2) > div:first-of-type > div > div:first-of-type > div:nth-of-type(2) > span`,
  34. `${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`,
  35. // classic template
  36. `${newLayoutId} .Post > div:nth-of-type(2) > div > div:nth-of-type(2) > div:nth-of-type(2) > div:first-of-type > div:nth-of-type(2) > span`,
  37. // compact template
  38. `${newLayoutId} .Post > div > div:nth-of-type(2) > div > div:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(2) > span`,
  39.  
  40. // comments
  41. `${newLayoutId} .Comment > div:nth-of-type(2) > div:first-of-type > div:nth-of-type(2) > span`,
  42.  
  43. // user profile comments
  44. `${newLayoutId} .Comment > div > div:last-of-type > div > div:first-of-type > div:first-of-type > div:nth-of-type(2) > span`,
  45. `${newLayoutId} .Comment > div > div:last-of-type > div > div:first-of-type > div:nth-of-type(2) > span`,
  46.  
  47. // moderators
  48. `${rightColSelector} > div:nth-of-type(5) > div > div > div:first-of-type > div > div > span`,
  49.  
  50. // user flair preview
  51. `${rightColSelector} > div:first-of-type > div:last-of-type > div:last-of-type > div > span`,
  52.  
  53. // flair edit
  54. 'body > div:last-of-type > div > div > div:nth-of-type(2) > div > span'
  55. ].join(','),
  56. onmatch(flair) {
  57. flair.innerHTML = flair.textContent.split(' ').map(segment => {
  58. if (segment.match(/^https?:\/\//)) {
  59. return `<a href="${segment}" class="flair-link" target="_blank" rel="noopener noreferrer">${segment}</a>`;
  60. } else {
  61. return segment;
  62. }
  63. }).join(' ');
  64. }
  65. });
  66. })();