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