chore: optimize code
This commit is contained in:
1 parent
b5a442d3b0
commit
1509250f92
2 files changed
+13
-12
No files matched your search
+11
-10
@@ -26,17 +26,18 @@ contextBridge.exposeInMainWorld('bridge', {
|
||||
|
||||
// `exposeInMainWorld` can not detect `prototype` attribute and methods, manually patch it.
|
||||
function withPrototype(obj: Record<string, any>) {
|
||||
const protos = Object.entries(Object.getPrototypeOf(obj))
|
||||
for (const [key, value] of protos) {
|
||||
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
if (typeof value === 'function') {
|
||||
// Some native API not work Renderer-process, like `NodeJS.EventEmitter['on']`. Wrap a function patch it.
|
||||
obj[key] = function (...args: any) {
|
||||
return value.call(obj, ...args)
|
||||
}
|
||||
} else {
|
||||
Object.assign(obj, { [key]: value })
|
||||
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 API not work in Renderer-process, like `NodeJS.EventEmitter['on']`. Wrap a function patch it.
|
||||
obj[key] = function (...args: any) {
|
||||
return value.call(obj, ...args)
|
||||
}
|
||||
} else {
|
||||
obj[key] = value
|
||||
}
|
||||
}
|
||||
return obj
|
||||
|
||||
Reference in new issue
Block a user