Reddit - Inline post insight

Display post "Insight" inline instead of in another page

  1. // ==UserScript==
  2. // @name Reddit - Inline post insight
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.5.7
  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, :not(:is(pre,code)) {
  54. background: transparent !important;
  55. background-color: transparent !important;
  56. }
  57.  
  58. html body > :not(shreddit-app),
  59. html body shreddit-app > :not([rpl]),
  60. html body shreddit-app > [rpl]::before,
  61. html body shreddit-app > [rpl][rpl][rpl] > :not(#subgrid-container),
  62. html body .main-container > :not(main),
  63. html body main > div > div,
  64. html body 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. body {
  91. overflow: auto;
  92. }`;
  93. }
  94.  
  95. function resize() {
  96. var TO=0, iframe;
  97.  
  98. if (!(iframe=parent.document.querySelector('iframe[id="'+location.pathname+'"]'))) return;
  99.  
  100. function resizeIframe() {
  101. if (TO) {clearTimeout(TO); TO=0;}
  102. iframe.style.height=document.body.scrollHeight + 20 + 'px';
  103. }
  104.  
  105. const obs = new ResizeObserver(function(e){
  106. if (TO) {clearTimeout(TO);}
  107. TO=setTimeout(resizeIframe,200);
  108. });
  109.  
  110. obs.observe(document.body);
  111. }
  112.  
  113.  
  114. function tryUntil(F, TO=150, c=-1, fail) {
  115. if (!c--) {
  116. fail && fail();
  117. return;
  118. }
  119. try{F();}catch(e){setTimeout(function(){tryUntil(F,TO,c,fail);}, TO)}
  120. }
  121.  
  122. if (window === window.top) {
  123. tryUntil(start, 0);
  124. if (document.readyState != 'loading') start();
  125. else {
  126. document.addEventListener('DOMContentLoaded', start);
  127. window.addEventListener('load', start);
  128. }
  129. }
  130. else if (location.pathname.startsWith('/poststats/')) {
  131. tryUntil(addStyle ,0);
  132. tryUntil(resize, 0);
  133. }
  134.  
  135. })();