Closed petrfelzmann closed 9 years ago
Thanks, it because current implementation sends messages only to BrowserWindows but not to webviews... (https://github.com/seanchas116/electron-safe-ipc/blob/master/main.js#L30)
Maybe we need another mode to communicate between renderer processes and webviews.
What that 'mode' could be? I do not have any idea right now...
Requiring this code from renderer instead of remote.require('electron-safe-ipc/main')
works:
"use strict";
var EventEmitter = require("events").EventEmitter;
var remote = require("remote");
var protocol = remote.require("protocol");
var arraySlice = Array.prototype.slice;
var url = require("url");
var qs = require("querystring");
var ipc = new EventEmitter();
protocol.registerProtocol("electron-safe-ipc", function (request) {
// nextTick workaround to prevent crash on exception
process.nextTick(function () {
var urlContents = url.parse(request.url);
var queries = qs.parse(urlContents.query);
var channel = queries.channel;
var args = JSON.parse(queries.argsJson);
ipc.emit.apply(ipc, [channel].concat(args));
});
return new protocol.RequestStringJob({data: ""});
});
ipc.send = function() {
var channel = arguments[0];
var args = arraySlice.call(arguments, 1);
[].forEach.call(document.querySelectorAll("webview"), function (webview) {
var script = "window.__electronSafeIpc && window.__electronSafeIpc("
+ JSON.stringify(channel) + ","
+ JSON.stringify(JSON.stringify(args))
+ ");";
webview.executeJavaScript(script);
});
};
module.exports = ipc;
I'll try implementing this in electron-safe-ipc.
Excellent, thank you! :+1: :star: :clap:
I released 0.5.0 with webview support (https://github.com/seanchas116/electron-safe-ipc#communicate-between-renderer-process-and-webview). Could you check this out?
:ok_hand: It works, superb! Thx!
Thanks for reporting, too!
Thank you for your code, it is exactly what I need! Particularly the case with node disabled
webview
. But unfortunately I cannot receive the message in thewebview
.The main window loads this web page:
where
webview.html
contains the message subscription:but the message 'foobar' is never displayed in the console.