-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,84 @@ | ||
const electron = require("electron"); | ||
|
||
const electronPath = require.resolve("electron"); | ||
delete require.cache[electronPath].exports; | ||
|
||
// God, have mercy on my soul. I'm so sorry. | ||
require.cache[electronPath].exports = { | ||
...electron, | ||
contextBridge: { | ||
exposeInMainWorld(name, properties) { | ||
window[name] = properties; | ||
}, | ||
}, | ||
}; | ||
|
||
electron.ipcRenderer.invoke("NEPTUNE_BUNDLE_FETCH").then((bundle) => { | ||
electron.webFrame.executeJavaScript(bundle); | ||
}); | ||
|
||
const originalPreload = electron.ipcRenderer.sendSync("NEPTUNE_ORIGINAL_PRELOAD"); | ||
if (originalPreload) require(originalPreload); | ||
function createEvalScope(code) { | ||
return electron.ipcRenderer.sendSync("NEPTUNE_CREATE_EVAL_SCOPE", code); | ||
} | ||
|
||
function getNativeValue(id, name) { | ||
if ( | ||
electron.ipcRenderer.sendSync( | ||
"NEPTUNE_RUN_IN_EVAL_SCOPE", | ||
id, | ||
`typeof neptuneExports.${name}` | ||
).value == "function" | ||
) | ||
return (...args) => { | ||
funcReturn = electron.ipcRenderer.sendSync( | ||
"NEPTUNE_RUN_IN_EVAL_SCOPE", | ||
id, | ||
`neptuneExports.${name}(${args | ||
.map((arg) => | ||
typeof arg != "function" ? JSON.stringify(arg) : arg.toString() | ||
) | ||
.join(",")})` | ||
); | ||
|
||
if (funcReturn.type == "promise") { | ||
return new Promise((res, rej) => { | ||
electron.ipcRenderer.once(funcReturn.value, (ev, { type, value }) => { | ||
type == "resolve" ? res(value) : rej(value); | ||
}); | ||
}); | ||
} | ||
|
||
if (funcReturn.type == "error") { | ||
throw new Error(funcReturn.value); | ||
} | ||
|
||
return funcReturn.value; | ||
}; | ||
|
||
return electron.ipcRenderer.sendSync( | ||
"NEPTUNE_RUN_IN_EVAL_SCOPE", | ||
id, | ||
`neptuneExports.${name}` | ||
); | ||
} | ||
|
||
function deleteEvalScope(id) { | ||
return electron.ipcRenderer.sendSync("NEPTUNE_DELETE_EVAL_SCOPE", id); | ||
} | ||
|
||
electron.contextBridge.exposeInMainWorld("NeptuneNative", { | ||
createEvalScope, | ||
getNativeValue, | ||
deleteEvalScope, | ||
}); | ||
|
||
electron.contextBridge.exposeInMainWorld("electron", { | ||
ipcRenderer: Object.fromEntries( | ||
[ | ||
"on", | ||
"off", | ||
"once", | ||
"addListener", | ||
"removeListener", | ||
"removeAllListeners", | ||
"send", | ||
"invoke", | ||
"sendSync", | ||
"postMessage", | ||
"sendToHost", | ||
].map((n) => [n, electron.ipcRenderer[n]]) | ||
), | ||
}); | ||
|
||
const originalPreload = electron.ipcRenderer.sendSync( | ||
"NEPTUNE_ORIGINAL_PRELOAD" | ||
); | ||
|
||
if (originalPreload) require(originalPreload); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters