Make simple runtime.connect work

This commit is contained in:
Cheng Zhao 2016-05-28 10:26:41 +09:00
parent dfe7ae2124
commit e76c36a9a8
2 changed files with 109 additions and 2 deletions

View file

@ -1,4 +1,4 @@
const {app, protocol, webContents, BrowserWindow} = require('electron')
const {app, ipcMain, protocol, webContents, BrowserWindow} = require('electron')
const renderProcessPreferences = process.atomBinding('render_process_preferences').forAllBrowserWindow()
const fs = require('fs')
@ -67,6 +67,7 @@ const startBackgroundPages = function (manifest) {
hostname: manifest.hostname,
pathname: '_generated_background_page.html'
}))
contents.openDevTools()
}
const removeBackgroundPages = function (manifest) {
@ -76,6 +77,28 @@ const removeBackgroundPages = function (manifest) {
delete backgroundPages[manifest.hostname]
}
// Handle the chrome.* API messages.
ipcMain.on('CHROME_RUNTIME_CONNECT', function (event, hostname, connectInfo) {
const page = backgroundPages[hostname]
if (!page) {
console.error(`Connect to unkown extension ${hostname}`)
return
}
event.returnValue = page.webContents.id
page.webContents.sendToAll('CHROME_RUNTIME_ONCONNECT', event.sender.id, hostname, connectInfo)
})
ipcMain.on('CHROME_PORT_POSTMESSAGE', function (event, webContentsId, hostname, message) {
const contents = webContents.fromId(webContentsId)
if (!contents) {
console.error(`Sending message to extension ${hostname} with unkown webContentsId ${webContentsId}`)
return
}
contents.sendToAll(`CHROME_PORT_ONMESSAGE_${hostname}`, message)
})
// Transfer the content scripts to renderer.
const contentScripts = {}