-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopytext.js
29 lines (27 loc) · 1.28 KB
/
copytext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Create clipboard textarea
var textarea = document.createElement("textarea");
textarea.id = "textarea";
textarea.style = "z-index: -999; top: 0; left = 0; position: fixed; width: 2em; height: 2em; padding: 0px; border: none; outline: none; boxShadow: none";
document.body.appendChild(textarea);
// Walk all new TeX nodes and register copy-on-click callbacks
function getText() {
var textNodes = document.querySelectorAll("span.MathJax_Preview:not(.found)");
for(var i = 0; i < textNodes.length; i++){
var clickableParent = textNodes[i].parentElement.parentElement.parentElement;
// Skip the preview over the input field
if (clickableParent.classList.contains('_5irm'))
continue;
clickableParent.setAttribute("onmousedown", "copy(this)");
clickableParent.title = "Click to Copy";
textNodes[i].className +=" found";
}
};
// Copy TeX markup from underlying node
function copy(currentNode) {
var textScript = currentNode.getElementsByTagName("script")[0];
var textarea = document.getElementById("textarea");
textarea.value = (textScript.type=="math/tex") ? ("\\(" + textScript.textContent + "\\)") : ("$$" + textScript.textContent + "$$");
textarea.select();
document.execCommand("copy");
textarea.value = "";
};