refactor: piggy-back on ELECTRON_BROWSER_SANDBOX_LOAD to get content scripts (#18823)

This commit is contained in:
Milan Burda 2019-06-19 17:23:44 +02:00 committed by Alexey Kuzmin
parent 450aa33775
commit edb56500c7
6 changed files with 17 additions and 12 deletions

View file

@ -312,9 +312,9 @@ ipcMainUtils.handle('CHROME_TABS_EXECUTE_SCRIPT', async function (event, tabId,
return ipcMainUtils.invokeInWebContents(contents, false, 'CHROME_TABS_EXECUTE_SCRIPT', extensionId, url, code)
})
ipcMainUtils.handle('CHROME_GET_CONTENT_SCRIPTS', async (event) => {
exports.getContentScripts = () => {
return Object.values(contentScripts)
})
}
// Transfer the content scripts to renderer.
const contentScripts = {}

View file

@ -12,6 +12,7 @@ const features = process.electronBinding('features')
const { isPromise } = electron
const { getContentScripts } = require('@electron/internal/browser/chrome-extension')
const { crashReporterInit } = require('@electron/internal/browser/crash-reporter-init')
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
@ -541,6 +542,8 @@ const getPreloadScript = async function (preloadPath) {
return { preloadPath, preloadSrc, preloadError }
}
ipcMainUtils.handle('ELECTRON_GET_CONTENT_SCRIPTS', () => getContentScripts())
ipcMainUtils.handle('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event) {
const preloadPaths = [
...(event.sender.session ? event.sender.session.getPreloads() : []),
@ -548,6 +551,7 @@ ipcMainUtils.handle('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event) {
]
return {
contentScripts: getContentScripts(),
preloadScripts: await Promise.all(preloadPaths.map(path => getPreloadScript(path))),
isRemoteModuleEnabled: isRemoteModuleEnabled(event.sender),
isWebViewTagEnabled: guestViewManager.isWebViewTagEnabled(event.sender),

View file

@ -108,13 +108,7 @@ ipcRendererUtils.handle('CHROME_TABS_EXECUTE_SCRIPT', function (
return runContentScript.call(window, extensionId, url, code)
})
type ContentScriptEntry = {
extensionId: string;
contentScripts: Electron.ContentScript[];
}
module.exports = () => {
const entries = ipcRendererUtils.invokeSync<ContentScriptEntry[]>('CHROME_GET_CONTENT_SCRIPTS')
module.exports = (entries: Electron.ContentScriptEntry[]) => {
for (const entry of entries) {
if (entry.contentScripts) {
for (const script of entry.contentScripts) {

View file

@ -54,6 +54,7 @@ v8Util.setHiddenValue(global, 'ipcNative', {
// Use electron module after everything is ready.
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal')
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init')
webFrameInit()
@ -111,7 +112,8 @@ switch (window.location.protocol) {
windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen)
// Inject content scripts.
require('@electron/internal/renderer/content-scripts-injector')()
const contentScripts = ipcRendererUtils.invokeSync('ELECTRON_GET_CONTENT_SCRIPTS') as Electron.ContentScriptEntry[]
require('@electron/internal/renderer/content-scripts-injector')(contentScripts)
}
}

View file

@ -30,7 +30,7 @@ const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-rendere
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
const {
preloadScripts, isRemoteModuleEnabled, isWebViewTagEnabled, process: processProps
contentScripts, preloadScripts, isRemoteModuleEnabled, isWebViewTagEnabled, process: processProps
} = ipcRendererUtils.invokeSync('ELECTRON_BROWSER_SANDBOX_LOAD')
process.isRemoteModuleEnabled = isRemoteModuleEnabled
@ -124,7 +124,7 @@ switch (window.location.protocol) {
}
default: {
// Inject content scripts.
require('@electron/internal/renderer/content-scripts-injector')()
require('@electron/internal/renderer/content-scripts-injector')(contentScripts)
}
}

View file

@ -47,6 +47,11 @@ declare namespace Electron {
allFrames: boolean
}
type ContentScriptEntry = {
extensionId: string;
contentScripts: ContentScript[];
}
interface IpcRendererInternal extends Electron.IpcRenderer {
sendToAll(webContentsId: number, channel: string, ...args: any[]): void
}