'How to access network requests in Electron
I need to redirect network requests (ES6 imports and fetch calls) to a different URL, when they match a regex.
I tried using WebRequest, which seems like it should work, but the callback is never invoked. It seems to only apply to navigation requests (that would load a new page).
I tried simply blocking every request with this code (executed in the main process):
const { session } = require("electron");
const handler = (details, callback) => callback({cancel: true});
session.defaultSession.webRequest.onBeforeRequest({urls: ["*://*/*"]}, handler);
When I then tried using imports and fetch (in the renderer process), the requests were unaffected.
Solution 1:[1]
Turns out that the code does work, except that Electron needs to be restarted (not just refreshed), before the edits take effect, as this is happening in the Main Process (not the Renderer).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
