Implement chrome.tabs.sendMessage

This commit is contained in:
Cheng Zhao 2016-05-28 21:35:07 +09:00
parent 62fb4f9820
commit 5eb9ed1729
2 changed files with 19 additions and 24 deletions

View file

@ -83,7 +83,7 @@ ipcMain.on('CHROME_RUNTIME_CONNECT', function (event, extensionId, connectInfo)
if (page.webContents.isDestroyed()) return if (page.webContents.isDestroyed()) return
page.webContents.sendToAll(`CHROME_PORT_DISCONNECT_${portId}`) 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) { ipcMain.on('CHROME_RUNTIME_SENDMESSAGE', function (event, extensionId, message) {
@ -93,7 +93,7 @@ ipcMain.on('CHROME_RUNTIME_SENDMESSAGE', function (event, extensionId, message)
return 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) { ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, webContentsId, extensionId, details) {

View file

@ -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| // Inject chrome API to the |context|
exports.injectTo = function (extensionId, context) { exports.injectTo = function (extensionId, context) {
const chrome = context.chrome = context.chrome || {} 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 = { chrome.runtime = {
getURL: function (path) { getURL: function (path) {
return url.format({ return url.format({
@ -112,7 +100,7 @@ exports.injectTo = function (extensionId, context) {
}) })
}, },
onConnect: new OnConnect(), onConnect: new Event(),
connect (...args) { connect (...args) {
// Parse the optional args. // Parse the optional args.
@ -144,7 +132,7 @@ exports.injectTo = function (extensionId, context) {
ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message) ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message)
}, },
onMessage: new OnMessage() onMessage: new Event()
} }
chrome.tabs = { chrome.tabs = {
@ -154,6 +142,13 @@ exports.injectTo = function (extensionId, context) {
callback([event.result]) callback([event.result])
}) })
ipcRenderer.send('CHROME_TABS_EXECUTESCRIPT', requestId, tabId, extensionId, details) 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)
} }
} }