Skip to content

fix: resolve console errors and improve data recovery (v2.5.1) #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.5.1] - {PR_MERGE_DATE}

### Fixed

- **Console Errors**: Resolved JavaScript errors that appeared in browser console
- Fixed JSON parsing errors in statistics compression/decompression with proper regex escaping
- Added fallback parsing and default structure for corrupted localStorage data
- Implemented automatic detection and cleanup of corrupted localStorage items
- **Browser Cache Issues**: Added console guidance for users experiencing autoPip.js MediaSession errors
- Provides clear instructions for cache clearing and localStorage reset
- Helps resolve stale browser cache references to non-existent files

### Enhanced

- **Data Recovery**: Improved robustness of data loading and error handling
- Enhanced statistics data compression with proper special character handling
- Added graceful fallbacks for corrupted or malformed data
- Automatic cleanup prevents persistent error states

## [2.5.0] - 2025-07-13

### Added
Expand Down Expand Up @@ -259,7 +278,7 @@ When adding entries to this changelog:

## Version History Summary

- **v2.5.x**: Enhanced navigation system with keyboard shortcuts, progress sidebar, and history
- **v2.5.x**: Enhanced navigation system with keyboard shortcuts, progress sidebar, history, and console error fixes
- **v2.4.x**: Advanced search and filtering system with smart auto-completion
- **v2.3.x**: Toolbar visibility toggle and UI declutter option
- **v2.2.x**: Changelog functionality and improvements
Expand Down
55 changes: 51 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,33 @@ function decompressData(compressedData) {
let decompressed = compressedData;
for (const [compressed, original] of Object.entries(decompressionMap)) {
decompressed = decompressed.replace(
new RegExp(compressed, "g"),
new RegExp(compressed.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "g"),
original
);
}

return JSON.parse(decompressed);
} catch (error) {
devError("Error decompressing data:", error);
// Try to parse as regular JSON if decompression fails
return JSON.parse(compressedData);
// If decompression fails, try to parse as regular JSON
try {
return JSON.parse(compressedData);
} catch (parseError) {
devError("Error parsing as regular JSON:", parseError);
// Return default statistics structure if all else fails
return {
sessions: [],
currentSession: null,
totalStats: {
totalQuestions: 0,
totalCorrect: 0,
totalIncorrect: 0,
totalPreview: 0,
totalTime: 0,
examStats: {},
}
};
}
}
}

Expand All @@ -439,6 +456,26 @@ function saveStatistics() {
}

// Statistics storage management
function clearCorruptedData() {
try {
const items = ['examViewerStatistics', 'examViewerSettings', 'examViewerFavorites'];
items.forEach(item => {
const data = localStorage.getItem(item);
if (data) {
try {
JSON.parse(data);
} catch (e) {
devError(`Corrupted ${item} detected, clearing...`);
localStorage.removeItem(item);
showError(`Corrupted data cleared: ${item}. Please refresh the page.`);
}
}
});
} catch (error) {
devError("Error clearing corrupted data:", error);
}
}

function loadStatistics() {
try {
const savedStats = localStorage.getItem("examViewerStatistics");
Expand Down Expand Up @@ -4694,7 +4731,17 @@ function navigateToQuestionIndex(newIndex, addToHistory = true) {
document.addEventListener("DOMContentLoaded", async function () {
devLog("DOM loaded, initializing application...");

// Load saved data
// Add console message for users experiencing autoPip.js errors
if (isDevelopmentMode()) {
console.info("%cIf you see autoPip.js MediaSession errors:", "font-weight: bold; color: #007bff;");
console.info("1. Clear browser cache and hard refresh (Ctrl+Shift+R / Cmd+Shift+R)");
console.info("2. Or clear localStorage with: localStorage.clear()");
}

// Check for and clear corrupted localStorage data
clearCorruptedData();

// Load saved data with recovery
loadSettings();
loadStatistics();
loadFavorites();
Expand Down