TEXT   228   0
   369 3.83 KB    129

Post Previewer UserScript

By Fillyanon
Created: 2022-01-30 14:21:34
Updated: 2022-01-31 15:02:31
Expiry: Never

  1. // ==UserScript==
  2. // @name 4chan Post Preview
  3. // @grant none
  4. // @version 1.21
  5. // @author Fillyanon
  6. // @description 1/30/2022, 1:37:00 PM
  7. // ==/UserScript==
  8.  
  9. const css = `
  10. .colorizer-preview {
  11. border-top: 1px solid #888;
  12. margin: 0;
  13. margin-top: 0.5rem;
  14. max-height: 300px;
  15. min-height: 80px;
  16. min-width: 300px;
  17. overflow-y: auto;
  18. word-wrap: break-word;
  19. }
  20.  
  21. .colorizer-quote {
  22. color: #789922;
  23. }
  24.  
  25. .colorizer-reference {
  26. color: #d00;
  27. }
  28.  
  29. .spoiler {
  30. background: #000;
  31. color: #fff;
  32. display: inline !important;
  33. }
  34. `;
  35.  
  36. const processPreview = (input) => {
  37. const patterns = [
  38. [/\[spoiler\]([\s\S]*?)\[\/spoiler\]?/mg, "<s>$1</s>"],
  39. [/>>(\d{8})/g, "<a class='quotelink' href='#p$1'>>>$1</a>"],
  40. [/(\n>.*)/g, "<span class='quote'>$1</span>"],
  41. [/\[spoiler\]([\s\S]*)/mg, "<s>$1</s>"]
  42. ];
  43.  
  44. const replaced = patterns.reduce((accumulator, pattern) => {
  45. const [rgx, tag] = pattern;
  46. return accumulator.replaceAll(rgx, tag)
  47. }, input)
  48.  
  49. // First replace removes the \n I added as a hack for quote-detection.
  50. // The rest of the \n-s get converted into <br>-s
  51. return replaced.replace(/\n/, "").replaceAll(/\n/g, "<br>")
  52. }
  53.  
  54. /* YANKED FROM FLAG RANDOMIZER SCRIPT */
  55.  
  56.  
  57. const creationObserver = (function () {
  58. const observedNodes = [];
  59. const callbacks = [];
  60. const executeCallback = (fn, node) => {
  61. if (observedNodes.includes(node))
  62. return;
  63. observedNodes.push(node);
  64. fn(node);
  65. };
  66. const obs = new MutationObserver(mutationRecords => {
  67. mutationRecords.forEach(mutation => {
  68. mutation.addedNodes.forEach(node => {
  69. if (!(node instanceof HTMLElement))
  70. return;
  71. callbacks.forEach(([selector, fn]) => {
  72. if (node.matches(selector))
  73. executeCallback(fn, node);
  74. node.querySelectorAll(selector).forEach(childNode => executeCallback(fn, childNode));
  75. });
  76. });
  77. });
  78. });
  79. obs.observe(document.body, { childList: true, subtree: true });
  80. return function (selector, fn) {
  81. document.querySelectorAll(selector).forEach(node => executeCallback(fn, node));
  82. callbacks.push([selector, fn]);
  83. };
  84. })();
  85.  
  86. const selectors = [
  87. 'form[name="post"]', // 4chan default post
  88. 'form[name="qrPost"]', // 4chanNX quick reply form
  89. 'div#qr form', // 4chanX quick reply form
  90. ].join(', ');
  91.  
  92.  
  93. /* YANKING ENDS HERE */
  94.  
  95. // Inject the style.
  96. const style = document.createElement("style")
  97. style.innerHTML = css
  98. document.head.appendChild(style)
  99.  
  100. creationObserver(selectors, form => {
  101. if (!(form instanceof HTMLFormElement))
  102. return;
  103.  
  104. const textArea = form.querySelector("textarea")
  105. const preview = document.createElement("blockquote")
  106. preview.className = "colorizer-preview"
  107. preview.width = textArea.width;
  108.  
  109. // Thanks >>38150841 !
  110. preview.addEventListener('mouseover', Main.onThreadMouseOver);
  111. preview.addEventListener('mouseout', Main.onThreadMouseOut);
  112.  
  113. form.appendChild(preview)
  114.  
  115. new ResizeObserver(() => {
  116. preview.style.width = textArea.offsetWidth + 'px';
  117. preview.style.maxWidth = (textArea.style.width == "" ? "296px" : textArea.style.width);
  118. }).observe(textArea)
  119.  
  120. "input,paste".split(",").forEach(ev => textArea.addEventListener(ev, () => {
  121. // "\n" + is a hack that simplifies the way I detect quotes.
  122. // Probably could be done in a better way, but it doesn't much matter.
  123. preview.innerHTML = processPreview("\n" + textArea.value);
  124. preview.width = textArea.width;
  125. }))
  126. });

Fillyanon's Bookshelf

by Fillyanon

Thus passes threadly glory

by Fillyanon

Flag Randomizer Userscript

by Fillyanon

Post Previewer UserScript

by Fillyanon

MLPFicReviews Extras

by Fillyanon