https://github.com/electron-utils/electron-protocols
Manage file protocols in Electron
https://github.com/electron-utils/electron-protocols
electron filesystem-path protocol
Last synced: 2 months ago
JSON representation
Manage file protocols in Electron
- Host: GitHub
- URL: https://github.com/electron-utils/electron-protocols
- Owner: electron-utils
- License: mit
- Created: 2017-01-23T03:11:16.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-19T04:04:26.000Z (about 8 years ago)
- Last Synced: 2025-03-01T10:06:29.310Z (3 months ago)
- Topics: electron, filesystem-path, protocol
- Language: JavaScript
- Size: 89.8 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# electron-protocols
[](https://travis-ci.org/electron-utils/electron-protocols)
[](https://ci.appveyor.com/project/jwu/electron-protocols)
[](https://david-dm.org/electron-utils/electron-protocols)
[](https://david-dm.org/electron-utils/electron-protocols#info=devDependencies)Manage file protocols in Electron
## Install
```bash
npm install --save electron-protocols
```Run the example:
```
npm start example
```## Usage
First register your protocol in main process before `app.on('ready')`:
**main process**
```javascript
const protocols = require('electron-protocols');
protocols.register('app', protocols.basepath(app.getAppPath()));
```Then you can use `protocols.path` to map your protocol to a file path:
**renderer/main process**
```javascript
const protocols = require('electron-protocols');// return the module in ${app.getAppPath()}/my/module.js
const myModule = require(protocols.path('app://my/module.js'));
```Also, you are free to use protocol in html in renderer process:
```html
![]()
```## FAQ
### What is the benefit to register again in renderer process?
It will speed up the search of `protocols.path` by skip calling the remote (ipc-sync) functions.
## API
### Methods
### protocols.register(protocol, fn)
- `protocol` string
- `fn` functionRegister a protocol so that {@link Editor.url} can use it to convert an url to the filesystem path.
The `fn` accept an url Object via [url.parse](https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost)Example:
```javascript
const {app} = require('electron');
const protocols = require('electron-protocols');
const path = require('path');protocols.register('app', uri => {
let base = app.getAppPath();
if ( uri.pathname ) {
return path.join( base, uri.host, uri.pathname );
}
return path.join( base, uri.host );
});
```### protocols.path(url)
- `url` string
Convert a url by its protocol to a filesystem path. This function is useful when you try to get
some internal file. You can use `protocols.register` to register and map your filesystem path to url.Example:
```javascript
// it will return "{your-app-path}/foobar/foobar.js"
protocols.path('app://foobar/foobar.js');
```### protocols.basepath(base)
- `base` string
A function help you register protocol by `base` path you provide.
Example:
```javascript
protocols.register('app', protocols.basepath(app.getAppPath()));
```## License
MIT © 2017 Johnny Wu