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