Created
November 10, 2021 11:35
-
-
Save aztack/a50ab2aa574bdcaa8be98bdbc2572297 to your computer and use it in GitHub Desktop.
Intercept file protocol in electron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<bod> | |
<iframe src="https://domain.com/page.html /> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<!-- custom protocol will bypass same-origin policy check and be intercepted--> | |
<img src="local:///Users/<user>/Pictures/img.png /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the usage example for the interceptor !! 🙏