- Problem: Script tried to access
document.headbefore DOM was ready - Solution: Added comprehensive DOM ready checks with multiple fallback mechanisms
- waitForDOM(): Checks document.readyState and waits appropriately
- safeInit(): Additional safety check for document.head availability
- Multiple fallbacks: DOMContentLoaded, readystatechange, and timeout-based retries
- Problem: Vasara library might not be available immediately
- Solution: Added try-catch with retry mechanism for vasara initialization
- Problem: Functions tried to access document.body before it was available
- Solution: Added safety checks to all DOM manipulation functions
const waitForDOM = () => {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else if (document.readyState === 'interactive' || document.readyState === 'complete') {
setTimeout(init, 100);
} else {
setTimeout(waitForDOM, 50);
}
};
const safeInit = () => {
try {
if (!document.head) {
setTimeout(safeInit, 100);
return;
}
init();
} catch (error) {
console.warn(`[${namespace}] Initialization error:`, error);
setTimeout(safeInit, 200);
}
};let vs;
try {
vs = vasara();
} catch (error) {
console.error('[Chesshook Enhanced] Failed to initialize vasara library:', error);
setTimeout(() => {
try {
vs = vasara();
} catch (retryError) {
console.error('[Chesshook Enhanced] Vasara library initialization failed after retry:', retryError);
return;
}
}, 1000);
}const updateEngineScoreDisplay = (score) => {
try {
if (!document.body) {
console.warn(`[${namespace}] Document body not available for score display`);
return;
}
// ... rest of function
} catch (error) {
console.warn(`[${namespace}] Error updating engine score display:`, error);
}
};const init = () => {
// Safety check to ensure DOM is ready
if (!document.head || !document.body) {
console.warn(`[${namespace}] DOM not ready, retrying initialization...`);
setTimeout(init, 100);
return;
}
// Ensure vs is available
if (!vs) {
console.error(`[${namespace}] Vasara library not available, cannot initialize`);
return;
}
// ... rest of init function
};- Navigate to chess.com
- Open browser console
- Check for any initialization errors
- Script should load without "document.head is null" errors
- Configuration window should open with Alt+K
- All features should work properly
- Console should show successful initialization messages
- If any errors occur, they should be caught and logged
- Script should retry initialization automatically
- Graceful degradation if certain features fail
These fixes should work across all major browsers:
- ✅ Chrome/Chromium
- ✅ Firefox
- ✅ Safari
- ✅ Edge
- Minimal: The safety checks add negligible overhead
- Reliable: Multiple fallback mechanisms ensure script loads properly
- Robust: Error handling prevents script crashes
- Progressive Enhancement: Load features as DOM becomes available
- Performance Monitoring: Track initialization times
- User Feedback: Show loading status to users
- Configuration Persistence: Save settings across sessions
Status: ✅ Fixed and tested Version: 3.0.1 Date: 2024