Merge pull request #11565 from electron/add-load-file-helper
Add window.loadFile and webContents.loadFile helper methods
This commit is contained in:
Коммит
32a1395bcf
|
@ -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()`
|
#### `win.reload()`
|
||||||
|
|
||||||
Same as `webContents.reload`.
|
Same as `webContents.reload`.
|
||||||
|
|
|
@ -625,6 +625,28 @@ const options = {extraHeaders: 'pragma: no-cache\n'}
|
||||||
webContents.loadURL('https://github.com', options)
|
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)`
|
#### `contents.downloadURL(url)`
|
||||||
|
|
||||||
* `url` String
|
* `url` String
|
||||||
|
|
|
@ -169,6 +169,9 @@ Object.assign(BrowserWindow.prototype, {
|
||||||
getURL (...args) {
|
getURL (...args) {
|
||||||
return this.webContents.getURL()
|
return this.webContents.getURL()
|
||||||
},
|
},
|
||||||
|
loadFile (filePath) {
|
||||||
|
return this.webContents.loadFile(filePath)
|
||||||
|
},
|
||||||
reload (...args) {
|
reload (...args) {
|
||||||
return this.webContents.reload(...args)
|
return this.webContents.reload(...args)
|
||||||
},
|
},
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
const {EventEmitter} = require('events')
|
const {EventEmitter} = require('events')
|
||||||
const electron = require('electron')
|
const electron = require('electron')
|
||||||
|
const path = require('path')
|
||||||
|
const url = require('url')
|
||||||
const {app, ipcMain, session, NavigationController} = electron
|
const {app, ipcMain, session, NavigationController} = electron
|
||||||
|
|
||||||
// session is not used here, the purpose is to make sure session is initalized
|
// 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) {
|
WebContents.prototype.getZoomFactor = function (callback) {
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
throw new Error('Must pass function as an argument')
|
throw new Error('Must pass function as an argument')
|
||||||
|
|
Загрузка…
Ссылка в новой задаче