最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can we use electron.protocol.interceptFileProtocol with only certain paths, leaving other requests untouched? -

programmeradmin0浏览0评论

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?

Share Improve this question asked May 1, 2019 at 22:13 trusktrtrusktr 45.5k58 gold badges210 silver badges287 bronze badges 1
  • 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
Add a ment  | 

2 Answers 2

Reset to default 2

Not 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');
})
发布评论

评论列表(0)

  1. 暂无评论