Commit 17672bcb authored by eternal-flame-AD's avatar eternal-flame-AD

Add jsland tool

parent e472631c
---
title: Javascript evaluator
priority: -1
---
<style scoped>
#js-land {
width: 100%;
height: 20rem;
}
</style>
<form>
<div class="form-group">
<input type="checkbox" id="js-format" checked="true"> Syntax Highlighting</input>
</div>
<div class="form-group">
<input type="checkbox" id="js-auto-eval" checked="true"> Auto Evaluate</input>
</div>
<div class="form-group">
<button class="btn btn-primary form-control" id="js-eval" disabled> Evaluate</button>
</div>
</form>
<div contenteditable="true" id="js-land">
</div>
<div> Result: <span id="js-result"></span></div>
<script>
const autoEval = document.querySelector("#js-auto-eval")
const evalBtn = document.querySelector("#js-eval")
const elt = document.querySelector("#js-land")
let lastStatusChange = new Date()
const updateEvalBtnStatus = function _self(status) {
lastStatusChange = new Date()
evalStatus = status
evalBtn.classList.remove("btn-danger")
evalBtn.classList.remove("btn-primary")
evalBtn.classList.remove("btn-success")
const scheduleChangeIdle = ()=>{
const timeout = autoEval.checked?3000:500;
setTimeout(()=>{
if (new Date()-lastStatusChange>=timeout) _self("idle")
}, timeout)
}
switch (status) {
case "working":
evalBtn.disabled = true
evalBtn.innerText = "Evaluating";
evalBtn.classList.add("btn-primary");
break
case "idle":
evalBtn.disabled = false
evalBtn.innerText = "Evaluate";
evalBtn.classList.add("btn-primary");
break
case "error":
evalBtn.disabled = true
evalBtn.innerText = "Error";
evalBtn.classList.add("btn-danger");
scheduleChangeIdle();
break
case "success":
evalBtn.disabled = true
evalBtn.innerText = "Success";
evalBtn.classList.add("btn-success");
scheduleChangeIdle();
break
}
}
evalBtn.disabled = autoEval.checked
autoEval.onchange = ()=>{
evalBtn.disabled = autoEval.checked
}
elt.innerText ='() => {\r\n"use strict";\r\nreturn "hello world";\r\n}\r\n'
const doFormat = ()=>{
if (document.querySelector("#js-format").checked) {
try{
hljs.highlightBlock(elt);
} catch {}
}
};
const doEval = ()=>{
updateEvalBtnStatus("working")
return new Promise((resolve, reject)=>{
setTimeout(()=>{
const cb = (res) => {
document.querySelector("#js-result").innerText = res
}
let content = elt.innerText
try {
let res = eval(content)
if (typeof res == "function") {
cb(res())
updateEvalBtnStatus("success")
resolve()
} else {
cb(res)
updateEvalBtnStatus("success")
resolve()
}
} catch (e) {
cb(`Error: ${e.message}`)
updateEvalBtnStatus("error")
reject()
}
}, 20)
})
};
evalBtn.onclick = (e)=>{
e.preventDefault()
doEval();
}
let lastEdit = null;
elt.onkeydown = function (e) {
{
lastEdit = new Date();
setTimeout(()=>{
if (new Date()-lastEdit>=3000) {
doFormat();
}
},3000)
}
if (autoEval.checked) {
doEval()
}
};
Promise.all([
enqueue_stylesheet("hljs", "//cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.13.1/build/styles/default.min.css", {
integrity: "sha384-zhIsEafzyQWHSoMCQ4BfT8ZlRXQyIFwAHAJn32PNdsb8n6tVysGZSLpEEIvCskw4",
}),
enqueue_script("hljs", "//cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.13.1/build/highlight.min.js", {
integrity: "sha384-BlPof9RtjBqeJFskKv3sK3dh4Wk70iKlpIe92FeVN+6qxaGUOUu+mZNpALZ+K7ya",
}),
]).then(doFormat)
doEval();
</script>
\ No newline at end of file
......@@ -46,7 +46,7 @@
for = "/*.html"
[headers.values]
Cache-Control = "max-age=600, must-revalidate"
Content-Security-Policy = "script-src 'self' 'unsafe-inline' https://code.jquery.com:443 https://gist.github.com:443 https://eternalflame-cn.disqus.com:443 https://disquscdn.com:443 https://*.disquscdn.com:443 https://c.disquscdn.com:443 https://disqus.com:443 https://stackpath.bootstrapcdn.com:443 https://platform.twitter.com:443 https://*.twimg.com:443; style-src 'self' data: 'unsafe-inline' https://stackpath.bootstrapcdn.com:443 https://assets-cdn.github.com:443 https://use.fontawesome.com:443 https://*.twitter.com:443 https://ton.twimg.com:443 https://*.disquscdn.com:443 https://*.githubassets.com:443; img-src 'self' data: https://gitlab.com:443 https://*.gitlab.com:443 https://github.com:443 https://*.github.com:443 https://*.githubusercontent.com:443 https://img.shields.io:443 https://*.twimg.com:443 https://*.twitter.com:443; font-src 'self' data: https://stackpath.bootstrapcdn.com:443 https://use.fontawesome.com:443; connect-src 'self' https://syndication.twitter.com:443 https://urlreq.appspot.com:443; media-src 'self' ; frame-src 'self' https://player.bilibili.com:443 https://music.163.com:443 https://open.spotify.com:443 https://www.strava.com:443 https://disqus.com:443 https://*.twitter.com:443; upgrade-insecure-requests; block-all-mixed-content; disown-opener; referrer no-referrer;"
Content-Security-Policy = "script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net:443 https://code.jquery.com:443 https://gist.github.com:443 https://eternalflame-cn.disqus.com:443 https://disquscdn.com:443 https://*.disquscdn.com:443 https://c.disquscdn.com:443 https://disqus.com:443 https://stackpath.bootstrapcdn.com:443 https://platform.twitter.com:443 https://*.twimg.com:443; style-src 'self' data: 'unsafe-inline' https://cdn.jsdelivr.net:443 https://stackpath.bootstrapcdn.com:443 https://assets-cdn.github.com:443 https://use.fontawesome.com:443 https://*.twitter.com:443 https://ton.twimg.com:443 https://*.disquscdn.com:443 https://*.githubassets.com:443; img-src 'self' data: https://gitlab.com:443 https://*.gitlab.com:443 https://github.com:443 https://*.github.com:443 https://*.githubusercontent.com:443 https://img.shields.io:443 https://*.twimg.com:443 https://*.twitter.com:443; font-src 'self' data: https://stackpath.bootstrapcdn.com:443 https://use.fontawesome.com:443; connect-src 'self' https://syndication.twitter.com:443 https://urlreq.appspot.com:443; media-src 'self' ; frame-src 'self' https://player.bilibili.com:443 https://music.163.com:443 https://open.spotify.com:443 https://www.strava.com:443 https://disqus.com:443 https://*.twitter.com:443; upgrade-insecure-requests; block-all-mixed-content; disown-opener; referrer no-referrer;"
Referrer-Policy = "same-origin"
X-Download-Options = "noopen"
X-Frame-Options = "allow-from https://player.bilibili.com/ https://music.163.com/ https://open.spotify.com/ https://www.strava.com/ https://disqus.com/ https://*.twitter.com/"
......@@ -55,7 +55,7 @@
for = "/*/"
[headers.values]
Cache-Control = "max-age=600, must-revalidate"
Content-Security-Policy = "script-src 'self' 'unsafe-inline' https://code.jquery.com:443 https://gist.github.com:443 https://eternalflame-cn.disqus.com:443 https://disquscdn.com:443 https://*.disquscdn.com:443 https://c.disquscdn.com:443 https://disqus.com:443 https://stackpath.bootstrapcdn.com:443 https://platform.twitter.com:443 https://*.twimg.com:443; style-src 'self' data: 'unsafe-inline' https://stackpath.bootstrapcdn.com:443 https://assets-cdn.github.com:443 https://use.fontawesome.com:443 https://*.twitter.com:443 https://ton.twimg.com:443 https://*.disquscdn.com:443 https://*.githubassets.com:443; img-src 'self' data: https://gitlab.com:443 https://*.gitlab.com:443 https://github.com:443 https://*.github.com:443 https://*.githubusercontent.com:443 https://img.shields.io:443 https://*.twimg.com:443 https://*.twitter.com:443; font-src 'self' data: https://stackpath.bootstrapcdn.com:443 https://use.fontawesome.com:443; connect-src 'self' https://syndication.twitter.com:443 https://urlreq.appspot.com:443; media-src 'self' ; frame-src 'self' https://player.bilibili.com:443 https://music.163.com:443 https://open.spotify.com:443 https://www.strava.com:443 https://disqus.com:443 https://*.twitter.com:443; upgrade-insecure-requests; block-all-mixed-content; disown-opener; referrer no-referrer;"
Content-Security-Policy = "script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net:443 https://code.jquery.com:443 https://gist.github.com:443 https://eternalflame-cn.disqus.com:443 https://disquscdn.com:443 https://*.disquscdn.com:443 https://c.disquscdn.com:443 https://disqus.com:443 https://stackpath.bootstrapcdn.com:443 https://platform.twitter.com:443 https://*.twimg.com:443; style-src 'self' data: 'unsafe-inline' https://cdn.jsdelivr.net:443 https://stackpath.bootstrapcdn.com:443 https://assets-cdn.github.com:443 https://use.fontawesome.com:443 https://*.twitter.com:443 https://ton.twimg.com:443 https://*.disquscdn.com:443 https://*.githubassets.com:443; img-src 'self' data: https://gitlab.com:443 https://*.gitlab.com:443 https://github.com:443 https://*.github.com:443 https://*.githubusercontent.com:443 https://img.shields.io:443 https://*.twimg.com:443 https://*.twitter.com:443; font-src 'self' data: https://stackpath.bootstrapcdn.com:443 https://use.fontawesome.com:443; connect-src 'self' https://syndication.twitter.com:443 https://urlreq.appspot.com:443; media-src 'self' ; frame-src 'self' https://player.bilibili.com:443 https://music.163.com:443 https://open.spotify.com:443 https://www.strava.com:443 https://disqus.com:443 https://*.twitter.com:443; upgrade-insecure-requests; block-all-mixed-content; disown-opener; referrer no-referrer;"
Referrer-Policy = "same-origin"
X-Download-Options = "noopen"
X-Frame-Options = "allow-from https://player.bilibili.com/ https://music.163.com/ https://open.spotify.com/ https://www.strava.com/ https://disqus.com/ https://*.twitter.com/"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment