add spec and docs
This commit is contained in:
parent
ad0c86db7a
commit
ae297760af
3 changed files with 42 additions and 4 deletions
|
@ -47,9 +47,10 @@ non-standard schemes can not recognize relative URLs:
|
||||||
<img src='test.png'>
|
<img src='test.png'>
|
||||||
</body>
|
</body>
|
||||||
```
|
```
|
||||||
|
Registering a scheme as standard, will allow access of files through
|
||||||
So if you want to register a custom protocol to replace the `http` protocol, you
|
the FileSystem API. Otherwise the renderer will throw a security error for the
|
||||||
have to register it as standard scheme:
|
scheme. So in general if you want to register a custom protocol to replace the
|
||||||
|
`http` protocol, you have to register it as standard scheme:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const {app, protocol} = require('electron')
|
const {app, protocol} = require('electron')
|
||||||
|
|
|
@ -4,7 +4,7 @@ const path = require('path')
|
||||||
const qs = require('querystring')
|
const qs = require('querystring')
|
||||||
const {closeWindow} = require('./window-helpers')
|
const {closeWindow} = require('./window-helpers')
|
||||||
const remote = require('electron').remote
|
const remote = require('electron').remote
|
||||||
const {BrowserWindow, protocol, webContents} = remote
|
const {BrowserWindow, ipcMain, protocol, webContents} = remote
|
||||||
|
|
||||||
describe('protocol module', function () {
|
describe('protocol module', function () {
|
||||||
var protocolName = 'sp'
|
var protocolName = 'sp'
|
||||||
|
@ -965,5 +965,18 @@ describe('protocol module', function () {
|
||||||
w.loadURL(origin)
|
w.loadURL(origin)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('can access files through FileSystem API', function (done) {
|
||||||
|
let filePath = path.join(__dirname, 'fixtures', 'pages', 'filesystem.html')
|
||||||
|
const handler = function (request, callback) {
|
||||||
|
callback({path: filePath})
|
||||||
|
}
|
||||||
|
protocol.registerFileProtocol(standardScheme, handler, function (error) {
|
||||||
|
if (error) return done(error)
|
||||||
|
w.loadURL(origin)
|
||||||
|
})
|
||||||
|
ipcMain.once('file-system-error', (event, err) => done(err))
|
||||||
|
ipcMain.once('file-system-write-end', () => done())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
24
spec/fixtures/pages/filesystem.html
vendored
Normal file
24
spec/fixtures/pages/filesystem.html
vendored
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<script>
|
||||||
|
const {ipcRenderer} = require('electron')
|
||||||
|
function onInitFs (fs) {
|
||||||
|
fs.root.getFile('log.txt', {create: true}, function (fileEntry) {
|
||||||
|
fileEntry.createWriter(function (fileWriter) {
|
||||||
|
var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});
|
||||||
|
fileWriter.onwriteend = function() {
|
||||||
|
ipcRenderer.send('file-system-write-end')
|
||||||
|
};
|
||||||
|
fileWriter.onerror = errorHandler
|
||||||
|
fileWriter.write(blob);
|
||||||
|
}, errorHandler);
|
||||||
|
}, errorHandler);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
navigator.webkitPersistentStorage.requestQuota(5 * 1024 * 1024, function (granted) {
|
||||||
|
webkitRequestFileSystem(TEMPORARY, granted, onInitFs, errorHandler);
|
||||||
|
}, errorHandler)
|
||||||
|
|
||||||
|
function errorHandler(e) {
|
||||||
|
ipcRenderer.send('file-system-error', e)
|
||||||
|
}
|
||||||
|
</script>
|
Loading…
Add table
Add a link
Reference in a new issue