electron/lib/renderer/chrome-api.js

193 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-05-28 01:26:41 +00:00
const {ipcRenderer} = require('electron')
2016-06-16 16:51:54 +00:00
const Event = require('./extensions/event')
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 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 || {}
let originResultID = 1
2016-05-28 06:37:44 +00:00
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, resultID) => {
chrome.runtime.onMessage.emit(message, new MessageSender(tabId, extensionId), (messageResult) => {
ipcRenderer.send(`CHROME_RUNTIME_ONMESSAGE_RESULT_${resultID}`, messageResult)
})
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) {
// A case of not provide extension-id: (message, responseCallback)
if (typeof args[1] === 'function') {
ipcRenderer.on(`CHROME_RUNTIME_SENDMESSAGE_RESULT_${originResultID}`, (event, result) => args[1](result))
message = args[0]
} else {
[targetExtensionId, message] = args
}
2016-05-28 12:23:43 +00:00
} else {
console.error('options is not supported')
ipcRenderer.on(`CHROME_RUNTIME_SENDMESSAGE_RESULT_${originResultID}`, (event, result) => args[2](result))
2016-05-28 12:23:43 +00:00
}
ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message, originResultID)
originResultID++
2016-05-28 12:23:43 +00:00
},
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) => {
// Disabled due to false positive in StandardJS
// eslint-disable-next-line standard/no-callback-literal
2016-05-28 07:41:12 +00:00
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) {
ipcRenderer.on(`CHROME_TABS_SEND_MESSAGE_RESULT_${originResultID}`, (event, result) => responseCallback(result))
2016-05-28 12:35:07 +00:00
}
ipcRenderer.send('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, isBackgroundPage, message, originResultID)
2016-11-17 03:56:44 +00:00
originResultID++
},
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
}
2017-07-07 01:13:19 +00:00
chrome.storage = require('./extensions/storage').setup(extensionId)
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-16 16:52:14 +00:00
chrome.i18n = require('./extensions/i18n').setup(extensionId)
chrome.webNavigation = require('./extensions/web-navigation').setup()
2016-05-28 01:26:41 +00:00
}