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.1
  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. a {
  70. pointer-events: none;
  71. }`;
  72. }
  73. }
  74.  
  75. function resize() {
  76. var TO=0, iframe;
  77.  
  78. if (!(iframe=parent.document.querySelector('iframe#poststats'))) return;
  79.  
  80. function resizeIframe() {
  81. if (TO) {clearTimeout(TO); TO=0;}
  82. iframe.style.height=document.body.scrollHeight + 20 + 'px';
  83. }
  84.  
  85. const obs = new ResizeObserver(function(e){
  86. if (TO) {clearTimeout(TO);}
  87. TO=setTimeout(resizeIframe,200);
  88. });
  89.  
  90. obs.observe(document.body);
  91. }
  92.  
  93.  
  94. function tryUntil(F, TO=150, c=-1, fail) {
  95. if (!c--) {
  96. fail && fail();
  97. return;
  98. }
  99. try{F();}catch(e){setTimeout(function(){tryUntil(F,TO,c,fail);}, TO)}
  100. }
  101.  
  102. if (window === window.top) {
  103. if (document.readyState != 'loading') start();
  104. else {
  105. document.addEventListener('DOMContentLoaded', start);
  106. window.addEventListener('load', start);
  107. }
  108. }
  109. else {
  110. tryUntil(addStyle ,0);
  111. tryUntil(resize, 0);
  112. }
  113.  
  114. })();