mirror of
https://github.com/electron/electron.git
synced 2026-02-11 15:45:04 -05:00
Read content_scripts to preferences
This commit is contained in:
@@ -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)})`)
|
||||
|
||||
Reference in New Issue
Block a user