Merge pull request #11565 from electron/add-load-file-helper

Add window.loadFile and webContents.loadFile helper methods
This commit is contained in:
Samuel Attard 2018-01-23 09:08:23 +11:00 коммит произвёл GitHub
Родитель 8a4c76d655 365fe6b067
Коммит 32a1395bcf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 46 добавлений и 0 удалений

Просмотреть файл

@ -1159,6 +1159,14 @@ win.loadURL('http://localhost:8000/post', {
})
```
#### `win.loadFile(filePath)`
* `filePath` String
Same as `webContents.loadFile`, `filePath` should be a path to an HTML
file relative to the root of your application. See the `webContents` docs
for more information.
#### `win.reload()`
Same as `webContents.reload`.

Просмотреть файл

@ -625,6 +625,28 @@ const options = {extraHeaders: 'pragma: no-cache\n'}
webContents.loadURL('https://github.com', options)
```
#### `contents.loadFile(filePath)`
* `filePath` String
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 app structure like this:
```sh
| root
| - package.json
| - src
| - main.js
| - index.html
```
Would require code like this
```js
win.loadFile('src/index.html')
```
#### `contents.downloadURL(url)`
* `url` String

Просмотреть файл

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

Просмотреть файл

@ -2,6 +2,8 @@
const {EventEmitter} = require('events')
const electron = require('electron')
const path = require('path')
const url = require('url')
const {app, ipcMain, session, NavigationController} = electron
// session is not used here, the purpose is to make sure session is initalized
@ -243,6 +245,17 @@ WebContents.prototype.getZoomLevel = function (callback) {
})
}
WebContents.prototype.loadFile = function (filePath) {
if (typeof filePath !== 'string') {
throw new Error('Must pass filePath as a string')
}
return this.loadURL(url.format({
protocol: 'file',
slashes: true,
pathname: path.resolve(app.getAppPath(), filePath)
}))
}
WebContents.prototype.getZoomFactor = function (callback) {
if (typeof callback !== 'function') {
throw new Error('Must pass function as an argument')