add spec
This commit is contained in:
parent
a734326907
commit
8386baf267
5 changed files with 44 additions and 28 deletions
|
@ -1,7 +1,3 @@
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
|
|
||||||
module.exports = new EventEmitter;
|
module.exports = new EventEmitter;
|
||||||
|
|
||||||
// Every webContents would add a listenter to the
|
|
||||||
// WEB_FRAME_RESPONSE event, so ignore the listenters warning.
|
|
||||||
module.exports.setMaxListeners(0);
|
|
||||||
|
|
|
@ -67,10 +67,6 @@ const webFrameMethods = [
|
||||||
'setZoomLevelLimits'
|
'setZoomLevelLimits'
|
||||||
];
|
];
|
||||||
|
|
||||||
const asyncWebFrameMethods = [
|
|
||||||
'executeJavaScript',
|
|
||||||
];
|
|
||||||
|
|
||||||
let wrapWebContents = function(webContents) {
|
let wrapWebContents = function(webContents) {
|
||||||
// webContents is an EventEmitter.
|
// webContents is an EventEmitter.
|
||||||
var controller, method, name, ref1;
|
var controller, method, name, ref1;
|
||||||
|
@ -112,37 +108,32 @@ let wrapWebContents = function(webContents) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let method of asyncWebFrameMethods) {
|
const asyncWebFrameMethods = function(requestId, method, ...args) {
|
||||||
webContents[method] = function() {
|
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args);
|
||||||
let args = Array.prototype.slice.call(arguments);
|
ipcMain.once('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE_' + requestId, function(event, result) {
|
||||||
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', method, args);
|
if (responseCallback[requestId]) {
|
||||||
};
|
responseCallback[requestId](result);
|
||||||
}
|
delete responseCallback[requestId];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Make sure webContents.executeJavaScript would run the code only when the
|
// Make sure webContents.executeJavaScript would run the code only when the
|
||||||
// webContents has been loaded.
|
// webContents has been loaded.
|
||||||
const executeJavaScript = webContents.executeJavaScript;
|
|
||||||
webContents.executeJavaScript = function(code, hasUserGesture, callback) {
|
webContents.executeJavaScript = function(code, hasUserGesture, callback) {
|
||||||
let requestId = getNextId();
|
let requestId = getNextId();
|
||||||
if (typeof hasUserGesture === "function") {
|
if (typeof hasUserGesture === "function") {
|
||||||
callback = hasUserGesture;
|
callback = hasUserGesture;
|
||||||
hasUserGesture = false;
|
hasUserGesture = false;
|
||||||
}
|
}
|
||||||
if (callback !== null)
|
if (callback != null)
|
||||||
responseCallback[requestId] = callback;
|
responseCallback[requestId] = callback;
|
||||||
if (this.getURL() && !this.isLoading())
|
if (this.getURL() && !this.isLoading())
|
||||||
return executeJavaScript.call(this, requestId, code, hasUserGesture);
|
return asyncWebFrameMethods.call(this, requestId, "executeJavaScript", code, hasUserGesture);
|
||||||
else
|
else
|
||||||
return this.once('did-finish-load', executeJavaScript.bind(this, requestId, code, hasUserGesture));
|
return this.once('did-finish-load', asyncWebFrameMethods.bind(this, requestId, "executeJavaScript", code, hasUserGesture));
|
||||||
};
|
};
|
||||||
|
|
||||||
ipcMain.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE', function(event, id, result) {
|
|
||||||
if (responseCallback[id]) {
|
|
||||||
responseCallback[id].apply(null, [result]);
|
|
||||||
delete responseCallback[id];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Dispatch IPC messages to the ipc module.
|
// Dispatch IPC messages to the ipc module.
|
||||||
webContents.on('ipc-message', function(event, packed) {
|
webContents.on('ipc-message', function(event, packed) {
|
||||||
var args, channel;
|
var args, channel;
|
||||||
|
|
|
@ -36,10 +36,9 @@ electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, m
|
||||||
electron.webFrame[method].apply(electron.webFrame, args);
|
electron.webFrame[method].apply(electron.webFrame, args);
|
||||||
});
|
});
|
||||||
|
|
||||||
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, method, args) => {
|
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => {
|
||||||
let requestId = args.shift();
|
|
||||||
const responseCallback = function(result) {
|
const responseCallback = function(result) {
|
||||||
event.sender.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE', requestId, result);
|
event.sender.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE_' + requestId, result);
|
||||||
};
|
};
|
||||||
args.push(responseCallback);
|
args.push(responseCallback);
|
||||||
electron.webFrame[method].apply(electron.webFrame, args);
|
electron.webFrame[method].apply(electron.webFrame, args);
|
||||||
|
|
|
@ -10,6 +10,7 @@ const screen = require('electron').screen;
|
||||||
|
|
||||||
const app = remote.require('electron').app;
|
const app = remote.require('electron').app;
|
||||||
const ipcMain = remote.require('electron').ipcMain;
|
const ipcMain = remote.require('electron').ipcMain;
|
||||||
|
const ipcRenderer = require('electron').ipcRenderer;
|
||||||
const BrowserWindow = remote.require('electron').BrowserWindow;
|
const BrowserWindow = remote.require('electron').BrowserWindow;
|
||||||
|
|
||||||
const isCI = remote.getGlobal('isCi');
|
const isCI = remote.getGlobal('isCi');
|
||||||
|
@ -690,4 +691,22 @@ describe('browser-window module', function() {
|
||||||
assert.equal(fs.existsSync(serializedPath), false);
|
assert.equal(fs.existsSync(serializedPath), false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('window.webContents.executeJavaScript', function() {
|
||||||
|
var expected = 'hello, world!';
|
||||||
|
var code = '(() => \"' + expected + '\")()';
|
||||||
|
|
||||||
|
it('doesnt throw when no calback is provided', function() {
|
||||||
|
const result = ipcRenderer.sendSync('executeJavaScript', code, false);
|
||||||
|
assert.equal(result, 'success');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns result when calback is provided', function(done) {
|
||||||
|
ipcRenderer.send('executeJavaScript', code, true);
|
||||||
|
ipcRenderer.once('executeJavaScript-response', function(event, result) {
|
||||||
|
assert.equal(result, expected);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -138,4 +138,15 @@ app.on('ready', function() {
|
||||||
});
|
});
|
||||||
event.returnValue = "done";
|
event.returnValue = "done";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ipcMain.on('executeJavaScript', function(event, code, hasCallback) {
|
||||||
|
if (hasCallback) {
|
||||||
|
window.webContents.executeJavaScript(code, (result) => {
|
||||||
|
window.webContents.send('executeJavaScript-response', result);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
window.webContents.executeJavaScript(code);
|
||||||
|
event.returnValue = "success";
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue