MediaWiki:Common.js: Difference between revisions
From Descendants of Darkness Wiki
No edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
function saveSidebarScrollPosition() { | function saveSidebarScrollPosition() { | ||
var scrollPosition = document.getElementById(sidebarId).scrollTop; | try { | ||
var scrollPosition = document.getElementById(sidebarId).scrollTop; | |||
var currentPage = window.location.href; | |||
console.log('Saving scroll position:', scrollPosition, 'for page:', currentPage); | |||
sessionStorage.setItem(currentPage, scrollPosition); | |||
} catch (error) { | |||
console.error('Error saving scroll position:', error); | |||
} | |||
} | } | ||
function restoreSidebarScrollPosition() { | function restoreSidebarScrollPosition() { | ||
var currentPage = window.location.href; | try { | ||
var currentPage = window.location.href; | |||
var scrollPosition = sessionStorage.getItem(currentPage); | |||
console.log('Restoring scroll position:', scrollPosition, 'for page:', currentPage); | |||
if (scrollPosition !== null) { | |||
var sidebar = document.getElementById(sidebarId); | |||
if (sidebar) { | |||
sidebar.scrollTop = parseInt(scrollPosition); | |||
} else { | |||
console.warn('Sidebar element not found'); | |||
} | |||
} | |||
} catch (error) { | |||
console.error('Error restoring scroll position:', error); | |||
} | } | ||
} | } | ||
| Line 20: | Line 34: | ||
document.addEventListener('visibilitychange', function () { | document.addEventListener('visibilitychange', function () { | ||
if (document.visibilityState === 'hidden') { | if (document.visibilityState === 'hidden') { | ||
console.log('Page visibility changed to hidden'); | |||
saveSidebarScrollPosition(); | saveSidebarScrollPosition(); | ||
} | } | ||
| Line 27: | Line 42: | ||
document.addEventListener('visibilitychange', function () { | document.addEventListener('visibilitychange', function () { | ||
if (document.visibilityState === 'visible') { | if (document.visibilityState === 'visible') { | ||
console.log('Page visibility changed to visible'); | |||
restoreSidebarScrollPosition(); | restoreSidebarScrollPosition(); | ||
} | } | ||
| Line 33: | Line 49: | ||
// Save scroll position when the sidebar is scrolled | // Save scroll position when the sidebar is scrolled | ||
var sidebar = document.getElementById(sidebarId); | var sidebar = document.getElementById(sidebarId); | ||
sidebar.addEventListener('scroll', saveSidebarScrollPosition); | if (sidebar) { | ||
sidebar.addEventListener('scroll', function () { | |||
console.log('Sidebar scrolled'); | |||
saveSidebarScrollPosition(); | |||
}); | |||
} else { | |||
console.warn('Sidebar element not found'); | |||
} | |||
// Restore scroll position when the page is loaded | // Restore scroll position when the page is loaded | ||
window.addEventListener('load', restoreSidebarScrollPosition); | window.addEventListener('load', function () { | ||
console.log('Page loaded'); | |||
restoreSidebarScrollPosition(); | |||
}); | |||
// Log relevant information for debugging | |||
console.log('Browser:', navigator.userAgent); | |||
console.log('JavaScript enabled:', typeof window !== 'undefined'); | |||
console.log('sessionStorage supported:', typeof sessionStorage !== 'undefined'); | |||
})(); | })(); | ||
Revision as of 00:24, 14 April 2024
(function () {
var sidebarId = 'mw-navigation';
function saveSidebarScrollPosition() {
try {
var scrollPosition = document.getElementById(sidebarId).scrollTop;
var currentPage = window.location.href;
console.log('Saving scroll position:', scrollPosition, 'for page:', currentPage);
sessionStorage.setItem(currentPage, scrollPosition);
} catch (error) {
console.error('Error saving scroll position:', error);
}
}
function restoreSidebarScrollPosition() {
try {
var currentPage = window.location.href;
var scrollPosition = sessionStorage.getItem(currentPage);
console.log('Restoring scroll position:', scrollPosition, 'for page:', currentPage);
if (scrollPosition !== null) {
var sidebar = document.getElementById(sidebarId);
if (sidebar) {
sidebar.scrollTop = parseInt(scrollPosition);
} else {
console.warn('Sidebar element not found');
}
}
} catch (error) {
console.error('Error restoring scroll position:', error);
}
}
// Save scroll position when the page is hidden (user navigates away)
document.addEventListener('visibilitychange', function () {
if (document.visibilityState === 'hidden') {
console.log('Page visibility changed to hidden');
saveSidebarScrollPosition();
}
});
// Restore scroll position when the page is visible (user comes back)
document.addEventListener('visibilitychange', function () {
if (document.visibilityState === 'visible') {
console.log('Page visibility changed to visible');
restoreSidebarScrollPosition();
}
});
// Save scroll position when the sidebar is scrolled
var sidebar = document.getElementById(sidebarId);
if (sidebar) {
sidebar.addEventListener('scroll', function () {
console.log('Sidebar scrolled');
saveSidebarScrollPosition();
});
} else {
console.warn('Sidebar element not found');
}
// Restore scroll position when the page is loaded
window.addEventListener('load', function () {
console.log('Page loaded');
restoreSidebarScrollPosition();
});
// Log relevant information for debugging
console.log('Browser:', navigator.userAgent);
console.log('JavaScript enabled:', typeof window !== 'undefined');
console.log('sessionStorage supported:', typeof sessionStorage !== 'undefined');
})();