Merge pull request #8744 from electron/MarshallOfSound-patch-1

Ensure userGesture is boolean when calling executeJavaScript
This commit is contained in:
Kevin Sawicki 2017-03-06 11:19:23 -08:00 committed by GitHub
commit 9547ff135f
4 changed files with 32 additions and 10 deletions

View file

@ -541,7 +541,7 @@ that can't be set via `<webview>` attributes.
* `userAgent` String (optional) - A user agent originating the request.
* `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)
* `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,
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])`
* `code` String
* `userGesture` Boolean (optional)
* `userGesture` Boolean (optional) - Default is `false`.
* `callback` Function (optional) - Called after script has been executed.
* `result` Any

View file

@ -115,7 +115,7 @@ const asyncWebFrameMethods = function (requestId, method, callback, ...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) {
if (error == null) {
if (callback != null) callback(result)
if (typeof callback === 'function') callback(result)
resolve(result)
} else {
reject(error)
@ -149,10 +149,17 @@ for (const method of webFrameMethodsWithResult) {
// WebContents has been loaded.
WebContents.prototype.executeJavaScript = function (code, hasUserGesture, callback) {
const requestId = getNextId()
if (typeof hasUserGesture === 'function') {
// Shift.
callback = hasUserGesture
hasUserGesture = null
}
if (hasUserGesture == null) {
hasUserGesture = false
}
if (this.getURL() && !this.isLoadingMainFrame()) {
return asyncWebFrameMethods.call(this, requestId, 'executeJavaScript', callback, code, hasUserGesture)
} else {

View file

@ -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.once('executeJavaScript-promise-response', function (event, result) {
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) {
ipcRenderer.send('executeJavaScript', asyncCode, true)
ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {

View file

@ -193,16 +193,23 @@ app.on('ready', function () {
})
ipcMain.on('executeJavaScript', function (event, code, hasCallback) {
let promise
if (hasCallback) {
window.webContents.executeJavaScript(code, (result) => {
promise = window.webContents.executeJavaScript(code, (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 {
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'
}
})