Skip to content

Instantly share code, notes, and snippets.

@wellwind
Created December 3, 2023 13:54
Show Gist options
  • Save wellwind/f19cf5121ca655f80f9ab2787c02b516 to your computer and use it in GitHub Desktop.
Save wellwind/f19cf5121ca655f80f9ab2787c02b516 to your computer and use it in GitHub Desktop.
Electron override headers
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