electron/lib/renderer/chrome-api.js

185 lines
4.4 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
this.disconnected = false
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
ipcRenderer.once(`CHROME_PORT_DISCONNECT_${portId}`, () => {
2016-05-28 03:07:08 +00:00
this._onDisconnect()
})
ipcRenderer.on(`CHROME_PORT_POSTMESSAGE_${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 () {
if (this.disconnected) return
ipcRenderer.sendToAll(this.webContentsId, `CHROME_PORT_DISCONNECT_${this.portId}`)
2016-05-28 03:07:08 +00:00
this._onDisconnect()
2016-05-28 01:26:41 +00:00
}
postMessage (message) {
ipcRenderer.sendToAll(this.webContentsId, `CHROME_PORT_POSTMESSAGE_${this.portId}`, message)
2016-05-28 03:07:08 +00:00
}
2016-05-28 06:37:44 +00:00
_onDisconnect () {
this.disconnected = true
ipcRenderer.removeAllListeners(`CHROME_PORT_POSTMESSAGE_${this.portId}`)
2016-05-28 03:07:08 +00:00
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 12:23:43 +00:00
class OnMessage extends Event {
constructor () {
super()
ipcRenderer.on('CHROME_RUNTIME_ONMESSAGE', (event, message) => {
this.emit(message)
})
}
}
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 12:23:43 +00:00
},
sendMessage (...args) {
// Parse the optional args.
let targetExtensionId = extensionId
let message, options, responseCallback
if (args.length === 1) {
message = args[0]
} else if (args.length === 2) {
[targetExtensionId, message] = args
} else {
console.error('responseCallback is not supported')
[targetExtensionId, message, options, responseCallback] = args
}
ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message)
},
onMessage: new OnMessage()
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 = {
2016-05-18 12:19:50 +00:00
getURL: chrome.runtime.getURL,
connect: chrome.runtime.connect,
2016-05-28 12:23:43 +00:00
onConnect: chrome.runtime.onConnect,
sendMessage: chrome.runtime.sendMessage,
onMessage: chrome.runtime.onMessage
2016-05-18 12:19:50 +00:00
}
chrome.storage = {
sync: {
get () {},
set () {}
}
}
chrome.pageAction = {
show () {},
hide () {},
setTitle () {},
getTitle () {},
setIcon () {},
setPopup () {},
getPopup () {}
2016-05-28 01:26:41 +00:00
}
}