Read content_scripts to preferences

This commit is contained in:
Cheng Zhao 2016-05-27 09:47:37 +09:00
parent 7eab259d92
commit a63ff714f8

View file

@ -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)})`)