To copy KaTeX formula with ease.
javascript:(function(){const d={inline:["$","$"],display:["$$","$$"]};function cT(f){f.querySelectorAll(".katex-mathml + .katex-html").forEach(e=>e.parentNode.removeChild(e));f.querySelectorAll(".katex-mathml").forEach(e=>{let a=e.querySelector("annotation");if(a){let container=e.closest(".katex"),isD=container&&(container.classList.contains("katex-display")||(container.parentElement&&container.parentElement.classList.contains("katex-display")));let o=isD?d.display[0]:d.inline[0],c=isD?d.display[1]:d.inline[1],r=document.createElement("span");r.textContent=o+a.textContent+c;r.className="katex-tex";e.parentNode.replaceChild(r,e)}})};function kT(f){f.querySelectorAll(".katex-mathml + .katex-html").forEach(e=>e.parentNode.removeChild(e));f.querySelectorAll(".katex-mathml").forEach(e=>{let a=e.querySelector("annotation");if(a){let container=e.closest(".katex"),isD=container&&(container.classList.contains("katex-display")||(container.parentElement&&container.parentElement.classList.contains("katex-display")));let o=isD?d.display[0]:d.inline[0],c=isD?d.display[1]:d.inline[1];e.parentNode.replaceChild(document.createTextNode(o+a.textContent+c),e)}});return f}function ck(n){let el=n.nodeType===Node.ELEMENT_NODE?n:n.parentElement;return el&&el.closest(".katex")}document.addEventListener("copy",e=>{let s=window.getSelection();if(s.isCollapsed||!e.clipboardData)return;let r=s.getRangeAt(0),sk=ck(r.startContainer),ek=ck(r.endContainer);if(sk)r.setStartBefore(sk);if(ek)r.setEndAfter(ek);let f=r.cloneContents();if(!f.querySelector(".katex-mathml"))return;let pf=f.cloneNode(true);kT(pf);let pt=pf.textContent,hf=f.cloneNode(true);cT(hf);let div=document.createElement("div");div.appendChild(hf);e.clipboardData.setData("text/plain",pt);e.clipboardData.setData("text/html",div.innerHTML);e.preventDefault()})})();
Make this one line URL a separate bookmark, click it when you want to copy from a site use KaTeX for rendering formula (e.g. ChatGPT), and then you can DIRECTLY select the formula with cursor and use your favourite Ctrl-C!
The script is transformed from the original copy-tex.js
extension of the KaTeX project by ChatGPT, and reviewed by me to ensure safety and usefulness,
see the original ChatGPT history for clues: https://chatgpt.com/share/67c520f1-d448-8012-b56a-92ccf059bbca