tru*_*ktr 8 javascript electron
我想拦截某些 HTTP 请求并用文件替换它们。所以我想我可以这样使用electron.protocol.interceptFileProtocol:
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?
})
Run Code Online (Sandbox Code Playgroud)
http除了http://example.com继续正常工作之外,我们如何允许其他请求?
不确定是否有办法准确地做到这一点?但我做了类似的事情,即使用session.defaultSession.webRequest.onBeforeRequest
参见: https: //developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest
就像是
session.defaultSession.webRequest.onBeforeRequest({urls: ['http://example.com']}, function(details, callback) {
callback({
redirectURL: 'file://' + this.getUrl(details.url)
});
});
Run Code Online (Sandbox Code Playgroud)
如果您需要的不仅仅是重定向,您可以重定向到您自己的自定义协议(例如,类似 的 url mycustomprotocol://...)。您可以使用protocol.registerStringProtocol等来实现您自己的协议处理程序。
我在电子中分别使用了 onBeforeRequest 和 registerStringProtocol ,到目前为止没有出现任何问题,但从来没有同时使用过 - 尽管我猜测应该可以一起工作。
| 归档时间: |
|
| 查看次数: |
2382 次 |
| 最近记录: |