Implement chrome.tabs.sendMessage

This commit is contained in:
Cheng Zhao 2016-05-28 21:35:07 +09:00
Родитель 62fb4f9820
Коммит 5eb9ed1729
2 изменённых файлов: 19 добавлений и 24 удалений

Просмотреть файл

@ -83,7 +83,7 @@ ipcMain.on('CHROME_RUNTIME_CONNECT', function (event, extensionId, connectInfo)
if (page.webContents.isDestroyed()) return
page.webContents.sendToAll(`CHROME_PORT_DISCONNECT_${portId}`)
})
page.webContents.sendToAll('CHROME_RUNTIME_ONCONNECT', event.sender.id, portId, extensionId, connectInfo)
page.webContents.sendToAll(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, event.sender.id, portId, connectInfo)
})
ipcMain.on('CHROME_RUNTIME_SENDMESSAGE', function (event, extensionId, message) {
@ -93,7 +93,7 @@ ipcMain.on('CHROME_RUNTIME_SENDMESSAGE', function (event, extensionId, message)
return
}
page.webContents.sendToAll('CHROME_RUNTIME_ONMESSAGE', message)
page.webContents.sendToAll(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, message)
})
ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, webContentsId, extensionId, details) {

Просмотреть файл

@ -78,30 +78,18 @@ class Port {
}
}
class OnConnect extends Event {
constructor () {
super()
ipcRenderer.on('CHROME_RUNTIME_ONCONNECT', (event, webContentsId, portId, extensionId, connectInfo) => {
this.emit(new Port(webContentsId, portId, extensionId, connectInfo.name))
})
}
}
class OnMessage extends Event {
constructor () {
super()
ipcRenderer.on('CHROME_RUNTIME_ONMESSAGE', (event, message) => {
this.emit(message)
})
}
}
// Inject chrome API to the |context|
exports.injectTo = function (extensionId, context) {
const chrome = context.chrome = context.chrome || {}
ipcRenderer.on(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, (event, webContentsId, portId, connectInfo) => {
chrome.runtime.onConnect.emit(new Port(webContentsId, portId, extensionId, connectInfo.name))
})
ipcRenderer.on(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, (event, message) => {
chrome.runtime.onMessage.emit(message)
})
chrome.runtime = {
getURL: function (path) {
return url.format({
@ -112,7 +100,7 @@ exports.injectTo = function (extensionId, context) {
})
},
onConnect: new OnConnect(),
onConnect: new Event(),
connect (...args) {
// Parse the optional args.
@ -144,7 +132,7 @@ exports.injectTo = function (extensionId, context) {
ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message)
},
onMessage: new OnMessage()
onMessage: new Event()
}
chrome.tabs = {
@ -154,6 +142,13 @@ exports.injectTo = function (extensionId, context) {
callback([event.result])
})
ipcRenderer.send('CHROME_TABS_EXECUTESCRIPT', requestId, tabId, extensionId, details)
},
sendMessage (tabId, message, options, responseCallback) {
if (responseCallback) {
console.error('responseCallback is not supported')
}
ipcRenderer.sendToAll(tabId, `CHROME_RUNTIME_ONMESSAGE_${extensionId}`, message)
}
}