Current handle sender.tab for background pages
This commit is contained in:
parent
dd804d7432
commit
2431d886bf
4 changed files with 51 additions and 24 deletions
|
@ -27,29 +27,29 @@ class Event {
|
|||
}
|
||||
|
||||
class Tab {
|
||||
constructor (webContentsId) {
|
||||
this.id = webContentsId
|
||||
constructor (tabId) {
|
||||
this.id = tabId
|
||||
}
|
||||
}
|
||||
|
||||
class MessageSender {
|
||||
constructor (webContentsId, extensionId) {
|
||||
this.tab = new Tab(webContentsId)
|
||||
constructor (tabId, extensionId) {
|
||||
this.tab = tabId ? new Tab(tabId) : null
|
||||
this.id = extensionId
|
||||
this.url = `chrome-extension://${extensionId}`
|
||||
}
|
||||
}
|
||||
|
||||
class Port {
|
||||
constructor (webContentsId, portId, extensionId, name) {
|
||||
this.webContentsId = webContentsId
|
||||
constructor (tabId, portId, extensionId, name) {
|
||||
this.tabId = tabId
|
||||
this.portId = portId
|
||||
this.disconnected = false
|
||||
|
||||
this.name = name
|
||||
this.onDisconnect = new Event()
|
||||
this.onMessage = new Event()
|
||||
this.sender = new MessageSender(webContentsId, extensionId)
|
||||
this.sender = new MessageSender(tabId, extensionId)
|
||||
|
||||
ipcRenderer.once(`CHROME_PORT_DISCONNECT_${portId}`, () => {
|
||||
this._onDisconnect()
|
||||
|
@ -63,12 +63,12 @@ class Port {
|
|||
disconnect () {
|
||||
if (this.disconnected) return
|
||||
|
||||
ipcRenderer.sendToAll(this.webContentsId, `CHROME_PORT_DISCONNECT_${this.portId}`)
|
||||
ipcRenderer.sendToAll(this.tabId, `CHROME_PORT_DISCONNECT_${this.portId}`)
|
||||
this._onDisconnect()
|
||||
}
|
||||
|
||||
postMessage (message) {
|
||||
ipcRenderer.sendToAll(this.webContentsId, `CHROME_PORT_POSTMESSAGE_${this.portId}`, message)
|
||||
ipcRenderer.sendToAll(this.tabId, `CHROME_PORT_POSTMESSAGE_${this.portId}`, message)
|
||||
}
|
||||
|
||||
_onDisconnect () {
|
||||
|
@ -79,11 +79,11 @@ class Port {
|
|||
}
|
||||
|
||||
// Inject chrome API to the |context|
|
||||
exports.injectTo = function (extensionId, context) {
|
||||
exports.injectTo = function (extensionId, isBackgroundPage, 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_ONCONNECT_${extensionId}`, (event, tabId, portId, connectInfo) => {
|
||||
chrome.runtime.onConnect.emit(new Port(tabId, portId, extensionId, connectInfo.name))
|
||||
})
|
||||
|
||||
ipcRenderer.on(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, (event, tabId, message) => {
|
||||
|
@ -103,6 +103,11 @@ exports.injectTo = function (extensionId, context) {
|
|||
onConnect: new Event(),
|
||||
|
||||
connect (...args) {
|
||||
if (isBackgroundPage) {
|
||||
console.error('chrome.runtime.connect is not supported in background page')
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the optional args.
|
||||
let targetExtensionId = extensionId
|
||||
let connectInfo = {name: ''}
|
||||
|
@ -112,11 +117,16 @@ exports.injectTo = function (extensionId, context) {
|
|||
[targetExtensionId, connectInfo] = args
|
||||
}
|
||||
|
||||
const {webContentsId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', targetExtensionId, connectInfo)
|
||||
return new Port(webContentsId, portId, extensionId, connectInfo.name)
|
||||
const {tabId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', targetExtensionId, connectInfo)
|
||||
return new Port(tabId, portId, extensionId, connectInfo.name)
|
||||
},
|
||||
|
||||
sendMessage (...args) {
|
||||
if (isBackgroundPage) {
|
||||
console.error('chrome.runtime.sendMessage is not supported in background page')
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the optional args.
|
||||
let targetExtensionId = extensionId
|
||||
let message
|
||||
|
@ -147,7 +157,7 @@ exports.injectTo = function (extensionId, context) {
|
|||
if (responseCallback) {
|
||||
console.error('responseCallback is not supported')
|
||||
}
|
||||
ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', extensionId, message)
|
||||
ipcRenderer.send(`CHROME_TABS_SEND_MESSAGE`, tabId, extensionId, isBackgroundPage, message)
|
||||
},
|
||||
|
||||
onUpdated: new Event()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue