1. ScenarioRecently I've been working on something similar to a plugin system based on the web, so I've been playing around with the js sandbox to execute code from third-party applications. 2. Basic functions of sandbox Before implementation (well, actually after researching some solutions), it was determined that the sandbox would implement the upper layer functions based on export interface IEventEmitter { /** * Listening event * @param channel * @param handle */ on(channel: string, handle: (data: any) => void): void; /** * Cancel listening * @param channel */ offByChannel(channel: string): void; /** * Trigger event * @param channel * @param data */ emit(channel: string, data: any): void; } /** * A basic js vm capability */ export interface IJavaScriptShadowbox extends IEventEmitter { /** * Execute arbitrary code * @param code */ eval(code: string): void; /** * Destroy the instance */ destroy(): void; } In addition to the ability to communicate, two additional methods are required:
JavaScript sandbox diagram: Below we will demonstrate how to use 3. iframe implementation To be honest, when talking about sandboxes in the web, the first thing that comes to mind is probably Of course, you can wrap the js code in html and then execute it function evalByIframe(code: string) { const html = `<!DOCTYPE html><body><script>$[code]</script></body></html>`; const iframe = document.createElement("iframe"); iframe.width = "0"; iframe.height = "0"; iframe.style.display = "none"; document.body.appendChild(iframe); const blob = new Blob([html], { type: "text/html" }); iframe.src = URL.createObjectURL(blob); return iframe; } evalByIframe(` document.body.innerHTML = 'hello world' console.log('location.href: ', location.href) console.log('localStorage: ',localStorage) `); But
4. Web worker implementation Basically, a function evalByWebWorker(code: string) { const blob = new Blob([code], { type: "application/javascript" }); const url = URL.createObjectURL(blob); return new Worker(url); } evalByWebWorker(` console.log('location.href: ', location.href) // console.log('localStorage: ', localStorage) `); But at the same time, it is indeed better than
5. Quickjs implementation The main inspiration for using What is quickjs? It is a JavaScript runtime, and while the most common runtimes we use are browsers and async function evalByQuickJS(code: string) { const quickJS = await getQuickJS(); const vm = quickJS.createVm(); const res = vm.dump(vm.unwrapResult(vm.evalCode(code))); vm.dispose(); return res; } console.log(await evalByQuickJS(`1+1`)); advantage:
shortcoming:
6. ConclusionFinally, we chose to implement the EventEmitter of web worker and quickjs based on the interface, and support the ability to switch at any time. This is the end of this article about JavaScript sandbox exploration. For more relevant JavaScript sandbox content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of CSS BEM writing standards
>>: Detailed explanation of mysql trigger example
1. Common connections for mysql INNER JOIN (inner...
Create table data CREATE TABLE `praise_info` ( `i...
Where is my hometown when I look northwest? How m...
centos7 switch boot kernel Note: If necessary, it...
1 Download MySQL8 from the official website and i...
I have previously shared the usage of asynchronou...
Recently, in order to realize the course design, ...
Table of contents App Update Process Rough flow c...
Based on SEO and security considerations, a 301 r...
Let’s take a look at the panoramic view effect: D...
Trigger Introduction A trigger is a special store...
1, %: represents any 0 or more characters. It can...
Linux is currently the most widely used server op...
This article shares the specific code of Navicat ...
1. This afternoon, due to the requirements of the...