問題描述
我想攔截某些 HTTP 請求并將它們替換為文件.所以我想我可以像這樣使用 electron.protocol.interceptFileProtocol
:
I'd like to intercept certain HTTP requests and replace them with files. So I thought I could use electron.protocol.interceptFileProtocol
like so:
protocol.interceptFileProtocol('http', (request, callback) => {
// intercept only requests to "http://example.com"
if (request.url.startsWith("http://example.com")) {
callback("/path/to/file")
}
// otherwise, let the HTTP request behave like normal.
// But how?
})
我們如何允許除 http://example.com
之外的其他 http
請求繼續正常工作?
How do we allow other http
requests other than http://example.com
to continue working as normal?
推薦答案
在使用 protocol.interceptXXXXProtocol(scheme, handler)
時,我們正在攔截方案協議,并使用 handler 作為協議的新處理程序,發送一個新的 XXXX 請求作為響應,如 在此處的文檔中所述一個>.
When using protocol.interceptXXXXProtocol(scheme, handler)
, we are intercepting scheme protocol and uses handler as the protocol’s new handler which sends a new XXXX request as a response, as said in the doc here.
但是,這樣做完全破壞了這個特定協議的初始處理程序,我們在處理回調執行后需要它.因此,我們只需要將它恢復到初始狀態,它就可以繼續正常工作:)
However, doing so totally breaks the initial handler for this specific protocol, which we would need after handling the callback execution. Thus, we just need to restore it back to its initial state, so that it can continue working as normal :)
讓我們使用:protocol.uninterceptProptocol(scheme)
protocol.interceptFileProtocol('http', (request, callback) => {
// intercept only requests to "http://example.com"
if (request.url.startsWith("http://example.com")) {
callback("/path/to/file")
}
// otherwise, let the HTTP request behave like normal.
protocol.uninterceptProtocol('http');
})
這篇關于我們如何才能只使用特定路徑的 electron.protocol.interceptFileProtocol ,而不影響其他請求?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!