Add simple support for background page
This commit is contained in:
parent
99c1434051
commit
edd8210ae5
1 changed files with 60 additions and 18 deletions
|
@ -1,4 +1,4 @@
|
||||||
const {app, protocol, BrowserWindow} = require('electron')
|
const {app, protocol, webContents, BrowserWindow} = require('electron')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const url = require('url')
|
const url = require('url')
|
||||||
|
@ -28,24 +28,43 @@ let manifestMap = {}
|
||||||
const getManifestFromPath = function (srcDirectory) {
|
const getManifestFromPath = function (srcDirectory) {
|
||||||
let manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json')))
|
let manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json')))
|
||||||
if (!manifestMap[manifest.name]) {
|
if (!manifestMap[manifest.name]) {
|
||||||
|
const hostname = generateHostForPath(srcDirectory)
|
||||||
manifestMap[manifest.name] = manifest
|
manifestMap[manifest.name] = manifest
|
||||||
|
Object.assign(manifest, {
|
||||||
|
srcDirectory: srcDirectory,
|
||||||
|
hostname: hostname,
|
||||||
// We can not use 'file://' directly because all resources in the extension
|
// We can not use 'file://' directly because all resources in the extension
|
||||||
// will be treated as relative to the root in Chrome.
|
// will be treated as relative to the root in Chrome.
|
||||||
manifest.startPage = url.format({
|
startPage: url.format({
|
||||||
protocol: 'chrome-extension',
|
protocol: 'chrome-extension',
|
||||||
slashes: true,
|
slashes: true,
|
||||||
hostname: generateHostForPath(srcDirectory),
|
hostname: hostname,
|
||||||
pathname: manifest.devtools_page
|
pathname: manifest.devtools_page
|
||||||
})
|
})
|
||||||
manifest.srcDirectory = srcDirectory
|
})
|
||||||
return manifest
|
return manifest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the extensions for the window.
|
// Manage the background pages.
|
||||||
const loadDevToolsExtensions = function (win, extensionInfoArray) {
|
let backgroundPages = {}
|
||||||
if (!win.devToolsWebContents) return
|
|
||||||
win.devToolsWebContents.executeJavaScript(`DevToolsAPI.addExtensions(${JSON.stringify(extensionInfoArray)})`)
|
const startBackgroundPages = function (manifest) {
|
||||||
|
if (backgroundPages[manifest.hostname] || !manifest.background) return
|
||||||
|
|
||||||
|
const scripts = manifest.background.scripts.map((name) => {
|
||||||
|
return `<script src="${name}"></script>`
|
||||||
|
}).join('')
|
||||||
|
const html = new Buffer(`<html><body>${scripts}</body></html>`)
|
||||||
|
|
||||||
|
const contents = webContents.create({})
|
||||||
|
backgroundPages[manifest.hostname] = { html: html, contents: contents }
|
||||||
|
contents.loadURL(url.format({
|
||||||
|
protocol: 'chrome-extension',
|
||||||
|
slashes: true,
|
||||||
|
hostname: manifest.hostname,
|
||||||
|
pathname: '_generated_background_page.html'
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transfer the |manifest| to a format that can be recognized by the
|
// Transfer the |manifest| to a format that can be recognized by the
|
||||||
|
@ -59,6 +78,17 @@ const manifestToExtensionInfo = function (manifest) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load the extensions for the window.
|
||||||
|
const loadDevToolsExtensions = function (win, manifests) {
|
||||||
|
if (!win.devToolsWebContents) return
|
||||||
|
|
||||||
|
for (let manifest of manifests) {
|
||||||
|
startBackgroundPages(manifest)
|
||||||
|
}
|
||||||
|
const extensionInfoArray = manifests.map(manifestToExtensionInfo)
|
||||||
|
win.devToolsWebContents.executeJavaScript(`DevToolsAPI.addExtensions(${JSON.stringify(extensionInfoArray)})`)
|
||||||
|
}
|
||||||
|
|
||||||
// The persistent path of "DevTools Extensions" preference file.
|
// The persistent path of "DevTools Extensions" preference file.
|
||||||
let loadedExtensionsPath = null
|
let loadedExtensionsPath = null
|
||||||
|
|
||||||
|
@ -107,9 +137,22 @@ app.once('ready', function () {
|
||||||
let directory = getPathForHost(parsed.hostname)
|
let directory = getPathForHost(parsed.hostname)
|
||||||
if (!directory) return callback()
|
if (!directory) return callback()
|
||||||
|
|
||||||
callback(path.join(directory, parsed.path))
|
if (parsed.path === '/_generated_background_page.html' &&
|
||||||
|
backgroundPages[parsed.hostname]) {
|
||||||
|
return callback({
|
||||||
|
mimeType: 'text/html',
|
||||||
|
data: backgroundPages[parsed.hostname].html
|
||||||
|
})
|
||||||
}
|
}
|
||||||
protocol.registerFileProtocol('chrome-extension', chromeExtensionHandler, function (error) {
|
|
||||||
|
fs.readFile(path.join(directory, parsed.path), function (err, content) {
|
||||||
|
if (err)
|
||||||
|
callback(-6) // FILE_NOT_FOUND
|
||||||
|
else
|
||||||
|
return callback({mimeType: 'text/html', data: content})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
protocol.registerBufferProtocol('chrome-extension', chromeExtensionHandler, function (error) {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(`Unable to register chrome-extension protocol: ${error}`)
|
console.error(`Unable to register chrome-extension protocol: ${error}`)
|
||||||
}
|
}
|
||||||
|
@ -118,9 +161,8 @@ app.once('ready', function () {
|
||||||
BrowserWindow.addDevToolsExtension = function (srcDirectory) {
|
BrowserWindow.addDevToolsExtension = function (srcDirectory) {
|
||||||
const manifest = getManifestFromPath(srcDirectory)
|
const manifest = getManifestFromPath(srcDirectory)
|
||||||
if (manifest) {
|
if (manifest) {
|
||||||
const extensionInfo = manifestToExtensionInfo(manifest)
|
|
||||||
for (let win of BrowserWindow.getAllWindows()) {
|
for (let win of BrowserWindow.getAllWindows()) {
|
||||||
loadDevToolsExtensions(win, [extensionInfo])
|
loadDevToolsExtensions(win, [manifest])
|
||||||
}
|
}
|
||||||
return manifest.name
|
return manifest.name
|
||||||
}
|
}
|
||||||
|
@ -134,7 +176,7 @@ app.once('ready', function () {
|
||||||
BrowserWindow.prototype._init = function () {
|
BrowserWindow.prototype._init = function () {
|
||||||
init.call(this)
|
init.call(this)
|
||||||
this.webContents.on('devtools-opened', () => {
|
this.webContents.on('devtools-opened', () => {
|
||||||
loadDevToolsExtensions(this, objectValues(manifestMap).map(manifestToExtensionInfo))
|
loadDevToolsExtensions(this, objectValues(manifestMap))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue