-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformula.html
166 lines (146 loc) · 5.08 KB
/
formula.html
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Formula Embed</title>
<style>
button {
background-color: rgb(187, 187, 187);
border: 1px solid rgb(133, 133, 133);
border-radius: 4px;
color: black;
cursor: pointer;
font-size: 0.7em;
margin: 0;
padding: 0.4em;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
}
button:hover {
background-color: gray;
border: 1px solid gray;
box-shadow: 2px 4px 6px 0 rgba(0,0,0,0.24), 3px 6px 8px 0 rgba(0,0,0,0.19);
color: white;
}
</style>
</head>
<body>
<p id="formula"></p>
</body>
<script type="text/javascript">
window.MathJax = {
jax: ["input/TeX", "input/MathML", "output/SVG", "output/PreviewHTML"],
extensions: ["tex2jax.js", "mml2jax.js", "MathMenu.js", "MathZoom.js", "AssistiveMML.js",
"a11y/accessibility-menu.js"
],
showMathMenu: true,
showProcessingMessages: false,
messageStyle: "none",
SVG: {
useGlobalCache: false
},
TeX: {
extensions: ["AMSmath.js", "AMSsymbols.js", "autoload-all.js", "noErrors.js", "noUndefined.js"]
},
AuthorInit: function () {
MathJax.Hub.Register.StartupHook("Begin", init);
MathJax.Hub.Register.StartupHook("End", addEvent);
}
}
function init() {
const urlParams = new URLSearchParams(window.location.search);
formula = urlParams.get('formula');
title = urlParams.get('title');
var formulaElement = document.getElementById('formula');
formulaElement.innerHTML = formula;
}
function isiOS() {
return navigator.userAgent.match(/ipad|iphone/i);
}
function setClipboard(value) {
var tempInput = document.createElement("input");
tempInput.style = "position: absolute; left: -10000px; top: -10000px";
tempInput.value = value;
document.body.appendChild(tempInput);
if (isiOS()) {
tempInput.mouseup = function (event) {
event.stop();
}
tempInput.focus();
tempInput.setSelectionRange(0, 99999);
} else {
tempInput.select();
}
document.execCommand("copy");
document.body.removeChild(tempInput);
}
function copyTex() {
setClipboard(formula);
}
// Link: http://bl.ocks.org/curran/7cf9967028259ea032e8
function svgDataURL(svg) {
var svgAsXML = (new XMLSerializer).serializeToString(svg);
return "data:image/svg+xml," + encodeURIComponent(svgAsXML);
}
function downloadDataURL(dataURL) {
var dl = document.createElement("a");
document.body.appendChild(dl); // This line makes it work in Firefox.
dl.setAttribute("href", dataURL);
dl.setAttribute("download", title + ".svg");
dl.click();
}
function downloadSVG() {
var formulaElement = document.getElementById("formula");
downloadDataURL(svgDataURL(formulaElement.getElementsByTagName("svg")[0]));
}
// init();
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" async></script>
<script>
async function addEvent() {
var formulaBlock = await
function () {
return new Promise((resolve, reject) => {
var check = setInterval(() => {
var formulaBlock = document.getElementsByClassName("MathJax_SVG_Display")[0];
if (formulaBlock !== undefined) {
clearInterval(check);
resolve(formulaBlock);
}
}, 100);
});
}();
var texbutton = document.createElement("button");
texbutton.textContent = "Copy Tex";
texbutton.onclick = copyTex;
texbutton.style.display = "none";
var svgbutton = document.createElement("button");
svgbutton.textContent = "Download SVG";
svgbutton.onclick = downloadSVG;
svgbutton.style.display = "none";
var div = document.createElement("div");
div.style.display = "flex";
div.style.position = "absolute";
div.style.right = "0";
div.style.bottom = "0";
div.style["z-index"] = "1";
div.appendChild(texbutton);
div.appendChild(svgbutton);
var formulaArea = document.getElementsByClassName("MathJax_SVG")[0];
formulaArea.appendChild(div);
formulaArea.onmouseenter = function (event) {
var buttons = this.getElementsByTagName("button");
for (let button of buttons) {
button.style.display = "";
}
}
formulaArea.onmouseleave = function (event) {
var buttons = this.getElementsByTagName("button");
for (let button of buttons) {
button.style.display = "none";
}
}
}
</script>
</html>