mirror of
https://github.com/qier222/YesPlayMusic.git
synced 2025-02-09 23:03:00 +08:00
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
![]() |
import { contextBridge, ipcRenderer } from 'electron'
|
||
|
import fs from 'fs'
|
||
|
import { useLoading } from './loading'
|
||
|
import { domReady } from './utils'
|
||
|
|
||
|
const { appendLoading, removeLoading } = useLoading()
|
||
|
|
||
|
;(async () => {
|
||
|
await domReady()
|
||
|
|
||
|
appendLoading()
|
||
|
})()
|
||
|
|
||
|
// --------- Expose some API to the Renderer process. ---------
|
||
|
contextBridge.exposeInMainWorld('fs', fs)
|
||
|
contextBridge.exposeInMainWorld('removeLoading', removeLoading)
|
||
|
contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))
|
||
|
|
||
|
// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it.
|
||
|
function withPrototype(obj: Record<string, any>) {
|
||
|
const protos = Object.getPrototypeOf(obj)
|
||
|
|
||
|
for (const [key, value] of Object.entries(protos)) {
|
||
|
if (Object.prototype.hasOwnProperty.call(obj, key)) continue
|
||
|
|
||
|
if (typeof value === 'function') {
|
||
|
// Some native APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function.
|
||
|
obj[key] = function (...args: any) {
|
||
|
return value.call(obj, ...args)
|
||
|
}
|
||
|
} else {
|
||
|
obj[key] = value
|
||
|
}
|
||
|
}
|
||
|
return obj
|
||
|
}
|