Created
December 3, 2023 13:54
-
-
Save wellwind/f19cf5121ca655f80f9ab2787c02b516 to your computer and use it in GitHub Desktop.
Electron override headers
This file contains hidden or 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 { protocol, app, BrowserWindow } = require("electron"); | |
const { readFileSync, statSync } = require("fs"); | |
const mime = require("mime-types"); | |
const createWindow = async () => { | |
const win = new BrowserWindow({ | |
width: 1920, | |
height: 1080, | |
webPreferences: { | |
webviewTag: true, | |
}, | |
}); | |
protocol.handle("file", (request) => { | |
const filePath = request.url.replace("file://", ""); | |
const response = readFileSync(filePath); | |
const stat = statSync(filePath); | |
const contentType = mime.lookup(filePath); | |
const content = Buffer.from(response).toString("utf-8"); | |
return new Response(content, { | |
headers: { | |
"last-modified": stat.mtime.toUTCString(), | |
"Content-Type": contentType, | |
"Origin-Agent-Cluster": "?1", | |
}, | |
}); | |
}); | |
win.loadFile("index.html"); | |
}; | |
app.whenReady().then(() => { | |
createWindow(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment