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
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -13,7 +13,7 @@ const matchesPattern = function (pattern) {
|
|||
// Run the code with chrome API integrated.
|
||||
const runContentScript = function (extensionId, url, code) {
|
||||
const context = {}
|
||||
require('./chrome-api').injectTo(extensionId, context)
|
||||
require('./chrome-api').injectTo(extensionId, false, context)
|
||||
const wrapper = `(function (chrome) {\n ${code}\n })`
|
||||
const compiledWrapper = runInThisContext(wrapper, {
|
||||
filename: url,
|
||||
|
|
|
@ -41,8 +41,9 @@ electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (ev
|
|||
})
|
||||
|
||||
// Process command line arguments.
|
||||
var nodeIntegration = 'false'
|
||||
var preloadScript = null
|
||||
let nodeIntegration = 'false'
|
||||
let preloadScript = null
|
||||
let isBackgroundPage = false
|
||||
for (let arg of process.argv) {
|
||||
if (arg.indexOf('--guest-instance-id=') === 0) {
|
||||
// This is a guest web view.
|
||||
|
@ -54,6 +55,8 @@ for (let arg of process.argv) {
|
|||
nodeIntegration = arg.substr(arg.indexOf('=') + 1)
|
||||
} else if (arg.indexOf('--preload=') === 0) {
|
||||
preloadScript = arg.substr(arg.indexOf('=') + 1)
|
||||
} else if (arg === '--background-page') {
|
||||
isBackgroundPage = true
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,7 +66,7 @@ if (window.location.protocol === 'chrome-devtools:') {
|
|||
nodeIntegration = 'true'
|
||||
} else if (window.location.protocol === 'chrome-extension:') {
|
||||
// Add implementations of chrome API.
|
||||
require('./chrome-api').injectTo(window.location.hostname, window)
|
||||
require('./chrome-api').injectTo(window.location.hostname, isBackgroundPage, window)
|
||||
nodeIntegration = 'false'
|
||||
} else {
|
||||
// Override default web functions.
|
||||
|
|
Loading…
Reference in a new issue