LibreTV / functions /_middleware.js
DD
Upload 47 files
f11ad89 verified
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}";`);
return new Response(html, {
headers: response.headers,
status: response.status,
statusText: response.statusText,
});
}
return response;
}