Current handle sender.tab for background pages

This commit is contained in:
Cheng Zhao 2016-05-29 10:46:48 +09:00
parent dd804d7432
commit 2431d886bf
4 changed files with 51 additions and 24 deletions

View file

@ -49,7 +49,9 @@ const startBackgroundPages = function (manifest) {
}).join('')
const html = new Buffer(`<html><body>${scripts}</body></html>`)
const contents = webContents.create({})
const contents = webContents.create({
commandLineSwitches: ['--background-page']
})
backgroundPages[manifest.extensionId] = { html: html, webContents: contents }
contents.loadURL(url.format({
protocol: 'chrome-extension',
@ -77,7 +79,7 @@ ipcMain.on('CHROME_RUNTIME_CONNECT', function (event, extensionId, connectInfo)
}
const portId = ++nextId
event.returnValue = {webContentsId: page.webContents.id, portId: portId}
event.returnValue = {tabId: page.webContents.id, portId: portId}
event.sender.once('render-view-deleted', () => {
if (page.webContents.isDestroyed()) return
@ -96,10 +98,22 @@ ipcMain.on('CHROME_RUNTIME_SENDMESSAGE', function (event, extensionId, message)
page.webContents.sendToAll(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, event.sender.id, message)
})
ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, webContentsId, extensionId, details) {
const contents = webContents.fromId(webContentsId)
ipcMain.on('CHROME_TABS_SEND_MESSAGE', function (event, tabId, extensionId, isBackgroundPage, message) {
const contents = webContents.fromId(tabId)
if (!contents) {
console.error(`Sending message to unkown webContentsId ${webContentsId}`)
console.error(`Sending message to unkown tab ${tabId}`)
return
}
const senderTabId = isBackgroundPage ? null : event.sender.id
contents.sendToAll(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, senderTabId, message)
})
ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, tabId, extensionId, details) {
const contents = webContents.fromId(tabId)
if (!contents) {
console.error(`Sending message to unkown tab ${tabId}`)
return
}