electron/lib/renderer/chrome-api.js

98 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-05-28 01:26:41 +00:00
const {ipcRenderer} = require('electron')
2016-03-25 19:57:17 +00:00
const url = require('url')
2016-05-28 01:26:41 +00:00
// TODO(zcbenz): Set it to correct value for content scripts.
const currentExtensionId = window.location.hostname
class Event {
constructor () {
this.listeners = []
}
addListener (callback) {
this.listeners.push(callback)
}
emit (...args) {
for (const listener of this.listeners) {
listener(...args)
}
}
}
class OnConnect extends Event {
constructor () {
super()
ipcRenderer.on('CHROME_RUNTIME_ONCONNECT', (event, webContentsId, extensionId, connectInfo) => {
this.emit(new Port(webContentsId, extensionId, connectInfo.name))
})
}
}
class MessageSender {
constructor () {
this.tab = null
this.frameId = null
this.id = null
this.url = null
this.tlsChannelId = null
}
}
class Port {
constructor (webContentsId, extensionId, name) {
this.webContentsId = webContentsId
this.extensionId = extensionId
this.name = name
this.onDisconnect = new Event()
this.onMessage = new Event()
this.sender = new MessageSender()
ipcRenderer.on(`CHROME_PORT_ONMESSAGE_${extensionId}`, (event, message) => {
this.onMessage.emit(message, new MessageSender(), function () {})
})
}
disconnect () {
}
postMessage (message) {
ipcRenderer.send('CHROME_PORT_POSTMESSAGE', this.webContentsId, this.extensionId, message)
}
}
2016-03-25 19:57:17 +00:00
const chrome = window.chrome = window.chrome || {}
2016-01-12 02:40:23 +00:00
chrome.extension = {
2016-03-25 19:57:17 +00:00
getURL: function (path) {
2016-01-12 02:40:23 +00:00
return url.format({
protocol: window.location.protocol,
2016-01-12 02:40:23 +00:00
slashes: true,
2016-05-28 01:26:41 +00:00
hostname: currentExtensionId,
2016-01-12 02:40:23 +00:00
pathname: path
2016-03-25 19:57:17 +00:00
})
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
}
2016-05-28 01:26:41 +00:00
chrome.runtime = {
getURL: chrome.extension.getURL,
onConnect: new OnConnect(),
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 = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', extensionId, connectInfo)
return new Port(webContentsId, extensionId, connectInfo.name)
}
}