electron/lib/renderer/chrome-api.js

200 lines
5.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 (tabId) {
this.id = tabId
2016-05-28 01:26:41 +00:00
}
}
class MessageSender {
constructor (tabId, extensionId) {
this.tab = tabId ? new Tab(tabId) : null
2016-05-28 07:01:16 +00:00
this.id = extensionId
this.url = `chrome-extension://${extensionId}`
2016-05-28 01:26:41 +00:00
}
}
class Port {
constructor (tabId, portId, extensionId, name) {
this.tabId = tabId
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()
this.sender = new MessageSender(tabId, 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.tabId, `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.tabId, `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 06:37:44 +00:00
// Inject chrome API to the |context|
exports.injectTo = function (extensionId, isBackgroundPage, context) {
2016-05-28 06:37:44 +00:00
const chrome = context.chrome = context.chrome || {}
ipcRenderer.on(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, (event, tabId, portId, connectInfo) => {
chrome.runtime.onConnect.emit(new Port(tabId, portId, extensionId, connectInfo.name))
2016-05-28 12:35:07 +00:00
})
ipcRenderer.on(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, (event, tabId, message) => {
chrome.runtime.onMessage.emit(message, new MessageSender(tabId, extensionId))
2016-05-28 12:35:07 +00:00
})
ipcRenderer.on('CHROME_TABS_ONCREATED', (event, tabId) => {
chrome.tabs.onCreated.emit(new Tab(tabId))
})
ipcRenderer.on('CHROME_TABS_ONREMOVED', (event, tabId) => {
chrome.tabs.onRemoved.emit(tabId)
})
2016-05-28 06:37:44 +00:00
chrome.runtime = {
2016-06-06 21:42:07 +00:00
id: extensionId,
2016-05-28 06:37:44 +00:00
getURL: function (path) {
return url.format({
protocol: 'chrome-extension',
slashes: true,
hostname: extensionId,
pathname: path
})
},
connect (...args) {
if (isBackgroundPage) {
console.error('chrome.runtime.connect is not supported in background page')
return
}
2016-05-28 06:37:44 +00:00
// 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 {tabId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', targetExtensionId, connectInfo)
return new Port(tabId, portId, extensionId, connectInfo.name)
2016-05-28 12:23:43 +00:00
},
sendMessage (...args) {
if (isBackgroundPage) {
console.error('chrome.runtime.sendMessage is not supported in background page')
return
}
2016-05-28 12:23:43 +00:00
// Parse the optional args.
let targetExtensionId = extensionId
2016-05-28 13:46:15 +00:00
let message
2016-05-28 12:23:43 +00:00
if (args.length === 1) {
message = args[0]
} else if (args.length === 2) {
[targetExtensionId, message] = args
} else {
2016-05-28 13:46:15 +00:00
console.error('options and responseCallback are not supported')
2016-05-28 12:23:43 +00:00
}
ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message)
},
onConnect: new Event(),
onMessage: new Event(),
onInstalled: new Event()
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 12:35:07 +00:00
},
sendMessage (tabId, message, options, responseCallback) {
if (responseCallback) {
console.error('responseCallback is not supported')
}
2016-05-29 02:57:20 +00:00
ipcRenderer.send('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, isBackgroundPage, message)
},
onUpdated: new Event(),
onCreated: new Event(),
onRemoved: new Event()
2016-05-28 07:41:12 +00:00
}
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
}
2016-06-09 21:14:14 +00:00
chrome.storage = require('./extensions/storage.js')
2016-05-18 12:19:50 +00:00
chrome.pageAction = {
show () {},
hide () {},
setTitle () {},
getTitle () {},
setIcon () {},
setPopup () {},
getPopup () {}
2016-05-28 01:26:41 +00:00
}
2016-06-07 22:34:17 +00:00
2016-06-08 00:00:53 +00:00
chrome.i18n = require('./extensions/i18n.js').setup(extensionId)
2016-05-28 01:26:41 +00:00
}