問(wèn)題描述
我想攔截某些 HTTP 請(qǐng)求并將它們替換為文件.所以我想我可以像這樣使用 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?
})
我們?nèi)绾卧试S除 http://example.com
之外的其他 http
請(qǐng)求繼續(xù)正常工作?
How do we allow other http
requests other than http://example.com
to continue working as normal?
推薦答案
在使用 protocol.interceptXXXXProtocol(scheme, handler)
時(shí),我們正在攔截方案協(xié)議,并使用 handler 作為協(xié)議的新處理程序,發(fā)送一個(gè)新的 XXXX 請(qǐng)求作為響應(yīng),如 在此處的文檔中所述一個(gè)>.
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.
但是,這樣做完全破壞了這個(gè)特定協(xié)議的初始處理程序,我們?cè)谔幚砘卣{(diào)執(zhí)行后需要它.因此,我們只需要將它恢復(fù)到初始狀態(tài),它就可以繼續(xù)正常工作:)
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');
})
這篇關(guān)于我們?nèi)绾尾拍苤皇褂锰囟窂降?electron.protocol.interceptFileProtocol ,而不影響其他請(qǐng)求?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!