-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsandbox.ts
27 lines (26 loc) · 1.01 KB
/
sandbox.ts
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
import { logToRenderer } from "$utils"
/**
* 本文件用来执行插件机制,输入为用户提供的插件(js 代码),返回为 markdown 插件
*/
window.addEventListener("message", async function (event) {
const source = event.source as {
window: WindowProxy
}
// Note: block 为实际数据
// func 为用户提供的 pluginCode,内容是 {bookmark: function(video){}, video: function(video){}} 类型的
// type 为模块的类型
const { block, func, type, id } = event.data || {}
try {
let result = null
const isExist = new Function(`return ${func}`)()?.[type]
if (isExist) {
result = new Function(`return ${func}`)()?.[type]?.(block) || ""
source.window.postMessage({ id, result, type }, event.origin)
} else {
source.window.postMessage({ id, result: undefined, type }, event.origin)
}
} catch (e) {
logToRenderer("error", "[Notion Flow] Plugin code run error", e)
source.window.postMessage({ id, result: null, type }, event.origin)
}
})