refactor: use ipcMainUtils.invokeInWebContents / ipcRendererUtils.handle helpers for Chrome APIs (#17417)
This commit is contained in:
parent
546466b209
commit
336db33d18
9 changed files with 82 additions and 99 deletions
|
@ -67,22 +67,20 @@ class Port {
|
|||
}
|
||||
|
||||
// Inject chrome API to the |context|
|
||||
export function injectTo (extensionId: string, isBackgroundPage: boolean, context: any) {
|
||||
export function injectTo (extensionId: string, context: any) {
|
||||
const chrome = context.chrome = context.chrome || {}
|
||||
let originResultID = 1
|
||||
|
||||
ipcRendererInternal.on(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, (
|
||||
_event: Electron.Event, tabId: number, portId: number, connectInfo: { name: string }
|
||||
) => {
|
||||
chrome.runtime.onConnect.emit(new Port(tabId, portId, extensionId, connectInfo.name))
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
ipcRendererInternal.on(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, (
|
||||
_event: Electron.Event, tabId: number, message: string, resultID: number
|
||||
ipcRendererUtils.handle(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, (
|
||||
_event: Electron.Event, tabId: number, message: string
|
||||
) => {
|
||||
chrome.runtime.onMessage.emit(message, new MessageSender(tabId, extensionId), (messageResult: any) => {
|
||||
ipcRendererInternal.send(`CHROME_RUNTIME_ONMESSAGE_RESULT_${resultID}`, messageResult)
|
||||
return new Promise(resolve => {
|
||||
chrome.runtime.onMessage.emit(message, new MessageSender(tabId, extensionId), resolve)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -115,11 +113,6 @@ export function injectTo (extensionId: string, isBackgroundPage: boolean, contex
|
|||
|
||||
// https://developer.chrome.com/extensions/runtime#method-connect
|
||||
connect (...args: Array<any>) {
|
||||
if (isBackgroundPage) {
|
||||
console.error('chrome.runtime.connect is not supported in background page')
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the optional args.
|
||||
let targetExtensionId = extensionId
|
||||
let connectInfo = { name: '' }
|
||||
|
@ -135,36 +128,33 @@ export function injectTo (extensionId: string, isBackgroundPage: boolean, contex
|
|||
|
||||
// https://developer.chrome.com/extensions/runtime#method-sendMessage
|
||||
sendMessage (...args: Array<any>) {
|
||||
if (isBackgroundPage) {
|
||||
console.error('chrome.runtime.sendMessage is not supported in background page')
|
||||
return
|
||||
// Parse the optional args.
|
||||
const targetExtensionId = extensionId
|
||||
let message: string
|
||||
let options: Object | undefined
|
||||
let responseCallback: Chrome.Tabs.SendMessageCallback = () => {}
|
||||
|
||||
if (typeof args[args.length - 1] === 'function') {
|
||||
responseCallback = args.pop()
|
||||
}
|
||||
|
||||
// Parse the optional args.
|
||||
let targetExtensionId = extensionId
|
||||
let message
|
||||
if (args.length === 1) {
|
||||
message = args[0]
|
||||
[message] = args
|
||||
} else if (args.length === 2) {
|
||||
// A case of not provide extension-id: (message, responseCallback)
|
||||
if (typeof args[1] === 'function') {
|
||||
ipcRendererInternal.once(`CHROME_RUNTIME_SENDMESSAGE_RESULT_${originResultID}`,
|
||||
(_event: Electron.Event, result: any) => args[1](result)
|
||||
)
|
||||
|
||||
message = args[0]
|
||||
if (typeof args[0] === 'string') {
|
||||
[extensionId, message] = args
|
||||
} else {
|
||||
[targetExtensionId, message] = args
|
||||
[message, options] = args
|
||||
}
|
||||
} else {
|
||||
console.error('options is not supported')
|
||||
ipcRendererInternal.once(`CHROME_RUNTIME_SENDMESSAGE_RESULT_${originResultID}`,
|
||||
(event: Electron.Event, result: any) => args[2](result)
|
||||
)
|
||||
[extensionId, message, options] = args
|
||||
}
|
||||
|
||||
ipcRendererInternal.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message, originResultID)
|
||||
originResultID++
|
||||
if (options) {
|
||||
console.error('options are not supported')
|
||||
}
|
||||
|
||||
ipcRendererUtils.invoke('CHROME_RUNTIME_SEND_MESSAGE', targetExtensionId, message).then(responseCallback)
|
||||
},
|
||||
|
||||
onConnect: new Event(),
|
||||
|
@ -177,15 +167,10 @@ export function injectTo (extensionId: string, isBackgroundPage: boolean, contex
|
|||
executeScript (
|
||||
tabId: number,
|
||||
details: Chrome.Tabs.ExecuteScriptDetails,
|
||||
resultCallback: Chrome.Tabs.ExecuteScriptCallback
|
||||
resultCallback: Chrome.Tabs.ExecuteScriptCallback = () => {}
|
||||
) {
|
||||
if (resultCallback) {
|
||||
ipcRendererInternal.once(`CHROME_TABS_EXECUTESCRIPT_RESULT_${originResultID}`,
|
||||
(_event: Electron.Event, result: any) => resultCallback([result])
|
||||
)
|
||||
}
|
||||
ipcRendererInternal.send('CHROME_TABS_EXECUTESCRIPT', originResultID, tabId, extensionId, details)
|
||||
originResultID++
|
||||
ipcRendererUtils.invoke('CHROME_TABS_EXECUTE_SCRIPT', tabId, extensionId, details)
|
||||
.then((result: any) => resultCallback([result]))
|
||||
},
|
||||
|
||||
// https://developer.chrome.com/extensions/tabs#method-sendMessage
|
||||
|
@ -193,15 +178,9 @@ export function injectTo (extensionId: string, isBackgroundPage: boolean, contex
|
|||
tabId: number,
|
||||
message: any,
|
||||
_options: Chrome.Tabs.SendMessageDetails,
|
||||
responseCallback: Chrome.Tabs.SendMessageCallback
|
||||
responseCallback: Chrome.Tabs.SendMessageCallback = () => {}
|
||||
) {
|
||||
if (responseCallback) {
|
||||
ipcRendererInternal.once(`CHROME_TABS_SEND_MESSAGE_RESULT_${originResultID}`,
|
||||
(_event: Electron.Event, result: any) => responseCallback(result)
|
||||
)
|
||||
}
|
||||
ipcRendererInternal.send('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, isBackgroundPage, message, originResultID)
|
||||
originResultID++
|
||||
ipcRendererUtils.invoke('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, message).then(responseCallback)
|
||||
},
|
||||
|
||||
onUpdated: new Event(),
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
||||
import { webFrame } from 'electron'
|
||||
|
||||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
|
||||
const v8Util = process.electronBinding('v8_util')
|
||||
|
||||
const IsolatedWorldIDs = {
|
||||
|
@ -94,17 +95,13 @@ const injectContentScript = function (extensionId: string, script: Electron.Cont
|
|||
}
|
||||
|
||||
// Handle the request of chrome.tabs.executeJavaScript.
|
||||
ipcRendererInternal.on('CHROME_TABS_EXECUTESCRIPT', function (
|
||||
ipcRendererUtils.handle('CHROME_TABS_EXECUTE_SCRIPT', function (
|
||||
event: Electron.Event,
|
||||
senderWebContentsId: number,
|
||||
requestId: number,
|
||||
extensionId: string,
|
||||
url: string,
|
||||
code: string
|
||||
) {
|
||||
runContentScript.call(window, extensionId, url, code).then(result => {
|
||||
ipcRendererInternal.sendToAll(senderWebContentsId, `CHROME_TABS_EXECUTESCRIPT_RESULT_${requestId}`, result)
|
||||
})
|
||||
return runContentScript.call(window, extensionId, url, code)
|
||||
})
|
||||
|
||||
module.exports = (getRenderProcessPreferences: typeof process.getRenderProcessPreferences) => {
|
||||
|
|
|
@ -49,7 +49,6 @@ const contextIsolation = hasSwitch('context-isolation')
|
|||
const nodeIntegration = hasSwitch('node-integration')
|
||||
const webviewTag = hasSwitch('webview-tag')
|
||||
const isHiddenPage = hasSwitch('hidden-page')
|
||||
const isBackgroundPage = hasSwitch('background-page')
|
||||
const usesNativeWindowOpen = hasSwitch('native-window-open')
|
||||
|
||||
const preloadScript = parseOption('preload', null)
|
||||
|
@ -74,7 +73,7 @@ switch (window.location.protocol) {
|
|||
}
|
||||
case 'chrome-extension:': {
|
||||
// Inject the chrome.* APIs that chrome extensions require
|
||||
require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, isBackgroundPage, window)
|
||||
require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, window)
|
||||
break
|
||||
}
|
||||
case 'chrome:':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue