Implement chrome.tabs.sendMessage
This commit is contained in:
parent
62fb4f9820
commit
5eb9ed1729
2 changed files with 19 additions and 24 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue