# 協議
`protocol` 模塊可以注冊一個自定義協議,或者使用一個已經存在的協議.
例子,使用一個與 `file://` 功能相似的協議 :
```javascript
const electron = require('electron');
const app = electron.app;
const path = require('path');
app.on('ready', function() {
var protocol = electron.protocol;
protocol.registerFileProtocol('atom', function(request, callback) {
var url = request.url.substr(7);
callback({path: path.normalize(__dirname + '/' + url)});
}, function (error) {
if (error)
console.error('Failed to register protocol')
});
});
```
**注意:** 這個模塊只有在 `app` 模塊的 `ready` 事件觸發之后才可使用.
## 方法
`protocol` 模塊有如下方法:
### `protocol.registerStandardSchemes(schemes)`
* `schemes` Array - 將一個自定義的方案注冊為標準的方案.
一個標準的 `scheme` 遵循 RFC 3986 的
[generic URI syntax](https://tools.ietf.org/html/rfc3986#section-3) 標準. 這包含了 `file:` 和 `filesystem:`.
### `protocol.registerServiceWorkerSchemes(schemes)`
* `schemes` Array - 將一個自定義的方案注冊為處理 service workers.
### `protocol.registerFileProtocol(scheme, handler[, completion])`
* `scheme` String
* `handler` Function
* `completion` Function (可選)
注冊一個協議,用來發送響應文件.當通過這個協議來發起一個請求的時候,將使用 `handler(request, callback)` 來調用
`handler` .當 `scheme` 被成功注冊或者完成(錯誤)時失敗,將使用 `completion(null)` 調用 `completion`.
* `request` Object
* `url` String
* `referrer` String
* `method` String
* `uploadData` Array (可選)
* `callback` Function
`uploadData` 是一個 `data` 對象數組:
* `data` Object
* `bytes` Buffer - 被發送的內容.
* `file` String - 上傳的文件路徑.
為了處理請求,調用 `callback` 時需要使用文件路徑或者一個帶 `path` 參數的對象, 例如 `callback(filePath)` 或
`callback({path: filePath})`.
當不使用任何參數調用 `callback` 時,你可以指定一個數字或一個帶有 `error` 參數的對象,來標識 `request` 失敗.你可以使用的 error number 可以參考
[net error list][net-error].
默認 `scheme` 會被注冊為一個 `http:` 協議,它與遵循 "generic URI syntax" 規則的協議解析不同,例如 `file:` ,所以你或許應該調用 `protocol.registerStandardSchemes` 來創建一個標準的 scheme.
### `protocol.registerBufferProtocol(scheme, handler[, completion])`
* `scheme` String
* `handler` Function
* `completion` Function (可選)
注冊一個 `scheme` 協議,用來發送響應 `Buffer` .
這個方法的用法類似 `registerFileProtocol`,除非使用一個 `Buffer` 對象,或一個有 `data`,
`mimeType`, 和 `charset` 屬性的對象來調用 `callback` .
例子:
```javascript
protocol.registerBufferProtocol('atom', function(request, callback) {
callback({mimeType: 'text/html', data: new Buffer('<h5>Response</h5>')});
}, function (error) {
if (error)
console.error('Failed to register protocol')
});
```
### `protocol.registerStringProtocol(scheme, handler[, completion])`
* `scheme` String
* `handler` Function
* `completion` Function (可選)
注冊一個 `scheme` 協議,用來發送響應 `String` .
這個方法的用法類似 `registerFileProtocol`,除非使用一個 `String` 對象,或一個有 `data`,
`mimeType`, 和 `charset` 屬性的對象來調用 `callback` .
### `protocol.registerHttpProtocol(scheme, handler[, completion])`
* `scheme` String
* `handler` Function
* `completion` Function (可選)
注冊一個 `scheme` 協議,用來發送 HTTP 請求作為響應.
這個方法的用法類似 `registerFileProtocol`,除非使用一個 `redirectRequest` 對象,或一個有 `url`, `method`,
`referrer`, `uploadData` 和 `session` 屬性的對象來調用 `callback` .
* `redirectRequest` Object
* `url` String
* `method` String
* `session` Object (可選)
* `uploadData` Object (可選)
默認這個 HTTP 請求會使用當前 session .如果你想使用不同的session值,你應該設置 `session` 為 `null`.
POST 請求應當包含 `uploadData` 對象.
* `uploadData` object
* `contentType` String - 內容的 MIME type.
* `data` String - 被發送的內容.
### `protocol.unregisterProtocol(scheme[, completion])`
* `scheme` String
* `completion` Function (可選)
注銷自定義協議 `scheme`.
### `protocol.isProtocolHandled(scheme, callback)`
* `scheme` String
* `callback` Function
將使用一個布爾值來調用 `callback` ,這個布爾值標識了是否已經存在 `scheme` 的句柄了.
### `protocol.interceptFileProtocol(scheme, handler[, completion])`
* `scheme` String
* `handler` Function
* `completion` Function (可選)
攔截 `scheme` 協議并且使用 `handler` 作為協議的新的句柄來發送響應文件.
### `protocol.interceptStringProtocol(scheme, handler[, completion])`
* `scheme` String
* `handler` Function
* `completion` Function (可選)
攔截 `scheme` 協議并且使用 `handler` 作為協議的新的句柄來發送響應 `String`.
### `protocol.interceptBufferProtocol(scheme, handler[, completion])`
* `scheme` String
* `handler` Function
* `completion` Function (可選)
攔截 `scheme` 協議并且使用 `handler` 作為協議的新的句柄來發送響應 `Buffer`.
### `protocol.interceptHttpProtocol(scheme, handler[, completion])`
* `scheme` String
* `handler` Function
* `completion` Function (optional)
攔截 `scheme` 協議并且使用 `handler` 作為協議的新的句柄來發送新的響應 HTTP 請求.
Intercepts `scheme` protocol and uses `handler` as the protocol's new handler
which sends a new HTTP request as a response.
### `protocol.uninterceptProtocol(scheme[, completion])`
* `scheme` String
* `completion` Function
取消對 `scheme` 的攔截,使用它的原始句柄進行處理.
[net-error]: https://code.google.com/p/chromium/codesearch#chromium/src/net/base/net_error_list.h
- 介紹
- 常見問題
- Electron 常見問題
- 向導
- 支持平臺
- 分發應用
- 提交應用到 Mac App Store
- 打包應用
- 使用 Node 原生模塊
- 主進程調試
- 使用 Selenium 和 WebDriver
- 使用開發人員工具擴展
- 使用 Pepper Flash 插件
- 使用 Widevine CDM 插件
- 教程
- 快速入門
- 桌面環境集成
- 在線/離線事件探測
- API文檔
- 簡介
- 進程對象
- 支持的 Chrome 命令行開關
- 環境變量
- 自定義的 DOM 元素
- File 對象
- &lt;webview&gt; 標簽
- window.open 函數
- 在主進程內可用的模塊
- app
- autoUpdater
- BrowserWindow
- contentTracing
- dialog
- globalShortcut
- ipcMain
- Menu
- MenuItem
- powerMonitor
- powerSaveBlocker
- protocol
- session
- webContents
- Tray
- 在渲染進程(網頁)內可用的模塊
- desktopCapturer
- ipcRenderer
- remote
- webFrame
- 在兩種進程中都可用的模塊
- clipboard
- crashReporter
- nativeImage
- screen
- shell
- 開發
- 代碼規范
- 源碼目錄結構
- 與 NW.js(原 node-webkit)在技術上的差異
- 構建系統概覽
- 構建步驟(OS X)
- 構建步驟(Windows)
- 構建步驟(Linux)
- 在調試中使用 Symbol Server