MediaWiki:Common.js: Difference between revisions
From Hidden Mickey Wiki
No edit summary Tags: Manual revert Reverted |
No edit summary Tag: Reverted |
||
| Line 92: | Line 92: | ||
}); | }); | ||
// Code that should work on all platforms | |||
$(document).ready(function() { | $(document).ready(function() { | ||
// Save checkbox state and timestamp to localStorage | // Save checkbox state and timestamp to localStorage | ||
function saveCheckboxState() { | function saveCheckboxState() { | ||
$('input[type="checkbox"]').each(function() { | $('input[type="checkbox"]').each(function() { | ||
const id = | const id = this.id; | ||
localStorage.setItem(id, | localStorage.setItem(id, this.checked); | ||
if (this.checked) { | |||
if | |||
const timestamp = new Date().toISOString(); | const timestamp = new Date().toISOString(); | ||
console.log('Saving timestamp for:', id, timestamp); // Debug log | |||
localStorage.setItem(id + '_timestamp', timestamp); | localStorage.setItem(id + '_timestamp', timestamp); | ||
} else { | } else { | ||
| Line 108: | Line 109: | ||
} | } | ||
// Load checkbox state and timestamp | // Load checkbox state and display timestamp | ||
function loadCheckboxState() { | function loadCheckboxState() { | ||
$('input[type="checkbox"]').each(function() { | $('input[type="checkbox"]').each(function() { | ||
const id = | const id = this.id; | ||
const savedState = localStorage.getItem(id); | const savedState = localStorage.getItem(id); | ||
if (savedState !== null) { | if (savedState !== null) { | ||
$(this).prop('checked', savedState === 'true'); | $(this).prop('checked', savedState === 'true'); | ||
} | } | ||
const savedTimestamp = localStorage.getItem(id + '_timestamp'); | const savedTimestamp = localStorage.getItem(id + '_timestamp'); | ||
$(this).siblings('.checkbox-timestamp').remove(); // | $(this).siblings('.checkbox-timestamp').remove(); // Remove old timestamps | ||
if (savedTimestamp && savedState === 'true') { | if (savedTimestamp && savedState === 'true') { | ||
$(this).after( | $(this).after( | ||
| Line 128: | Line 128: | ||
} | } | ||
// | // Initial load | ||
loadCheckboxState(); | loadCheckboxState(); | ||
// | // Attach change event with debug logging and save logic | ||
$('input[type="checkbox"]').change(function() { | $('input[type="checkbox"]').change(function() { | ||
const id = this.id; | |||
console.log('Checkbox changed:', id, this.checked); // Debug log | |||
saveCheckboxState(); | saveCheckboxState(); | ||
loadCheckboxState(); | loadCheckboxState(); | ||
}); | }); | ||
}); | }); | ||
Revision as of 15:43, 22 October 2025
/* Any JavaScript here will be loaded for all users on every page load. */
// JavaScript code to save checkbox state and restore it when the page loads
$(document).ready(function() {
// Function to save the state of checkboxes to localStorage
function saveCheckboxState() {
$('input[type="checkbox"]').each(function() {
localStorage.setItem($(this).attr('id'), $(this).prop('checked'));
});
}
// Function to load the state of checkboxes from localStorage
function loadCheckboxState() {
$('input[type="checkbox"]').each(function() {
const savedState = localStorage.getItem($(this).attr('id'));
if (savedState !== null) {
$(this).prop('checked', savedState === 'true');
}
});
}
// Load the saved checkbox state when the page is loaded
loadCheckboxState();
// Save the checkbox state whenever a checkbox is changed
$('input[type="checkbox"]').change(function() {
saveCheckboxState();
});
});
// Adjust the search box width
$(document).ready(function () {
$('#searchInput').css('width', '600px'); // Adjust width as needed
});
// Add Edit Source to user dropdown
mw.loader.using('mediawiki.util', function () {
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'edit' } ), 'Edit Source', 'pt-editsource' );
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'history' } ), 'View History', 'pt-history' );
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'delete' } ), 'Delete', 'pt-delete' );
var moveLink = document.getElementById('ca-move');
if (moveLink) {
var a = moveLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-move', a.title || 'Move this page' );
moveLink.remove();
}
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'protect' } ), 'Protect', 'pt-protect' );
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'unwatch' } ), 'Unwatch', 'pt-unwatch' );
var talkLink = document.getElementById('pt-mytalk');
talkLink.remove();
var whatLinksHereLink = document.getElementById('t-whatlinkshere');
if (whatLinksHereLink) {
var a = whatLinksHereLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-whatlinkshere', a.title || 'What Links Here' );
whatLinksHereLink.remove();
}
var relatedChangesLink = document.getElementById('t-recentchangeslinked');
if (relatedChangesLink) {
var a = relatedChangesLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-recentchanges', a.title || 'Recent Changes' );
relatedChangesLink.remove();
}
var uploadLink = document.getElementById('t-upload');
if (uploadLink) {
var a = uploadLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-upload', a.title || 'Upload File' );
uploadLink.remove();
}
var specialPagesLink = document.getElementById('t-specialpages');
if (specialPagesLink) {
var a = specialPagesLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-specialpages', a.title || 'Special Pages' );
specialPagesLink.remove();
}
var permanentLink = document.getElementById('t-permalink');
if (permanentLink) {
var a = permanentLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-permalink', a.title || 'Permanent Link' );
permanentLink.remove();
}
var pageInfoLink = document.getElementById('t-info');
if (pageInfoLink) {
var a = pageInfoLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-info', a.title || 'Page Info' );
pageInfoLink.remove();
}
var printLink = document.getElementById('t-print');
if (printLink) {
var a = printLink.querySelector('a');
printLink.remove();
}
});
// Code that should work on all platforms
$(document).ready(function() {
// Save checkbox state and timestamp to localStorage
function saveCheckboxState() {
$('input[type="checkbox"]').each(function() {
const id = this.id;
localStorage.setItem(id, this.checked);
if (this.checked) {
const timestamp = new Date().toISOString();
console.log('Saving timestamp for:', id, timestamp); // Debug log
localStorage.setItem(id + '_timestamp', timestamp);
} else {
localStorage.removeItem(id + '_timestamp');
}
});
}
// Load checkbox state and display timestamp
function loadCheckboxState() {
$('input[type="checkbox"]').each(function() {
const id = this.id;
const savedState = localStorage.getItem(id);
if (savedState !== null) {
$(this).prop('checked', savedState === 'true');
}
const savedTimestamp = localStorage.getItem(id + '_timestamp');
$(this).siblings('.checkbox-timestamp').remove(); // Remove old timestamps
if (savedTimestamp && savedState === 'true') {
$(this).after(
'<span class="checkbox-timestamp"> (Checked on: ' +
new Date(savedTimestamp).toLocaleString() + ')</span>'
);
}
});
}
// Initial load
loadCheckboxState();
// Attach change event with debug logging and save logic
$('input[type="checkbox"]').change(function() {
const id = this.id;
console.log('Checkbox changed:', id, this.checked); // Debug log
saveCheckboxState();
loadCheckboxState();
});
});