feat: expose executeJavaScriptInIsolatedWorld on webContents (#21190)
* feat: expose executeJavaScriptInIsolatedWorld on webContents * Apply suggestions from code review Co-Authored-By: loc <andy@slack-corp.com>
This commit is contained in:
parent
a7c2f79a94
commit
34452ee69e
3 changed files with 43 additions and 12 deletions
|
@ -183,22 +183,25 @@ for (const method of webFrameMethods) {
|
|||
}
|
||||
}
|
||||
|
||||
const executeJavaScript = (contents, code, hasUserGesture) => {
|
||||
return ipcMainUtils.invokeInWebContents(contents, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', 'executeJavaScript', code, hasUserGesture)
|
||||
const waitTillCanExecuteJavaScript = async (webContents) => {
|
||||
if (webContents.getURL() && !webContents.isLoadingMainFrame()) return
|
||||
|
||||
return new Promise((resolve) => {
|
||||
webContents.once('did-stop-loading', () => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Make sure WebContents::executeJavaScript would run the code only when the
|
||||
// WebContents has been loaded.
|
||||
WebContents.prototype.executeJavaScript = function (code, hasUserGesture) {
|
||||
if (this.getURL() && !this.isLoadingMainFrame()) {
|
||||
return executeJavaScript(this, code, hasUserGesture)
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.once('did-stop-loading', () => {
|
||||
executeJavaScript(this, code, hasUserGesture).then(resolve, reject)
|
||||
})
|
||||
})
|
||||
}
|
||||
WebContents.prototype.executeJavaScript = async function (code, hasUserGesture) {
|
||||
await waitTillCanExecuteJavaScript(this)
|
||||
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', 'executeJavaScript', code, hasUserGesture)
|
||||
}
|
||||
WebContents.prototype.executeJavaScriptInIsolatedWorld = async function (code, hasUserGesture) {
|
||||
await waitTillCanExecuteJavaScript(this)
|
||||
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', 'executeJavaScriptInIsolatedWorld', code, hasUserGesture)
|
||||
}
|
||||
|
||||
// Translate the options of printToPDF.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue