Inject chrome.* to content scripts
This commit is contained in:
parent
d8db695712
commit
f5b430d9e1
4 changed files with 56 additions and 33 deletions
|
@ -1,9 +1,6 @@
|
|||
const {ipcRenderer} = require('electron')
|
||||
const url = require('url')
|
||||
|
||||
// TODO(zcbenz): Set it to correct value for content scripts.
|
||||
const currentExtensionId = window.location.hostname
|
||||
|
||||
class Event {
|
||||
constructor () {
|
||||
this.listeners = []
|
||||
|
@ -13,6 +10,13 @@ class Event {
|
|||
this.listeners.push(callback)
|
||||
}
|
||||
|
||||
removeListener (callback) {
|
||||
const index = this.listeners.indexOf(callback)
|
||||
if (index !== -1) {
|
||||
this.listeners.splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
emit (...args) {
|
||||
for (const listener of this.listeners) {
|
||||
listener(...args)
|
||||
|
@ -67,41 +71,44 @@ class Port {
|
|||
ipcRenderer.send('CHROME_PORT_POSTMESSAGE', this.webContentsId, this.portId, message)
|
||||
}
|
||||
|
||||
_onDisconnect() {
|
||||
_onDisconnect () {
|
||||
ipcRenderer.removeAllListeners(`CHROME_PORT_ONMESSAGE_${this.portId}`)
|
||||
this.onDisconnect.emit()
|
||||
}
|
||||
}
|
||||
|
||||
const chrome = window.chrome = window.chrome || {}
|
||||
// Inject chrome API to the |context|
|
||||
exports.injectTo = function (extensionId, context) {
|
||||
const chrome = context.chrome = context.chrome || {}
|
||||
|
||||
chrome.extension = {
|
||||
getURL: function (path) {
|
||||
return url.format({
|
||||
protocol: window.location.protocol,
|
||||
slashes: true,
|
||||
hostname: currentExtensionId,
|
||||
pathname: path
|
||||
})
|
||||
}
|
||||
}
|
||||
chrome.runtime = {
|
||||
getURL: function (path) {
|
||||
return url.format({
|
||||
protocol: 'chrome-extension',
|
||||
slashes: true,
|
||||
hostname: extensionId,
|
||||
pathname: path
|
||||
})
|
||||
},
|
||||
|
||||
chrome.runtime = {
|
||||
getURL: chrome.extension.getURL,
|
||||
onConnect: new OnConnect(),
|
||||
|
||||
onConnect: new OnConnect(),
|
||||
connect (...args) {
|
||||
// Parse the optional args.
|
||||
let targetExtensionId = extensionId
|
||||
let connectInfo = {name: ''}
|
||||
if (args.length === 1) {
|
||||
connectInfo = args[0]
|
||||
} else if (args.length === 2) {
|
||||
[targetExtensionId, connectInfo] = args
|
||||
}
|
||||
|
||||
connect (...args) {
|
||||
// Parse the optional args.
|
||||
let extensionId = currentExtensionId
|
||||
let connectInfo = {name: ''}
|
||||
if (args.length === 1) {
|
||||
connectInfo = args[0]
|
||||
} else if (args.length === 2) {
|
||||
[extensionId, connectInfo] = args
|
||||
const {webContentsId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', targetExtensionId, connectInfo)
|
||||
return new Port(webContentsId, portId, connectInfo.name)
|
||||
}
|
||||
}
|
||||
|
||||
const {webContentsId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', extensionId, connectInfo)
|
||||
return new Port(webContentsId, portId, connectInfo.name)
|
||||
chrome.extension = {
|
||||
getURL: chrome.runtime.getURL
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue