Skip to content

Instantly share code, notes, and snippets.

@aztack
Created November 10, 2021 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztack/a50ab2aa574bdcaa8be98bdbc2572297 to your computer and use it in GitHub Desktop.
Save aztack/a50ab2aa574bdcaa8be98bdbc2572297 to your computer and use it in GitHub Desktop.
Intercept file protocol in electron
<html>
<bod>
<iframe src="https://domain.com/page.html />
</body>
</html>
const fs = require('fs');
const { protocol } = require('electron');
protocol.interceptFileProtocol('local', (request, callback) => {
// remove custom protocol prefix
const path = request.url.slice('local://'.length);
// use content of local file as response body
callback({path});
// or
//calblack({data: fs.readFileSync(path)})
});
<html>
<body>
<!-- custom protocol will bypass same-origin policy check and be intercepted-->
<img src="local:///Users/<user>/Pictures/img.png />
</body>
</html>
@ajoslin103
Copy link

Thank you for the usage example for the interceptor !! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment