This commit is contained in:
Robo 2016-02-24 15:41:09 +05:30
parent a734326907
commit 8386baf267
5 changed files with 44 additions and 28 deletions

View file

@ -1,7 +1,3 @@
const EventEmitter = require('events').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);

View file

@ -67,10 +67,6 @@ const webFrameMethods = [
'setZoomLevelLimits'
];
const asyncWebFrameMethods = [
'executeJavaScript',
];
let wrapWebContents = function(webContents) {
// webContents is an EventEmitter.
var controller, method, name, ref1;
@ -112,37 +108,32 @@ let wrapWebContents = function(webContents) {
};
}
for (let method of asyncWebFrameMethods) {
webContents[method] = function() {
let args = Array.prototype.slice.call(arguments);
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', method, args);
};
const asyncWebFrameMethods = function(requestId, method, ...args) {
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args);
ipcMain.once('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE_' + requestId, function(event, result) {
if (responseCallback[requestId]) {
responseCallback[requestId](result);
delete responseCallback[requestId];
}
});
};
// Make sure webContents.executeJavaScript would run the code only when the
// webContents has been loaded.
const executeJavaScript = webContents.executeJavaScript;
webContents.executeJavaScript = function(code, hasUserGesture, callback) {
let requestId = getNextId();
if (typeof hasUserGesture === "function") {
callback = hasUserGesture;
hasUserGesture = false;
}
if (callback !== null)
if (callback != null)
responseCallback[requestId] = callback;
if (this.getURL() && !this.isLoading())
return executeJavaScript.call(this, requestId, code, hasUserGesture);
return asyncWebFrameMethods.call(this, requestId, "executeJavaScript", code, hasUserGesture);
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.
webContents.on('ipc-message', function(event, packed) {
var args, channel;

View file

@ -36,10 +36,9 @@ electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, m
electron.webFrame[method].apply(electron.webFrame, args);
});
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, method, args) => {
let requestId = args.shift();
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => {
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);
electron.webFrame[method].apply(electron.webFrame, args);

View file

@ -10,6 +10,7 @@ const screen = require('electron').screen;
const app = remote.require('electron').app;
const ipcMain = remote.require('electron').ipcMain;
const ipcRenderer = require('electron').ipcRenderer;
const BrowserWindow = remote.require('electron').BrowserWindow;
const isCI = remote.getGlobal('isCi');
@ -690,4 +691,22 @@ describe('browser-window module', function() {
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();
});
});
});
});

View file

@ -138,4 +138,15 @@ app.on('ready', function() {
});
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";
}
});
});