fix unicode handling in content hash generation for hermes introduction caching

This commit is contained in:
Russell Ballestrini 2025-06-30 15:54:53 -04:00
parent fac46e5afa
commit db57965ef4

View file

@ -1478,8 +1478,21 @@ function openHermesModal() {
const pageTitle = document.title;
const pageUrl = window.location.href;
// Create cache key based on page content hash
const contentHash = btoa(pageContent.substring(0, 1000)).replace(/[^a-zA-Z0-9]/g, '').substring(0, 32);
// Create cache key based on page content hash (handle Unicode safely)
const contentForHash = pageContent.substring(0, 1000);
let contentHash;
try {
contentHash = btoa(unescape(encodeURIComponent(contentForHash))).replace(/[^a-zA-Z0-9]/g, '').substring(0, 32);
} catch (e) {
// Fallback: use simple string hash if btoa fails
let hash = 0;
for (let i = 0; i < contentForHash.length; i++) {
const char = contentForHash.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
contentHash = Math.abs(hash).toString(36).substring(0, 32);
}
const cacheKey = `hermes-intro-${contentHash}`;
// Check if we have a cached introduction for this page content