Merge pull request #6298 from jhen0409/patch-5

Support background.page in extension manifest
This commit is contained in:
Kevin Sawicki 2016-07-01 08:59:39 -07:00 committed by GitHub
commit 5dcc354432

View file

@ -72,21 +72,29 @@ const backgroundPages = {}
const startBackgroundPages = function (manifest) {
if (backgroundPages[manifest.extensionId] || !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>`)
let html
let name
if (manifest.background.page) {
name = manifest.background.page
html = fs.readFileSync(path.join(manifest.srcDirectory, manifest.background.page))
} else {
name = '_generated_background_page.html'
const scripts = manifest.background.scripts.map((name) => {
return `<script src="${name}"></script>`
}).join('')
html = new Buffer(`<html><body>${scripts}</body></html>`)
}
const contents = webContents.create({
isBackgroundPage: true,
commandLineSwitches: ['--background-page']
})
backgroundPages[manifest.extensionId] = { html: html, webContents: contents }
backgroundPages[manifest.extensionId] = { html: html, webContents: contents, name: name }
contents.loadURL(url.format({
protocol: 'chrome-extension',
slashes: true,
hostname: manifest.extensionId,
pathname: '_generated_background_page.html'
pathname: name
}))
}
@ -309,11 +317,11 @@ app.once('ready', function () {
const manifest = manifestMap[parsed.hostname]
if (!manifest) return callback()
if (parsed.path === '/_generated_background_page.html' &&
backgroundPages[parsed.hostname]) {
const page = backgroundPages[parsed.hostname]
if (page && parsed.path === `/${page.name}`) {
return callback({
mimeType: 'text/html',
data: backgroundPages[parsed.hostname].html
data: page.html
})
}