2018-09-22 12:28:50 +00:00
|
|
|
'use strict'
|
|
|
|
|
2018-10-06 11:48:00 +00:00
|
|
|
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
|
2018-09-20 03:43:26 +00:00
|
|
|
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:41:12 +00:00
|
|
|
let nextId = 0
|
|
|
|
|
2016-05-28 07:01:16 +00:00
|
|
|
class Tab {
|
2016-05-29 01:46:48 +00:00
|
|
|
constructor (tabId) {
|
|
|
|
this.id = tabId
|
2016-05-28 01:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MessageSender {
|
2016-05-29 01:46:48 +00:00
|
|
|
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 {
|
2016-05-29 01:46:48 +00:00
|
|
|
constructor (tabId, portId, extensionId, name) {
|
|
|
|
this.tabId = tabId
|
2016-05-28 03:07:08 +00:00
|
|
|
this.portId = portId
|
2016-05-28 08:51:49 +00:00
|
|
|
this.disconnected = false
|
2016-05-28 01:26:41 +00:00
|
|
|
|
|
|
|
this.name = name
|
|
|
|
this.onDisconnect = new Event()
|
|
|
|
this.onMessage = new Event()
|
2016-05-29 01:46:48 +00:00
|
|
|
this.sender = new MessageSender(tabId, extensionId)
|
2016-05-28 01:26:41 +00:00
|
|
|
|
2016-05-28 12:13:00 +00:00
|
|
|
ipcRenderer.once(`CHROME_PORT_DISCONNECT_${portId}`, () => {
|
2016-05-28 03:07:08 +00:00
|
|
|
this._onDisconnect()
|
|
|
|
})
|
2016-05-28 12:13:00 +00:00
|
|
|
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 () {
|
2016-05-28 08:51:49 +00:00
|
|
|
if (this.disconnected) return
|
|
|
|
|
2018-10-06 11:48:00 +00:00
|
|
|
ipcRenderer._sendInternalToAll(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) {
|
2018-10-06 11:48:00 +00:00
|
|
|
ipcRenderer._sendInternalToAll(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 () {
|
2016-05-28 08:51:49 +00:00
|
|
|
this.disconnected = true
|
2016-05-28 12:13:00 +00:00
|
|
|
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|
|
2016-05-29 01:46:48 +00:00
|
|
|
exports.injectTo = function (extensionId, isBackgroundPage, context) {
|
2016-05-28 06:37:44 +00:00
|
|
|
const chrome = context.chrome = context.chrome || {}
|
2016-11-15 10:30:40 +00:00
|
|
|
let originResultID = 1
|
2016-05-28 06:37:44 +00:00
|
|
|
|
2016-05-29 01:46:48 +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
|
|
|
})
|
|
|
|
|
2016-11-15 10:30:40 +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
|
|
|
})
|
|
|
|
|
2016-05-29 02:50:14 +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) {
|
2016-05-29 01:46:48 +00:00
|
|
|
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)
|
2016-05-29 01:46:48 +00:00
|
|
|
return new Port(tabId, portId, extensionId, connectInfo.name)
|
2016-05-28 12:23:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sendMessage (...args) {
|
2016-05-29 01:46:48 +00:00
|
|
|
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) {
|
2016-06-22 11:47:07 +00:00
|
|
|
// A case of not provide extension-id: (message, responseCallback)
|
|
|
|
if (typeof args[1] === 'function') {
|
2016-11-15 10:30:40 +00:00
|
|
|
ipcRenderer.on(`CHROME_RUNTIME_SENDMESSAGE_RESULT_${originResultID}`, (event, result) => args[1](result))
|
2016-06-22 11:47:07 +00:00
|
|
|
message = args[0]
|
|
|
|
} else {
|
|
|
|
[targetExtensionId, message] = args
|
|
|
|
}
|
2016-05-28 12:23:43 +00:00
|
|
|
} else {
|
2016-11-15 10:30:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-15 10:30:40 +00:00
|
|
|
ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message, originResultID)
|
|
|
|
originResultID++
|
2016-05-28 12:23:43 +00:00
|
|
|
},
|
|
|
|
|
2016-05-29 02:50:14 +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) => {
|
2017-11-23 21:53:03 +00:00
|
|
|
// 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) {
|
2016-11-15 10:30:40 +00:00
|
|
|
ipcRenderer.on(`CHROME_TABS_SEND_MESSAGE_RESULT_${originResultID}`, (event, result) => responseCallback(result))
|
2016-05-28 12:35:07 +00:00
|
|
|
}
|
2016-11-15 10:30:40 +00:00
|
|
|
ipcRenderer.send('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, isBackgroundPage, message, originResultID)
|
2016-11-17 03:56:44 +00:00
|
|
|
originResultID++
|
2016-05-28 12:45:23 +00:00
|
|
|
},
|
|
|
|
|
2016-05-29 02:50:14 +00:00
|
|
|
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,
|
2016-05-28 12:13:00 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-20 03:43:26 +00:00
|
|
|
chrome.storage = require('@electron/internal/renderer/extensions/storage').setup(extensionId)
|
2016-05-18 12:19:50 +00:00
|
|
|
|
|
|
|
chrome.pageAction = {
|
2016-05-28 12:13:00 +00:00
|
|
|
show () {},
|
|
|
|
hide () {},
|
|
|
|
setTitle () {},
|
|
|
|
getTitle () {},
|
|
|
|
setIcon () {},
|
|
|
|
setPopup () {},
|
|
|
|
getPopup () {}
|
2016-05-28 01:26:41 +00:00
|
|
|
}
|
2016-06-07 22:34:17 +00:00
|
|
|
|
2018-09-20 03:43:26 +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
|
|
|
}
|