-
Notifications
You must be signed in to change notification settings - Fork 0
/
math-examples.js
241 lines (187 loc) · 5.74 KB
/
math-examples.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
var debugInfoGlobal = "";
function getDebugInfo(){
return debugInfoGlobal;
}
function addDebugInfo(info){
return debugInfoGlobal+=info+"<br>";
}
function clearDebugInfo(info){
return debugInfoGlobal="";
}
function checkEventOrder(){
window.addEventListener("load", (event) => {
addDebugInfo("window load");
});
document.addEventListener("readystatechange", (event) => {
addDebugInfo(`document readystatechange: ${document.readyState}`);
});
document.addEventListener("DOMContentLoaded", (event) => {
addDebugInfo(`document DOMContentLoaded`);
});
}
checkEventOrder();
function appendJsCss(element){
document.getElementsByTagName('head').item(0).appendChild(element);
element.addEventListener("load", (event) => {
if (element.hasAttribute("src")) {// js
addDebugInfo(`Loaded ${element.src}`);
}else{// css
addDebugInfo(`Loaded ${element.href}`);
}
}, false);
}
function includeDefer(file) {
var script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = true;
script.async = false;
appendJsCss(script);
}
function include(file) {
var script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = false;
script.async = false;
appendJsCss(script);
}
function includeAsync(file) {
var script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = false;
script.async = true;
appendJsCss(script);
}
function loadJsCssFile(path, type) {
if (type == "js") {
var fileref = document.createElement("script");
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", path);
} else if (type == "css") {
var fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", path);
}
appendJsCss(fileref);
}
// Stylesheets
loadJsCssFile("https://math-examples.github.io/web/math-examples.css", "css");
/* Include Many js files */
includeDefer('https://math-examples.github.io/web/displayTime.js');
//include('https://math-examples.github.io/web/requestUserRepoContent.js');
include('https://math-examples.github.io/web/txt2html.js');
//include('https://math-examples.github.io/web/iframe.js');
includeAsync('https://math-examples.github.io/web/resizeText.js');
includeAsync('https://math-examples.github.io/web/showLog.js');
//includeAsync('https://math-examples.github.io/web/file.js');
includeAsync('https://math-examples.github.io/web/toggle.js');
includeAsync('https://math-examples.github.io/web/tts.js');
//######## file.js ###########
function saveAs(content, fileName) {
const anchor = document.createElement("a");
const isBlob = content.toString().indexOf("Blob") > -1;
let url = content;
if (isBlob) {
url = window.URL.createObjectURL(content);
}
anchor.href = url;
anchor.download = fileName;
anchor.click();
if (isBlob) {
window.URL.revokeObjectURL(url);
}
//document.removeChild(anchor);
}
function saveTxt(txt, name){
var blob = new Blob([txt], { type: "text/plain;charset=utf-8" });
saveAs(blob, name);
}
//######## txt2html.js ###########
function txt2html(txt){
const data = txt.split(/\r?\n/).filter(element => element);
let div = document.getElementById('txt2html');
while(div.firstChild) {
div.removeChild(div.firstChild);
}
let length = data.length;
for (let j = 1; j <= length; ++j) {
let i = j-1;
let p = document.createElement('p');
p.setAttribute('class', 'contentClass');
var name = data[i];
p.innerHTML = (`${name}`);
/* li.innerHTML = (`
<p>${name}</p>
`); */
div.appendChild(p);
}
}
//######## iframe.js ###########
const globalHasIframe = 1;
const trustedOrigins = ["https://math-examples.github.io"];
function runCode(x) {
if(x.startsWith("scode=")){
const code = x.substring(6);
const f = new Function(code);
try {
f();
} catch (e) {
showLog(e.message);
}
}else if(x.startsWith("code=")){
const iframe = document.querySelector("iframe");
if(iframe)iframe.contentWindow.postMessage(x, '*');
}
}
function replaceWebHref() {
//var x = document.getElementsByClassName("requestUserRepoContent");
var x = document.querySelectorAll("a");
for (var i = 0; i < x.length; i++) {
if (!x[i].hasAttribute("href"))continue;
x[i].href = "https://math-examples.github.io/web/receive-href-web.html"+"?href="+x[i].href;
//alert(x[i].href);
}
//alert(x.length);
}
function tellIframe(code){
if(window.self !== window.top){
//alert("not top window");
return;
}
const iframe = document.querySelector("iframe");
if(!iframe)return;
//const code = "resizeText(1)";
iframe.contentWindow.postMessage(code, '*');
}
function onMsgIframe(event) {
if (!trustedOrigins.includes(event.origin)) return;
if(event.data.startsWith("code=")){
const code = event.data.substring(5);
const f = new Function(code);
try {
f();
} catch (e) {
showLog(e.message);
}
}else{
alert(`Message from main window: ${event.data} from ${event.origin}`);
}
//event.source.postMessage('succeeded', event.origin);
}
function initIframe(){
if(window.self === window.top)return;
window.addEventListener("message", onMsgIframe, false);
document.addEventListener("readystatechange", (event) => {
if (event.target.readyState === "interactive") {
replaceWebHref();
//initLoader();
} else if (event.target.readyState === "complete") {
//initApp();
}
});
}
initIframe();
//######## x.js ###########