Poe Latex Renderer

Render Latex in poe.com

  1. // ==UserScript==
  2. // @name Poe Latex Renderer
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @author You
  6. // @description Render Latex in poe.com
  7. // @match https://poe.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=poe.com
  9. // @grant none
  10. // @license GNU GPLv3
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. const renderLatex = function () {
  17. MathJax.typeset();
  18. }
  19.  
  20. function endOfChat() {
  21. const isEndOfChat = function () {
  22. if (document.querySelector("[class*='ChatMessageFeedbackButtons_feedbackButtonsContainer']") != void 0) return true;
  23. return false;
  24. }
  25.  
  26. return new Promise((resolve, reject) => {
  27. const resolver = () => {
  28. if (isEndOfChat()) {
  29. return resolve();
  30. }
  31. window.setTimeout(resolver, 500);
  32. }
  33. setTimeout(resolver, 1000);
  34. })
  35. }
  36.  
  37.  
  38.  
  39. function createBtnAndInit() {
  40. const btnsCSS = `
  41. display: flex;
  42. align-items: center;
  43. justify-content: center;
  44. height: 50px;
  45. cursor: pointer;
  46. `
  47. renderLatex();
  48. // document.querySelector(".ChatMessageInputContainer_inputContainer__SQvPA").style.right = '55px';
  49. const _sc_renderBtnContainer = document.createElement("div");
  50. const container = document.querySelector(".ChatMessageInputContainer_inputContainer__SQvPA");
  51. if (container.childElementCount >=4){
  52. return;
  53. }
  54.  
  55. _sc_renderBtnContainer.classList.add("Button_buttonBase__0QP_m", "Button_flat__1hj0f");
  56. _sc_renderBtnContainer.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pen" viewBox="0 0 16 16"> <path d="m13.498.795.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001zm-.644.766a.5.5 0 0 0-.707 0L1.95 11.756l-.764 3.057 3.057-.764L14.44 3.854a.5.5 0 0 0 0-.708l-1.585-1.585z"/> </svg>';
  57. _sc_renderBtnContainer.addEventListener('click', () => { renderLatex(); });
  58.  
  59. const _sc_writeBtnContainer = document.createElement("div");
  60. _sc_writeBtnContainer.classList.add("Button_buttonBase__0QP_m", "Button_flat__1hj0f");
  61. _sc_writeBtnContainer.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-send-plus" viewBox="0 0 16 16"> <path d="M15.964.686a.5.5 0 0 0-.65-.65L.767 5.855a.75.75 0 0 0-.124 1.329l4.995 3.178 1.531 2.406a.5.5 0 0 0 .844-.536L6.637 10.07l7.494-7.494-1.895 4.738a.5.5 0 1 0 .928.372l2.8-7Zm-2.54 1.183L5.93 9.363 1.591 6.602l11.833-4.733Z"/> <path d="M16 12.5a3.5 3.5 0 1 1-7 0 3.5 3.5 0 0 1 7 0Zm-3.5-2a.5.5 0 0 0-.5.5v1h-1a.5.5 0 0 0 0 1h1v1a.5.5 0 0 0 1 0v-1h1a.5.5 0 0 0 0-1h-1v-1a.5.5 0 0 0-.5-.5Z"/> </svg>'
  62. _sc_writeBtnContainer.addEventListener('click', async () => {
  63. const txtArea = document.querySelector(".GrowingTextArea_textArea__eadlu");
  64. txtArea.value = `Please write your equations in LaTex, surround all latex blocks by '$$', and also surround all inline latex by '$'. \n\n ${txtArea.value}`;
  65. document.querySelector(".ChatMessageSendButton_sendButton__OMyK1").click();
  66. await endOfChat();
  67. renderLatex();
  68. })
  69.  
  70.  
  71. const lastChild = container.lastChild;
  72. container.insertBefore(_sc_renderBtnContainer, lastChild);
  73. container.insertBefore(_sc_writeBtnContainer, lastChild);
  74. }
  75.  
  76. window.MathJax = {
  77. tex: {
  78. inlineMath: [['$', '$'], ['\\(', '\\)']],
  79. displayMath: [['$$', '$$', ['\\[', '\\]']]]
  80. },
  81. svg: {
  82. fontCache: 'global'
  83. },
  84. startup: {
  85. typeset: false
  86. },
  87. options: {
  88. skipHtmlTags: [ // HTML tags that won't be searched for math
  89. 'script', 'noscript', 'style', 'textarea', 'annotation', 'annotation-xml'],
  90. includeHtmlTags: { // HTML tags that can appear within math
  91. br: '\n',
  92. wbr: '',
  93. '#comment': ''
  94. },
  95. ignoreHtmlClass: 'tex2jax_ignore',
  96. // class that marks tags not to search
  97. processHtmlClass: 'tex2jax_process',
  98. // class that marks tags that should be searched
  99. compileError: function (doc, math, err) {
  100. doc.compileError(math, err)
  101. },
  102. typesetError: function (doc, math, err) {
  103. doc.typesetError(math, err)
  104. },
  105. }
  106. };
  107.  
  108. (function () {
  109. var script = document.createElement('script');
  110. script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js';
  111. script.async = true;
  112. document.head.appendChild(script);
  113. })();
  114.  
  115. (() => {
  116. const _sc_style = document.createElement("style");
  117. //.MathJax { font-size: 1.3em !important; }
  118. _sc_style.innerHTML = `
  119. svg {
  120. height: initial;
  121. }
  122.  
  123. section[class*='PageWithSidebarLayout_mainSection'] {
  124. width: initial;
  125. max-width: initial;
  126. }
  127. `
  128. document.head.appendChild(_sc_style);
  129. })();
  130.  
  131. setInterval(() => {
  132. createBtnAndInit();
  133. }, 2000);
  134.  
  135.  
  136. // Your code here...
  137. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址