diff --git a/py_app/app/static/js/storage-utils.js b/py_app/app/static/js/storage-utils.js new file mode 100644 index 0000000..ec479fe --- /dev/null +++ b/py_app/app/static/js/storage-utils.js @@ -0,0 +1,40 @@ +/** + * Safe localStorage wrapper to handle tracking prevention errors + * This prevents console errors when browser tracking prevention blocks storage access + */ +const safeStorage = { + getItem: function(key) { + try { + return localStorage.getItem(key); + } catch (e) { + console.warn('localStorage access blocked:', e.message); + return null; + } + }, + setItem: function(key, value) { + try { + localStorage.setItem(key, value); + } catch (e) { + console.warn('localStorage access blocked:', e.message); + } + }, + removeItem: function(key) { + try { + localStorage.removeItem(key); + } catch (e) { + console.warn('localStorage access blocked:', e.message); + } + }, + clear: function() { + try { + localStorage.clear(); + } catch (e) { + console.warn('localStorage access blocked:', e.message); + } + } +}; + +// Export for use in other scripts +if (typeof window !== 'undefined') { + window.safeStorage = safeStorage; +} diff --git a/py_app/app/static/script.js b/py_app/app/static/script.js index 484044a..dbc6ea4 100644 --- a/py_app/app/static/script.js +++ b/py_app/app/static/script.js @@ -1,3 +1,29 @@ +// Safe localStorage wrapper to handle tracking prevention +const safeStorage = { + getItem: function(key) { + try { + return localStorage.getItem(key); + } catch (e) { + console.warn('localStorage access blocked:', e); + return null; + } + }, + setItem: function(key, value) { + try { + localStorage.setItem(key, value); + } catch (e) { + console.warn('localStorage access blocked:', e); + } + }, + removeItem: function(key) { + try { + localStorage.removeItem(key); + } catch (e) { + console.warn('localStorage access blocked:', e); + } + } +}; + document.addEventListener('DOMContentLoaded', () => { const reportButtons = document.querySelectorAll('.report-btn'); const reportTitle = document.getElementById('report-title'); @@ -17,7 +43,7 @@ document.addEventListener('DOMContentLoaded', () => { } // Check and apply the saved theme from localStorage - const savedTheme = localStorage.getItem('theme'); + const savedTheme = safeStorage.getItem('theme'); if (savedTheme) { body.classList.toggle('dark-mode', savedTheme === 'dark'); } @@ -28,7 +54,7 @@ document.addEventListener('DOMContentLoaded', () => { // Toggle the theme on button click themeToggleButton.addEventListener('click', () => { const isDarkMode = body.classList.toggle('dark-mode'); - localStorage.setItem('theme', isDarkMode ? 'dark' : 'light'); + safeStorage.setItem('theme', isDarkMode ? 'dark' : 'light'); updateThemeToggleButtonText(); // Update the button text after toggling }); diff --git a/py_app/app/templates/base.html b/py_app/app/templates/base.html index 9a52421..08668a2 100644 --- a/py_app/app/templates/base.html +++ b/py_app/app/templates/base.html @@ -65,6 +65,8 @@