Skip to content

Commit

Permalink
feat: add support for webContents.executeJavaScript (#366)
Browse files Browse the repository at this point in the history
* webContents.executeJavaScript now works as intended

* Document difference with webContents.executeJavaScript between spectron and electron

* tests for webContents.executeJavaScript

* fix lint error

* var to const
  • Loading branch information
magne4000 authored and codebytere committed Jul 15, 2019
1 parent 29a2129 commit 474333e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ app.webContents.savePage('/Users/kevin/page.html', 'HTMLComplete')
})
```

##### executeJavaScript
The async `executeJavaScript` API is supported but instead of taking a callback it
returns a `Promise` that will resolve with the result of the last statement of the
script.

```js
app.webContents.executeJavaScript('1 + 2')
.then(function (result) {
console.log(result) // prints 3
})
```

#### mainProcess

The `mainProcess` property is an alias for `require('electron').remote.process`.
Expand Down
20 changes: 19 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ Api.prototype.addWebContentsApis = function (api) {
app.webContents = {}

const asyncApis = {
'webContents.savePage': true
'webContents.savePage': true,
'webContents.executeJavaScript': true
}

Object.keys(api).filter(function (name) {
Expand All @@ -361,6 +362,7 @@ Api.prototype.addWebContentsApis = function (api) {
})

this.addSavePageSupport()
this.addExecuteJavaScriptSupport()
}

Api.prototype.addSavePageSupport = function () {
Expand All @@ -385,6 +387,22 @@ Api.prototype.addSavePageSupport = function () {
}
}

Api.prototype.addExecuteJavaScriptSupport = function () {
const app = this.app
const self = this

app.client.addCommand('webContents.executeJavaScript', function (code, useGesture) {
return this.executeAsync(function (code, useGesture, requireName, done) {
const webContents = window[requireName]('electron').remote.getCurrentWebContents()
webContents.executeJavaScript(code, useGesture, done)
}, code, useGesture, self.requireName).then(getResponseValue)
})

app.webContents.executeJavaScript = function () {
return app.client['webContents.executeJavaScript'].apply(app.client, arguments)
}
}

Api.prototype.addProcessApis = function (api) {
var app = this.app
app.rendererProcess = {}
Expand Down
1 change: 1 addition & 0 deletions lib/spectron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ declare module "spectron" {
):boolean;
savePage(fullPath:string, saveType:"HTMLOnly" | "HTMLComplete" | "MHTML"):Promise<void>;
savePage(fullPath:string, saveType:"HTMLOnly" | "HTMLComplete" | "MHTML"):any;
executeJavaScript(code:string, userGesture?:boolean):Promise<any>;
}

type BasicAppSettings = {
Expand Down
20 changes: 20 additions & 0 deletions test/application-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ describe('application loading', function () {
})
})

describe('webContents.executeJavaScript', function () {
it('executes the given script and returns the result of its last statement (sync)', function () {
return app.webContents.executeJavaScript('1 + 2').then(function (result) {
expect(result).to.equal(3)
})
})

it('executes the given script and returns the result of its last statement (async)', function () {
return app.webContents.executeJavaScript(`
new Promise(function(resolve){
setTimeout(function(){
resolve("ok")
}, 1000)
})`
).then(function (result) {
expect(result).to.equal('ok')
})
})
})

describe('electron.ipcRenderer.send', function () {
it('sends the message to the main process', function () {
return app.electron.remote.getGlobal('ipcEventCount').should.eventually.equal(0)
Expand Down

0 comments on commit 474333e

Please sign in to comment.