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.3
  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. r.outerHTML='<iframe src="'+t.pathname+'" id="'+t.pathname+'" style="border: none; height: 0;"></iframe>';
  27. }
  28. }, true);
  29. started++;
  30. }
  31.  
  32. function addStyle() {
  33. if (location.pathname.startsWith('/poststats/')) {
  34. let st=document.createElement("style");
  35. document.documentElement.appendChild(st);
  36. st.textContent=
  37. `html, * , :host * {
  38. background: transparent !important;
  39. background-color: transparent !important;
  40. }
  41.  
  42. body > :not(shreddit-app),
  43. shreddit-app > :not([rpl]),
  44. shreddit-app > [rpl]::before,
  45. shreddit-app > [rpl] > :not(#subgrid-container),
  46. .main-container > :not(main),
  47. main > div > div,
  48. main > div > div + section
  49. {
  50. display: none !important;
  51. }
  52.  
  53. shreddit-app > [rpl] {
  54. display: block !important;
  55. }
  56. obody,
  57. shreddit-app,
  58. shreddit-app > [rpl],
  59. #subgrid-container,
  60. #subgrid-container > .main-container,
  61. #subgrid-container > .main-container > main {
  62. display: contents !important;
  63. }
  64.  
  65. main > div {
  66. margin: 0 !important;
  67. padding: 0 !important;
  68. }
  69.  
  70. a {
  71. pointer-events: none;
  72. }
  73. `;
  74. }
  75. }
  76.  
  77. function resize() {
  78. var TO=0, iframe;
  79.  
  80. if (!(iframe=parent.document.querySelector('iframe[id="'+location.pathname+'"]'))) return;
  81.  
  82. function resizeIframe() {
  83. if (TO) {clearTimeout(TO); TO=0;}
  84. iframe.style.height=document.body.scrollHeight + 20 + 'px';
  85. }
  86.  
  87. const obs = new ResizeObserver(function(e){
  88. if (TO) {clearTimeout(TO);}
  89. TO=setTimeout(resizeIframe,200);
  90. });
  91.  
  92. obs.observe(document.body);
  93. }
  94.  
  95.  
  96. function tryUntil(F, TO=150, c=-1, fail) {
  97. if (!c--) {
  98. fail && fail();
  99. return;
  100. }
  101. try{F();}catch(e){setTimeout(function(){tryUntil(F,TO,c,fail);}, TO)}
  102. }
  103.  
  104. if (window === window.top) {
  105. tryUntil(start, 0);
  106. if (document.readyState != 'loading') start();
  107. else {
  108. document.addEventListener('DOMContentLoaded', start);
  109. window.addEventListener('load', start);
  110. }
  111. }
  112. else {
  113. tryUntil(addStyle ,0);
  114. tryUntil(resize, 0);
  115. }
  116.  
  117. })();