escrcpy/electron/helpers/index.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

import { resolve } from 'node:path'
import { contextBridge } from 'electron'
export const isPackaged = process.env.IS_PACKAGED === 'true'
export const extraResolve = (value) => {
return isPackaged
? resolve(process.resourcesPath, `extra/${value}`)
: resolve(`electron/resources/extra/${value}`)
}
export const buildResolve = value =>
resolve(`electron/resources/build/${value}`)
export function exposeContext(key, value) {
if (process.contextIsolated) {
try {
contextBridge.exposeInMainWorld(key, value)
}
catch (error) {
console.error(error)
}
}
else {
window[key] = value
}
}
/**
* 创建一个代理对象将目标对象的指定方法转发并执行
*
* @param {object} targetObject - 目标对象包含要代理的方法
* @param {string[]} methodNames - 要代理的方法名称数组
* @returns {object} - 代理对象包含转发的方法
*/
export function createProxy(targetObject, methodNames) {
return methodNames.reduce((proxyObj, methodName) => {
proxyObj[methodName] = (...args) => targetObject[methodName](...args)
return proxyObj
}, {})
}