Reddit - Inline post insight

Display post "Insight" inline instead of in another page

当前为 2025-01-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit - Inline post insight
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.5.4
  5. // @description Display post "Insight" inline instead of in another page
  6. // @author Achernar
  7. // @match https://www.reddit.com/*
  8. // @match https://sh.reddit.com/*
  9. // @grant none
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. "use strict";
  15.  
  16. var started=0;
  17. function start() {
  18. if (started) return;
  19. document.body.addEventListener('click', function(ev){
  20. //console.info({ev});
  21. var t=ev.target, r, f;
  22. if (t.href && (location.hostname == t.hostname) && t.pathname.startsWith('/poststats/') ) {
  23. ev.stopPropagation();
  24. ev.preventDefault();
  25. r=t.closest('div');
  26. if (r.nextElementSibling && r.nextElementSibling.id.startsWith('/poststats/')) {
  27. r.nextElementSibling.remove();
  28. return;
  29. }
  30. let f=document.createElement('iframe');
  31. f.src=t.pathname;
  32. f.id=t.pathname;
  33. f.style='border: none; height: 0;';
  34. r.parentNode.appendChild(f);
  35. }
  36. }, true);
  37. addStyle(2);
  38. started++;
  39. }
  40.  
  41. function addStyle(v=1) {
  42. let st=document.createElement("style");
  43. document.documentElement.appendChild(st);
  44. if (v==2) st.textContent=
  45. `div:has(+ iframe):not(:has( div a[href^="/poststats/"] )) a[href^="/poststats/"] {
  46. font-size: 0;
  47. }
  48. div:has(+ iframe):not(:has( div a[href^="/poststats/"] )) a[href^="/poststats/"]::before {
  49. content: "[-]";
  50. font-size: 1rem !important;
  51. }`;
  52. else if (v==1) st.textContent=
  53. `html, * , :host * {
  54. background: transparent !important;
  55. background-color: transparent !important;
  56. }
  57.  
  58. body > :not(shreddit-app),
  59. shreddit-app > :not([rpl]),
  60. shreddit-app > [rpl]::before,
  61. shreddit-app > [rpl] > :not(#subgrid-container),
  62. .main-container > :not(main),
  63. main > div > div,
  64. main > div > div + section
  65. {
  66. display: none !important;
  67. }
  68.  
  69. shreddit-app > [rpl] {
  70. display: block !important;
  71. }
  72. obody,
  73. shreddit-app,
  74. shreddit-app > [rpl],
  75. #subgrid-container,
  76. #subgrid-container > .main-container,
  77. #subgrid-container > .main-container > main {
  78. display: contents !important;
  79. }
  80.  
  81. main > div {
  82. margin: 0 !important;
  83. padding: 0 !important;
  84. }
  85.  
  86. a {
  87. pointer-events: none;
  88. }`;
  89. }
  90.  
  91. function resize() {
  92. var TO=0, iframe;
  93.  
  94. if (!(iframe=parent.document.querySelector('iframe[id="'+location.pathname+'"]'))) return;
  95.  
  96. function resizeIframe() {
  97. if (TO) {clearTimeout(TO); TO=0;}
  98. iframe.style.height=document.body.scrollHeight + 20 + 'px';
  99. }
  100.  
  101. const obs = new ResizeObserver(function(e){
  102. if (TO) {clearTimeout(TO);}
  103. TO=setTimeout(resizeIframe,200);
  104. });
  105.  
  106. obs.observe(document.body);
  107. }
  108.  
  109.  
  110. function tryUntil(F, TO=150, c=-1, fail) {
  111. if (!c--) {
  112. fail && fail();
  113. return;
  114. }
  115. try{F();}catch(e){setTimeout(function(){tryUntil(F,TO,c,fail);}, TO)}
  116. }
  117.  
  118. if (window === window.top) {
  119. tryUntil(start, 0);
  120. if (document.readyState != 'loading') start();
  121. else {
  122. document.addEventListener('DOMContentLoaded', start);
  123. window.addEventListener('load', start);
  124. }
  125. }
  126. else if (location.pathname.startsWith('/poststats/')) {
  127. tryUntil(addStyle ,0);
  128. tryUntil(resize, 0);
  129. }
  130.  
  131. })();