Merge pull request #8744 from electron/MarshallOfSound-patch-1
Ensure userGesture is boolean when calling executeJavaScript
This commit is contained in:
commit
9547ff135f
4 changed files with 32 additions and 10 deletions
|
@ -541,7 +541,7 @@ that can't be set via `<webview>` attributes.
|
||||||
* `userAgent` String (optional) - A user agent originating the request.
|
* `userAgent` String (optional) - A user agent originating the request.
|
||||||
* `extraHeaders` String (optional) - Extra headers separated by "\n"
|
* `extraHeaders` String (optional) - Extra headers separated by "\n"
|
||||||
* `postData` ([UploadRawData](structures/upload-raw-data.md) | [UploadFile](structures/upload-file.md) | [UploadFileSystem](structures/upload-file-system.md) | [UploadBlob](structures/upload-blob.md))[] - (optional)
|
* `postData` ([UploadRawData](structures/upload-raw-data.md) | [UploadFile](structures/upload-file.md) | [UploadFileSystem](structures/upload-file-system.md) | [UploadBlob](structures/upload-blob.md))[] - (optional)
|
||||||
* `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files.
|
* `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files.
|
||||||
|
|
||||||
Loads the `url` in the window. The `url` must contain the protocol prefix,
|
Loads the `url` in the window. The `url` must contain the protocol prefix,
|
||||||
e.g. the `http://` or `file://`. If the load should bypass http cache then
|
e.g. the `http://` or `file://`. If the load should bypass http cache then
|
||||||
|
@ -672,7 +672,7 @@ Injects CSS into the current web page.
|
||||||
#### `contents.executeJavaScript(code[, userGesture, callback])`
|
#### `contents.executeJavaScript(code[, userGesture, callback])`
|
||||||
|
|
||||||
* `code` String
|
* `code` String
|
||||||
* `userGesture` Boolean (optional)
|
* `userGesture` Boolean (optional) - Default is `false`.
|
||||||
* `callback` Function (optional) - Called after script has been executed.
|
* `callback` Function (optional) - Called after script has been executed.
|
||||||
* `result` Any
|
* `result` Any
|
||||||
|
|
||||||
|
|
|
@ -115,7 +115,7 @@ const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
|
||||||
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args)
|
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args)
|
||||||
ipcMain.once(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, function (event, error, result) {
|
ipcMain.once(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, function (event, error, result) {
|
||||||
if (error == null) {
|
if (error == null) {
|
||||||
if (callback != null) callback(result)
|
if (typeof callback === 'function') callback(result)
|
||||||
resolve(result)
|
resolve(result)
|
||||||
} else {
|
} else {
|
||||||
reject(error)
|
reject(error)
|
||||||
|
@ -149,10 +149,17 @@ for (const method of webFrameMethodsWithResult) {
|
||||||
// WebContents has been loaded.
|
// WebContents has been loaded.
|
||||||
WebContents.prototype.executeJavaScript = function (code, hasUserGesture, callback) {
|
WebContents.prototype.executeJavaScript = function (code, hasUserGesture, callback) {
|
||||||
const requestId = getNextId()
|
const requestId = getNextId()
|
||||||
|
|
||||||
if (typeof hasUserGesture === 'function') {
|
if (typeof hasUserGesture === 'function') {
|
||||||
|
// Shift.
|
||||||
callback = hasUserGesture
|
callback = hasUserGesture
|
||||||
|
hasUserGesture = null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasUserGesture == null) {
|
||||||
hasUserGesture = false
|
hasUserGesture = false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.getURL() && !this.isLoadingMainFrame()) {
|
if (this.getURL() && !this.isLoadingMainFrame()) {
|
||||||
return asyncWebFrameMethods.call(this, requestId, 'executeJavaScript', callback, code, hasUserGesture)
|
return asyncWebFrameMethods.call(this, requestId, 'executeJavaScript', callback, code, hasUserGesture)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1851,7 +1851,7 @@ describe('BrowserWindow module', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('resolves the returned promise with the result', function (done) {
|
it('resolves the returned promise with the result when a callback is specified', function (done) {
|
||||||
ipcRenderer.send('executeJavaScript', code, true)
|
ipcRenderer.send('executeJavaScript', code, true)
|
||||||
ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {
|
ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {
|
||||||
assert.equal(result, expected)
|
assert.equal(result, expected)
|
||||||
|
@ -1859,6 +1859,14 @@ describe('BrowserWindow module', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('resolves the returned promise with the result when no callback is specified', function (done) {
|
||||||
|
ipcRenderer.send('executeJavaScript', code, false)
|
||||||
|
ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {
|
||||||
|
assert.equal(result, expected)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('resolves the returned promise with the result if the code returns an asyncronous promise', function (done) {
|
it('resolves the returned promise with the result if the code returns an asyncronous promise', function (done) {
|
||||||
ipcRenderer.send('executeJavaScript', asyncCode, true)
|
ipcRenderer.send('executeJavaScript', asyncCode, true)
|
||||||
ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {
|
ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {
|
||||||
|
|
|
@ -193,16 +193,23 @@ app.on('ready', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('executeJavaScript', function (event, code, hasCallback) {
|
ipcMain.on('executeJavaScript', function (event, code, hasCallback) {
|
||||||
|
let promise
|
||||||
|
|
||||||
if (hasCallback) {
|
if (hasCallback) {
|
||||||
window.webContents.executeJavaScript(code, (result) => {
|
promise = window.webContents.executeJavaScript(code, (result) => {
|
||||||
window.webContents.send('executeJavaScript-response', result)
|
window.webContents.send('executeJavaScript-response', result)
|
||||||
}).then((result) => {
|
|
||||||
window.webContents.send('executeJavaScript-promise-response', result)
|
|
||||||
}).catch((err) => {
|
|
||||||
window.webContents.send('executeJavaScript-promise-error', err)
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
window.webContents.executeJavaScript(code)
|
promise = window.webContents.executeJavaScript(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
promise.then((result) => {
|
||||||
|
window.webContents.send('executeJavaScript-promise-response', result)
|
||||||
|
}).catch((error) => {
|
||||||
|
window.webContents.send('executeJavaScript-promise-error', error)
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!hasCallback) {
|
||||||
event.returnValue = 'success'
|
event.returnValue = 'success'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue