The consts

This commit is contained in:
Cheng Zhao 2016-05-27 07:43:23 +09:00
parent cdfbe876a5
commit b646d7a55c

View file

@ -9,11 +9,11 @@ const objectValues = function (object) {
} }
// Mapping between hostname and file path. // Mapping between hostname and file path.
let hostPathMap = {} const hostPathMap = {}
let hostPathMapNextKey = 0 let hostPathMapNextKey = 0
const generateHostForPath = function (path) { const generateHostForPath = function (path) {
let key = `extension-${++hostPathMapNextKey}` const key = `extension-${++hostPathMapNextKey}`
hostPathMap[key] = path hostPathMap[key] = path
return key return key
} }
@ -23,10 +23,10 @@ const getPathForHost = function (host) {
} }
// Cache manifests. // Cache manifests.
let manifestMap = {} const manifestMap = {}
const getManifestFromPath = function (srcDirectory) { const getManifestFromPath = function (srcDirectory) {
let manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json'))) const manifest = JSON.parse(fs.readFileSync(path.join(srcDirectory, 'manifest.json')))
if (!manifestMap[manifest.name]) { if (!manifestMap[manifest.name]) {
const hostname = generateHostForPath(srcDirectory) const hostname = generateHostForPath(srcDirectory)
manifestMap[manifest.name] = manifest manifestMap[manifest.name] = manifest
@ -47,7 +47,7 @@ const getManifestFromPath = function (srcDirectory) {
} }
// Manage the background pages. // Manage the background pages.
let backgroundPages = {} const backgroundPages = {}
const startBackgroundPages = function (manifest) { const startBackgroundPages = function (manifest) {
if (backgroundPages[manifest.hostname] || !manifest.background) return if (backgroundPages[manifest.hostname] || !manifest.background) return
@ -82,7 +82,7 @@ const manifestToExtensionInfo = function (manifest) {
const loadDevToolsExtensions = function (win, manifests) { const loadDevToolsExtensions = function (win, manifests) {
if (!win.devToolsWebContents) return if (!win.devToolsWebContents) return
for (let manifest of manifests) { for (const manifest of manifests) {
startBackgroundPages(manifest) startBackgroundPages(manifest)
} }
const extensionInfoArray = manifests.map(manifestToExtensionInfo) const extensionInfoArray = manifests.map(manifestToExtensionInfo)
@ -94,7 +94,7 @@ let loadedExtensionsPath = null
app.on('will-quit', function () { app.on('will-quit', function () {
try { try {
let loadedExtensions = objectValues(manifestMap).map(function (manifest) { const loadedExtensions = objectValues(manifestMap).map(function (manifest) {
return manifest.srcDirectory return manifest.srcDirectory
}) })
if (loadedExtensions.length > 0) { if (loadedExtensions.length > 0) {
@ -117,10 +117,10 @@ app.once('ready', function () {
// Load persisted extensions. // Load persisted extensions.
loadedExtensionsPath = path.join(app.getPath('userData'), 'DevTools Extensions') loadedExtensionsPath = path.join(app.getPath('userData'), 'DevTools Extensions')
try { try {
let loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath)) const loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath))
if (Array.isArray(loadedExtensions)) { if (Array.isArray(loadedExtensions)) {
// Preheat the manifest cache. // Preheat the manifest cache.
for (let srcDirectory of loadedExtensions) { for (const srcDirectory of loadedExtensions) {
getManifestFromPath(srcDirectory) getManifestFromPath(srcDirectory)
} }
} }
@ -130,11 +130,11 @@ app.once('ready', function () {
// The chrome-extension: can map a extension URL request to real file path. // The chrome-extension: can map a extension URL request to real file path.
const chromeExtensionHandler = function (request, callback) { const chromeExtensionHandler = function (request, callback) {
let parsed = url.parse(request.url) const parsed = url.parse(request.url)
if (!parsed.hostname || !parsed.path) return callback() if (!parsed.hostname || !parsed.path) return callback()
if (!/extension-\d+/.test(parsed.hostname)) return callback() if (!/extension-\d+/.test(parsed.hostname)) return callback()
let directory = getPathForHost(parsed.hostname) const directory = getPathForHost(parsed.hostname)
if (!directory) return callback() if (!directory) return callback()
if (parsed.path === '/_generated_background_page.html' && if (parsed.path === '/_generated_background_page.html' &&
@ -146,10 +146,11 @@ app.once('ready', function () {
} }
fs.readFile(path.join(directory, parsed.path), function (err, content) { fs.readFile(path.join(directory, parsed.path), function (err, content) {
if (err) if (err) {
callback(-6) // FILE_NOT_FOUND return callback(-6) // FILE_NOT_FOUND
else } else {
return callback({mimeType: 'text/html', data: content}) return callback({mimeType: 'text/html', data: content})
}
}) })
} }
protocol.registerBufferProtocol('chrome-extension', chromeExtensionHandler, function (error) { protocol.registerBufferProtocol('chrome-extension', chromeExtensionHandler, function (error) {
@ -161,7 +162,7 @@ app.once('ready', function () {
BrowserWindow.addDevToolsExtension = function (srcDirectory) { BrowserWindow.addDevToolsExtension = function (srcDirectory) {
const manifest = getManifestFromPath(srcDirectory) const manifest = getManifestFromPath(srcDirectory)
if (manifest) { if (manifest) {
for (let win of BrowserWindow.getAllWindows()) { for (const win of BrowserWindow.getAllWindows()) {
loadDevToolsExtensions(win, [manifest]) loadDevToolsExtensions(win, [manifest])
} }
return manifest.name return manifest.name
@ -172,7 +173,7 @@ app.once('ready', function () {
} }
// Load persisted extensions when devtools is opened. // Load persisted extensions when devtools is opened.
let init = BrowserWindow.prototype._init const init = BrowserWindow.prototype._init
BrowserWindow.prototype._init = function () { BrowserWindow.prototype._init = function () {
init.call(this) init.call(this)
this.webContents.on('devtools-opened', () => { this.webContents.on('devtools-opened', () => {