electron/lib/renderer/chrome-api.js

197 lines
6 KiB
JavaScript
Raw Normal View History

'use strict'
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
const Event = require('@electron/internal/renderer/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: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
})
},
getManifest: function () {
const manifest = ipcRendererUtils.invokeSync('CHROME_EXTENSION_MANIFEST', extensionId)
return manifest
},
2016-05-28 06:37:44 +00:00
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
2018-09-13 16:10:51 +00:00
let connectInfo = { name: '' }
2016-05-28 06:37:44 +00:00
if (args.length === 1) {
connectInfo = args[0]
} else if (args.length === 2) {
[targetExtensionId, connectInfo] = args
}
2018-09-13 16:10:51 +00:00
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.once(`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.once(`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, resultCallback) {
if (resultCallback) {
ipcRenderer.once(`CHROME_TABS_EXECUTESCRIPT_RESULT_${originResultID}`, (event, result) => resultCallback([result]))
}
ipcRenderer.send('CHROME_TABS_EXECUTESCRIPT', originResultID, tabId, extensionId, details)
originResultID++
2016-05-28 12:35:07 +00:00
},
sendMessage (tabId, message, options, responseCallback) {
if (responseCallback) {
ipcRenderer.once(`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
}
chrome.storage = require('@electron/internal/renderer/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
chrome.i18n = require('@electron/internal/renderer/extensions/i18n').setup(extensionId)
chrome.webNavigation = require('@electron/internal/renderer/extensions/web-navigation').setup()
2016-05-28 01:26:41 +00:00
}