|
import { sha256 } from '../js/sha256.js'; |
|
|
|
|
|
export async function onRequest(context) { |
|
const { request, env, next } = context; |
|
|
|
|
|
const response = await next(); |
|
|
|
|
|
const contentType = response.headers.get("content-type") || ""; |
|
|
|
if (contentType.includes("text/html")) { |
|
|
|
let html = await response.text(); |
|
|
|
|
|
|
|
const password = env.PASSWORD || ""; |
|
let passwordHash = ""; |
|
if (password) { |
|
passwordHash = await sha256(password); |
|
} |
|
html = html.replace('window.__ENV__.PASSWORD = "{{PASSWORD}}";', |
|
`window.__ENV__.PASSWORD = "${passwordHash}"; // SHA-256 hash`); |
|
|
|
|
|
return new Response(html, { |
|
headers: response.headers, |
|
status: response.status, |
|
statusText: response.statusText, |
|
}); |
|
} |
|
|
|
|
|
return response; |
|
} |