UNPKG

runasjs-prismjs-plugin

Version:

runJavascript is a prism js plugin which will run the javascript inside the code block.

2 lines (1 loc) 1.51 kB
!function(){"use strict";"undefined"!=typeof Prism&&"undefined"!=typeof document&&document.querySelector&&(Prism.plugins.toolbar?Prism.plugins.toolbar.registerButton("Run",(function(e){if("javascript"===e.language){let o;var t=document.createElement("button");t.className="run",t.setAttribute("type","button");var n=document.createElement("span");return n.textContent="Run",t.appendChild(n),t.addEventListener("click",(()=>{if(!e.element.getAttribute("data-customConsole")){let t=document.createElement("div"),n=document.createElement("span");n.textContent="Output: ",t.appendChild(n),o=document.createElement("pre"),o.style.cssText="color:#f5f5f5";let l=document.createElement("div");l.style.cssText="position:absolute;right:0.2em;bottom: 0;top: 0;height: max-content;padding: 0.2rem;margin: 0 0.2rem 0 0.2rem;";let s=document.createElement("button");s.textContent="Close",s.className="output-close",s.addEventListener("click",(()=>{e.element.removeAttribute("data-customConsole"),t.remove()})),l.appendChild(s),t.appendChild(o),t.appendChild(l),t.className="customConsole",e.element.appendChild(t),e.element.setAttribute("data-customConsole","true")}console.browserDefault=console.log,console.log=function(e){o.textContent+=e+"\n"};try{o.textContent="",new Function(e.code)(),o.style.cssText="color:#f5f5f5"}catch(e){o.textContent="",o.style.cssText="color:#f54141",console.log("Error>>> "+e.message)}console.log=console.browserDefault})),t}})):console.warn("runAsJs plugin loaded before Toolbar plugin."))}();