admin管理员组文章数量:1318563
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 ""
if (request.url.startsWith("")) {
callback("/path/to/file")
}
// otherwise, let the HTTP request behave like normal.
// But how?
})
How do we allow other http
requests other than to continue working as normal?
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."
if (request.url.startsWith("http://example.")) {
callback("/path/to/file")
}
// otherwise, let the HTTP request behave like normal.
// But how?
})
How do we allow other http
requests other than http://example.
to continue working as normal?
-
any idea how i can capture the requests/responses from a
webview
?? ive been stuck on this problem for a while now. here is my question – oldboy Commented Jan 20, 2020 at 7:52
2 Answers
Reset to default 2Not sure if there is a way to do this exactly? but I did something similar which is to use session.defaultSession.webRequest.onBeforeRequest
See: https://developer.mozilla/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest
something like
session.defaultSession.webRequest.onBeforeRequest({urls: ['http://example.']}, function(details, callback) {
callback({
redirectURL: 'file://' + this.getUrl(details.url)
});
});
If you need more than a redirect you could redirect to your own custom protocol (eg. a url like mycustomprotocol://...
). You can implement your own protocol handler with protocol.registerStringProtocol
, etc.
I used both onBeforeRequest and registerStringProtocol separately in electron without issues so far but never both together - should work together though I geuss.
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 :)
Let's use: protocol.uninterceptProptocol(scheme)
protocol.interceptFileProtocol('http', (request, callback) => {
// intercept only requests to "http://example."
if (request.url.startsWith("http://example.")) {
callback("/path/to/file")
}
// otherwise, let the HTTP request behave like normal.
protocol.uninterceptProtocol('http');
})
本文标签:
版权声明:本文标题:javascript - How can we use electron.protocol.interceptFileProtocol with only certain paths, leaving other requests untouched? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742049312a2417977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论