feat: add options to webContents.loadFile (#14515)

This commit is contained in:
Milan Burda 2018-09-11 09:56:49 +02:00 committed by Samuel Attard
parent 7a766e82f9
commit 58577bd431
8 changed files with 31 additions and 29 deletions

View file

@ -1221,9 +1221,13 @@ win.loadURL('http://localhost:8000/post', {
}) })
``` ```
#### `win.loadFile(filePath)` #### `win.loadFile(filePath[, options])`
* `filePath` String * `filePath` String
* `options` Object (optional)
* `query` Object (optional) - Passed to `url.format()`.
* `search` String (optional) - Passed to `url.format()`.
* `hash` String (optional) - Passed to `url.format()`.
Same as `webContents.loadFile`, `filePath` should be a path to an HTML Same as `webContents.loadFile`, `filePath` should be a path to an HTML
file relative to the root of your application. See the `webContents` docs file relative to the root of your application. See the `webContents` docs

View file

@ -647,9 +647,13 @@ const options = {extraHeaders: 'pragma: no-cache\n'}
webContents.loadURL('https://github.com', options) webContents.loadURL('https://github.com', options)
``` ```
#### `contents.loadFile(filePath)` #### `contents.loadFile(filePath[, options])`
* `filePath` String * `filePath` String
* `options` Object (optional)
* `query` Object (optional) - Passed to `url.format()`.
* `search` String (optional) - Passed to `url.format()`.
* `hash` String (optional) - Passed to `url.format()`.
Loads the given file in the window, `filePath` should be a path to Loads the given file in the window, `filePath` should be a path to
an HTML file relative to the root of your application. For instance an HTML file relative to the root of your application. For instance

View file

@ -182,8 +182,8 @@ Object.assign(BrowserWindow.prototype, {
getURL (...args) { getURL (...args) {
return this.webContents.getURL() return this.webContents.getURL()
}, },
loadFile (filePath) { loadFile (...args) {
return this.webContents.loadFile(filePath) return this.webContents.loadFile(...args)
}, },
reload (...args) { reload (...args) {
return this.webContents.reload(...args) return this.webContents.reload(...args)

View file

@ -243,14 +243,19 @@ WebContents.prototype.getZoomLevel = function (callback) {
}) })
} }
WebContents.prototype.loadFile = function (filePath) { WebContents.prototype.loadFile = function (filePath, options = {}) {
if (typeof filePath !== 'string') { if (typeof filePath !== 'string') {
throw new Error('Must pass filePath as a string') throw new Error('Must pass filePath as a string')
} }
const {query, search, hash} = options
return this.loadURL(url.format({ return this.loadURL(url.format({
protocol: 'file', protocol: 'file',
slashes: true, slashes: true,
pathname: path.resolve(app.getAppPath(), filePath) pathname: path.resolve(app.getAppPath(), filePath),
query,
search,
hash
})) }))
} }

View file

@ -1441,10 +1441,8 @@ describe('BrowserWindow module', () => {
} }
}) })
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload) ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
let htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open-external')
const pageUrl = 'file://' + htmlPath
let popupWindow let popupWindow
w.loadURL(pageUrl) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), {search: 'window-open-external'})
w.webContents.once('new-window', (e, url, frameName, disposition, options) => { w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
assert.equal(url, 'http://www.google.com/#q=electron') assert.equal(url, 'http://www.google.com/#q=electron')
assert.equal(options.width, 505) assert.equal(options.width, 505)
@ -1518,8 +1516,6 @@ describe('BrowserWindow module', () => {
}) })
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload) ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
let htmlPath = path.join(fixtures, 'api', 'sandbox.html?verify-ipc-sender')
const pageUrl = 'file://' + htmlPath
let childWc let childWc
w.webContents.once('new-window', (e, url, frameName, disposition, options) => { w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
childWc = options.webContents childWc = options.webContents
@ -1538,7 +1534,7 @@ describe('BrowserWindow module', () => {
'parent-answer', 'parent-answer',
'child-answer' 'child-answer'
], done) ], done)
w.loadURL(pageUrl) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), {search: 'verify-ipc-sender'})
}) })
describe('event handling', () => { describe('event handling', () => {
@ -1546,7 +1542,7 @@ describe('BrowserWindow module', () => {
waitForEvents(w, [ waitForEvents(w, [
'page-title-updated' 'page-title-updated'
], done) ], done)
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?window-events')) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), {search: 'window-events'})
}) })
it('works for stop events', (done) => { it('works for stop events', (done) => {
@ -1555,7 +1551,7 @@ describe('BrowserWindow module', () => {
'did-fail-load', 'did-fail-load',
'did-stop-loading' 'did-stop-loading'
], done) ], done)
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?webcontents-stop')) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), {search: 'webcontents-stop'})
}) })
it('works for web contents events', (done) => { it('works for web contents events', (done) => {
@ -1569,7 +1565,7 @@ describe('BrowserWindow module', () => {
'did-frame-finish-load', 'did-frame-finish-load',
'dom-ready' 'dom-ready'
], done) ], done)
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?webcontents-events')) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), {search: 'webcontents-events'})
}) })
}) })
@ -1640,7 +1636,7 @@ describe('BrowserWindow module', () => {
sandbox: true sandbox: true
} }
}) })
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?allocate-memory')) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), {search: 'allocate-memory'})
ipcMain.once('answer', function (event, {bytesBeforeOpen, bytesAfterOpen, bytesAfterClose}) { ipcMain.once('answer', function (event, {bytesBeforeOpen, bytesAfterOpen, bytesAfterClose}) {
const memoryIncreaseByOpen = bytesAfterOpen - bytesBeforeOpen const memoryIncreaseByOpen = bytesAfterOpen - bytesBeforeOpen
const memoryDecreaseByClose = bytesAfterOpen - bytesAfterClose const memoryDecreaseByClose = bytesAfterOpen - bytesAfterClose
@ -1663,7 +1659,7 @@ describe('BrowserWindow module', () => {
sandbox: true sandbox: true
} }
}) })
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?reload-remote')) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), {search: 'reload-remote'})
ipcMain.on('get-remote-module-path', (event) => { ipcMain.on('get-remote-module-path', (event) => {
event.returnValue = path.join(fixtures, 'module', 'hello.js') event.returnValue = path.join(fixtures, 'module', 'hello.js')
@ -1698,7 +1694,7 @@ describe('BrowserWindow module', () => {
}) })
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload) ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?reload-remote-child')) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), {search: 'reload-remote-child'})
ipcMain.on('get-remote-module-path', (event) => { ipcMain.on('get-remote-module-path', (event) => {
event.returnValue = path.join(fixtures, 'module', 'hello-child.js') event.returnValue = path.join(fixtures, 'module', 'hello-child.js')

View file

@ -64,12 +64,7 @@ describe('crashReporter module', () => {
stopServer = startServer({ stopServer = startServer({
callback (port) { callback (port) {
const crashUrl = url.format({ w.loadFile(path.join(fixtures, 'api', 'crash.html'), {query: {port}})
protocol: 'file',
pathname: path.join(fixtures, 'api', 'crash.html'),
search: '?port=' + port
})
w.loadURL(crashUrl)
}, },
processType: 'renderer', processType: 'renderer',
done: done done: done

View file

@ -1445,6 +1445,7 @@ describe('net module', () => {
ipcRenderer.send('eval', ` ipcRenderer.send('eval', `
const {net} = require('electron') const {net} = require('electron')
const http = require('http') const http = require('http')
const url = require('url')
const netRequest = net.request('${server.url}${netRequestUrl}') const netRequest = net.request('${server.url}${netRequestUrl}')
netRequest.on('response', function (netResponse) { netRequest.on('response', function (netResponse) {
const serverUrl = url.parse('${server.url}') const serverUrl = url.parse('${server.url}')

View file

@ -8,7 +8,6 @@ const {Coverage} = require('electabul')
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const url = require('url')
const util = require('util') const util = require('util')
const v8 = require('v8') const v8 = require('v8')
@ -139,14 +138,12 @@ app.on('ready', function () {
backgroundThrottling: false backgroundThrottling: false
} }
}) })
window.loadURL(url.format({ window.loadFile('static/index.html', {
pathname: path.join(__dirname, '/index.html'),
protocol: 'file',
query: { query: {
grep: argv.grep, grep: argv.grep,
invert: argv.invert ? 'true' : '' invert: argv.invert ? 'true' : ''
} }
})) })
window.on('unresponsive', function () { window.on('unresponsive', function () {
var chosen = dialog.showMessageBox(window, { var chosen = dialog.showMessageBox(window, {
type: 'warning', type: 'warning',