electron/lib/renderer/chrome-api.js

132 lines
3.2 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
2016-05-28 07:41:12 +00:00
let nextId = 0
2016-05-28 01:26:41 +00:00
class Event {
constructor () {
this.listeners = []
}
addListener (callback) {
this.listeners.push(callback)
}
2016-05-28 06:37:44 +00:00
removeListener (callback) {
const index = this.listeners.indexOf(callback)
if (index !== -1) {
this.listeners.splice(index, 1)
}
}
2016-05-28 01:26:41 +00:00
emit (...args) {
for (const listener of this.listeners) {
listener(...args)
}
}
}
2016-05-28 07:01:16 +00:00
class Tab {
constructor (webContentsId) {
this.id = webContentsId
2016-05-28 01:26:41 +00:00
}
}
class MessageSender {
2016-05-28 07:01:16 +00:00
constructor (webContentsId, extensionId) {
this.tab = new Tab(webContentsId)
this.id = extensionId
this.url = `chrome-extension://${extensionId}`
2016-05-28 01:26:41 +00:00
}
}
class Port {
2016-05-28 07:01:16 +00:00
constructor (webContentsId, portId, extensionId, name) {
2016-05-28 01:26:41 +00:00
this.webContentsId = webContentsId
2016-05-28 03:07:08 +00:00
this.portId = portId
2016-05-28 01:26:41 +00:00
this.name = name
this.onDisconnect = new Event()
this.onMessage = new Event()
2016-05-28 07:01:16 +00:00
this.sender = new MessageSender(webContentsId, extensionId)
2016-05-28 01:26:41 +00:00
2016-05-28 03:07:08 +00:00
ipcRenderer.once(`CHROME_PORT_ONDISCONNECT_${portId}`, () => {
this._onDisconnect()
})
ipcRenderer.on(`CHROME_PORT_ONMESSAGE_${portId}`, (event, message) => {
2016-05-28 07:01:16 +00:00
const sendResponse = function () { console.error('sendResponse is not implemented') }
this.onMessage.emit(message, this.sender, sendResponse)
2016-05-28 01:26:41 +00:00
})
}
disconnect () {
2016-05-28 03:07:08 +00:00
ipcRenderer.send('CHROME_PORT_DISCONNECT', this.webContentsId, this.portId)
this._onDisconnect()
2016-05-28 01:26:41 +00:00
}
postMessage (message) {
2016-05-28 03:07:08 +00:00
ipcRenderer.send('CHROME_PORT_POSTMESSAGE', this.webContentsId, this.portId, message)
}
2016-05-28 06:37:44 +00:00
_onDisconnect () {
2016-05-28 03:07:08 +00:00
ipcRenderer.removeAllListeners(`CHROME_PORT_ONMESSAGE_${this.portId}`)
this.onDisconnect.emit()
2016-05-28 01:26:41 +00:00
}
}
2016-05-28 07:01:16 +00:00
class OnConnect extends Event {
constructor () {
super()
ipcRenderer.on('CHROME_RUNTIME_ONCONNECT', (event, webContentsId, portId, extensionId, connectInfo) => {
this.emit(new Port(webContentsId, portId, extensionId, connectInfo.name))
})
}
}
2016-05-28 06:37:44 +00:00
// Inject chrome API to the |context|
exports.injectTo = function (extensionId, context) {
const chrome = context.chrome = context.chrome || {}
chrome.runtime = {
getURL: function (path) {
return url.format({
protocol: 'chrome-extension',
slashes: true,
hostname: extensionId,
pathname: path
})
},
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
}
const {webContentsId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', targetExtensionId, connectInfo)
2016-05-28 07:01:16 +00:00
return new Port(webContentsId, portId, extensionId, connectInfo.name)
2016-05-28 01:26:41 +00:00
}
2016-05-28 06:37:44 +00:00
}
2016-05-28 01:26:41 +00:00
2016-05-28 07:41:12 +00:00
chrome.tabs = {
executeScript (tabId, details, callback) {
const requestId = ++nextId
ipcRenderer.once(`CHROME_TABS_EXECUTESCRIPT_RESULT_${requestId}`, (event, result) => {
callback([event.result])
})
ipcRenderer.send('CHROME_TABS_EXECUTESCRIPT', requestId, tabId, extensionId, details)
}
}
2016-05-28 06:37:44 +00:00
chrome.extension = {
getURL: chrome.runtime.getURL
2016-05-28 01:26:41 +00:00
}
}