INNER CODE UNIT · JavaScript
detectCharsetFromHeaders
EdgeTypE/better-deepseek · src/background/index.js:442
function detectCharsetFromHeaders(resp) {
const contentType = resp.headers.get("content-type");
if (!contentType) return null;
const match = contentType.match(/charset\s*=\s*([^\s;]+)/i);
return match ? match[1].trim().replace(/^["']|["']$/g, "") : null;
}
function detectCharsetFromHtml(buffer) {
const scanView = new TextDecoder("latin1").decode(buffer.slice(0, 10240));
let match = scanView.match(/<meta[\s>][^>]*charset\s*=\s*["']?\s*([a-zA-Z0-9_-]+)\s*["']?[^>]*\/?>/i);
if (match) return match[1];
match = scanView.match(/<meta\s+http-equiv\s*=\s*["']?\s*Content-Type\s*["']?\s*content\s*=\s*["'][^"']*charset\s*=\s*([a-zA-Z0-9_-]+)/i);
if (match) return match[1];
return null;
}