diff --git a/lib/browser/chrome-extension.js b/lib/browser/chrome-extension.js index aa72552d0f63..1ccc130ebb85 100644 --- a/lib/browser/chrome-extension.js +++ b/lib/browser/chrome-extension.js @@ -1,4 +1,6 @@ const {app, protocol, webContents, BrowserWindow} = require('electron') +const renderProcessPreferences = process.atomBinding('render_process_preferences').forAllBrowserWindow() + const fs = require('fs') const path = require('path') const url = require('url') @@ -67,6 +69,34 @@ const startBackgroundPages = function (manifest) { })) } +// Transfer the content scripts to renderer. +const contentScripts = {} + +const injectContentScripts = function (manifest) { + if (contentScripts[manifest.name] || !manifest.content_scripts) return + + const readArrayOfFiles = function (relativePath) { + return fs.readFileSync(path.join(manifest.srcDirectory, relativePath)) + } + + const contentScriptToEntry = function (script) { + return { + matches: script.matches, + js: script.js.map(readArrayOfFiles), + run_at: script.run_at || 'document_idle' + } + } + + try { + const entry = { + contentScripts: manifest.content_scripts.map(contentScriptToEntry) + } + contentScripts[manifest.name] = renderProcessPreferences.addEntry(entry) + } catch (e) { + console.error('Failed to read content scripts', e) + } +} + // Transfer the |manifest| to a format that can be recognized by the // |DevToolsAPI.addExtensions|. const manifestToExtensionInfo = function (manifest) { @@ -84,6 +114,7 @@ const loadDevToolsExtensions = function (win, manifests) { for (const manifest of manifests) { startBackgroundPages(manifest) + injectContentScripts(manifest) } const extensionInfoArray = manifests.map(manifestToExtensionInfo) win.devToolsWebContents.executeJavaScript(`DevToolsAPI.addExtensions(${JSON.stringify(extensionInfoArray)})`)