MediaWiki:Common.js: Difference between revisions
From Hidden Mickey Wiki
No edit summary Tag: Reverted |
No edit summary Tag: Manual revert |
||
| Line 91: | Line 91: | ||
}); | }); | ||
/* | /* Test code to make checkboxes and timestamps work */ | ||
/* This code works for PC Windows */ | |||
(function () { | (function () { | ||
'use strict'; | 'use strict'; | ||
// Toggle this in the console with `window.mwTimestamp.DEBUG = false` to silence logs | |||
window.mwTimestamp = window.mwTimestamp || {}; | |||
window.mwTimestamp.DEBUG = window.mwTimestamp.DEBUG !== undefined ? window.mwTimestamp.DEBUG : true; | |||
var PREFIX = 'mw-checkbox-ts:'; | |||
function dbg() { | |||
if (!window.mwTimestamp.DEBUG) return; | |||
var args = Array.prototype.slice.call(arguments); | |||
args.unshift('mw-timestamp:'); | |||
console.log.apply(console, args); | |||
} | |||
function formatTime(d) { | function formatTime(d) { | ||
return d.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', second: '2-digit' }); | return d.toLocaleTimeString('en-US', { | ||
hour: 'numeric', | |||
minute: '2-digit', | |||
second: '2-digit' | |||
}); | |||
} | } | ||
function | function ensureTsBox(cb) { | ||
if (!cb) { | |||
dbg('ensureTsBox called with falsy checkbox'); | |||
box = document.createElement('span'); | return null; | ||
} | |||
// prefer row-based lookup | |||
var tr = cb.closest && cb.closest('tr'); | |||
if (tr) { | |||
var existing = tr.querySelector && tr.querySelector('.mw-ts-box'); | |||
if (existing) { | |||
dbg('Found existing tsBox for', cb.id); | |||
return existing; | |||
} | |||
// create in last cell if not present | |||
var lastCell = tr.querySelector('td:last-child, th:last-child') || tr.lastElementChild; | |||
if (lastCell) { | |||
var ts = lastCell.querySelector('.mw-ts-box'); | |||
if (!ts) { | |||
ts = document.createElement('span'); | |||
ts.className = 'mw-ts-box'; | |||
lastCell.appendChild(ts); | |||
dbg('Created tsBox in last cell for', cb.id); | |||
} else { | |||
dbg('Found tsBox in last cell for', cb.id); | |||
} | |||
return ts; | |||
} | |||
} | |||
// fallback: next sibling cell | |||
var td = cb.closest && cb.closest('td') || cb.parentElement; | |||
if (td && td.nextElementSibling) { | |||
var next = td.nextElementSibling; | |||
var ts2 = next.querySelector && next.querySelector('.mw-ts-box'); | |||
if (!ts2) { | |||
ts2 = document.createElement('span'); | |||
ts2.className = 'mw-ts-box'; | |||
next.appendChild(ts2); | |||
dbg('Created tsBox in next cell for', cb.id); | |||
} else { | |||
dbg('Found tsBox in next cell for', cb.id); | |||
} | |||
return ts2; | |||
} | } | ||
return | |||
// last resort: insert immediately after checkbox | |||
var span = document.createElement('span'); | |||
span.className = 'mw-ts-box'; | |||
if (cb.parentNode) cb.parentNode.insertBefore(span, cb.nextSibling); | |||
dbg('Inserted tsBox after checkbox for', cb.id); | |||
return span; | |||
} | } | ||
function | function saveState(cb, tsBox) { | ||
if (!cb || !cb.id) { | |||
checked: cb.checked, | dbg('saveState: missing checkbox or id, cannot save'); | ||
timestamp: tsBox(cb). | return; | ||
})); | } | ||
var data = { | |||
checked: !!cb.checked, | |||
timestamp: tsBox ? tsBox.textContent : '' | |||
}; | |||
try { | |||
localStorage.setItem(PREFIX + cb.id, JSON.stringify(data)); | |||
dbg('Saved', cb.id, data); | |||
} catch (e) { | |||
dbg('saveState failed for', cb.id, e); | |||
} | |||
} | } | ||
function | function restoreOne(id, data) { | ||
dbg('Attempting restoreOne for', id, data); | |||
if (! | var cb = document.getElementById(id); | ||
cb.checked = data.checked; | if (!cb) { | ||
tsBox(cb).textContent = data.timestamp || ''; | dbg('No checkbox element found for id', id); | ||
return false; | |||
} | |||
cb.checked = !!data.checked; | |||
var tsBox = ensureTsBox(cb); | |||
if (tsBox) { | |||
tsBox.textContent = data.timestamp || ''; | |||
dbg('Restored timestamp for', id, '→', tsBox.textContent); | |||
} else { | |||
dbg('Failed to create/find tsBox for', id); | |||
} | |||
return true; | |||
} | } | ||
function | function restoreAllOnce() { | ||
dbg('restoreAllOnce: start'); | |||
try { | |||
var keys = Object.keys(localStorage); | |||
keys.forEach(function (k) { | |||
if (k.indexOf(PREFIX) !== 0) return; | |||
var id = k.slice(PREFIX.length); | |||
var json = localStorage.getItem(k); | |||
if (!json) { | |||
dbg('No JSON for key', k); | |||
return; | |||
} | |||
try { | |||
var data = JSON.parse(json); | |||
restoreOne(id, data); | |||
} catch (e) { | |||
dbg('Bad JSON for', k, e); | |||
} | |||
}); | |||
} catch (e) { | |||
dbg('restoreAllOnce failed', e); | |||
} | |||
dbg('restoreAllOnce: done'); | |||
} | } | ||
function initRestore() { | |||
restoreAllOnce(); | |||
if ( | |||
// MediaWiki hook (if available) | |||
if (window.mw && mw.hook) { | |||
try { | |||
mw.hook('wikipage.content').add(function () { | |||
dbg('mw.hook wikipage.content fired'); | |||
restoreAllOnce(); | |||
}); | |||
} catch (e) { | |||
dbg('mw.hook attach failed', e); | |||
} | |||
} | |||
// short retries (handles async injection) | |||
setTimeout(function () { dbg('timeout retry 200ms'); restoreAllOnce(); }, 200); | |||
setTimeout(function () { dbg('timeout retry 1200ms'); restoreAllOnce(); }, 1200); | |||
// MutationObserver fallback | |||
if (window.MutationObserver) { | |||
try { | |||
var observer = new MutationObserver(function (mutations) { | |||
var want = false; | |||
for (var i = 0; i < mutations.length && !want; i++) { | |||
var added = mutations[i].addedNodes; | |||
for (var j = 0; j < added.length && !want; j++) { | |||
var node = added[j]; | |||
if (node.nodeType !== 1) continue; | |||
if (node.matches && node.matches('.mw-checkbox-ts')) want = true; | |||
if (node.querySelector && (node.querySelector('.mw-checkbox-ts') || node.querySelector('.mw-ts-box'))) want = true; | |||
} | |||
} | |||
if (want) { | |||
dbg('MutationObserver detected relevant nodes; restoring'); | |||
restoreAllOnce(); | |||
} | |||
}); | |||
observer.observe(document.body, { childList: true, subtree: true }); | |||
dbg('MutationObserver attached'); | |||
} catch (e) { | |||
dbg('MutationObserver attach failed', e); | |||
} | |||
} | |||
} | |||
// change handler — writes timestamp and saves state | |||
document.addEventListener('change', function (ev) { | |||
var cb = ev.target; | |||
if (!cb || !(cb.matches && cb.matches('.mw-checkbox-ts'))) return; | |||
dbg('change event for', cb.id, 'checked =', cb.checked); | |||
var tsBox = ensureTsBox(cb); | |||
tsBox.textContent = cb.checked ? formatTime(new Date()) : ''; | |||
saveState(cb, tsBox); | |||
}, false); | |||
// Start | |||
if (document.readyState === 'loading') { | if (document.readyState === 'loading') { | ||
document.addEventListener('DOMContentLoaded', | document.addEventListener('DOMContentLoaded', initRestore); | ||
} else { | } else { | ||
initRestore(); | |||
} | } | ||
// Expose debug helpers | |||
window.mwTimestamp.restoreAll = restoreAllOnce; | |||
window.mwTimestamp.listSaved = function () { | |||
return Object.keys(localStorage).filter(function (k) { return k.indexOf(PREFIX) === 0; }); | |||
}; | |||
window.mwTimestamp.getSaved = function (id) { | |||
var j = localStorage.getItem(PREFIX + id); | |||
try { return j ? JSON.parse(j) : null; } catch (e) { dbg('getSaved parse error', e); return null; } | |||
}; | |||
dbg('mw-timestamp initialized. Use window.mwTimestamp.* to inspect.'); | |||
})(); | })(); | ||
Revision as of 13:38, 2 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();
}
});
/* Test code to make checkboxes and timestamps work */
/* This code works for PC Windows */
(function () {
'use strict';
// Toggle this in the console with `window.mwTimestamp.DEBUG = false` to silence logs
window.mwTimestamp = window.mwTimestamp || {};
window.mwTimestamp.DEBUG = window.mwTimestamp.DEBUG !== undefined ? window.mwTimestamp.DEBUG : true;
var PREFIX = 'mw-checkbox-ts:';
function dbg() {
if (!window.mwTimestamp.DEBUG) return;
var args = Array.prototype.slice.call(arguments);
args.unshift('mw-timestamp:');
console.log.apply(console, args);
}
function formatTime(d) {
return d.toLocaleTimeString('en-US', {
hour: 'numeric',
minute: '2-digit',
second: '2-digit'
});
}
function ensureTsBox(cb) {
if (!cb) {
dbg('ensureTsBox called with falsy checkbox');
return null;
}
// prefer row-based lookup
var tr = cb.closest && cb.closest('tr');
if (tr) {
var existing = tr.querySelector && tr.querySelector('.mw-ts-box');
if (existing) {
dbg('Found existing tsBox for', cb.id);
return existing;
}
// create in last cell if not present
var lastCell = tr.querySelector('td:last-child, th:last-child') || tr.lastElementChild;
if (lastCell) {
var ts = lastCell.querySelector('.mw-ts-box');
if (!ts) {
ts = document.createElement('span');
ts.className = 'mw-ts-box';
lastCell.appendChild(ts);
dbg('Created tsBox in last cell for', cb.id);
} else {
dbg('Found tsBox in last cell for', cb.id);
}
return ts;
}
}
// fallback: next sibling cell
var td = cb.closest && cb.closest('td') || cb.parentElement;
if (td && td.nextElementSibling) {
var next = td.nextElementSibling;
var ts2 = next.querySelector && next.querySelector('.mw-ts-box');
if (!ts2) {
ts2 = document.createElement('span');
ts2.className = 'mw-ts-box';
next.appendChild(ts2);
dbg('Created tsBox in next cell for', cb.id);
} else {
dbg('Found tsBox in next cell for', cb.id);
}
return ts2;
}
// last resort: insert immediately after checkbox
var span = document.createElement('span');
span.className = 'mw-ts-box';
if (cb.parentNode) cb.parentNode.insertBefore(span, cb.nextSibling);
dbg('Inserted tsBox after checkbox for', cb.id);
return span;
}
function saveState(cb, tsBox) {
if (!cb || !cb.id) {
dbg('saveState: missing checkbox or id, cannot save');
return;
}
var data = {
checked: !!cb.checked,
timestamp: tsBox ? tsBox.textContent : ''
};
try {
localStorage.setItem(PREFIX + cb.id, JSON.stringify(data));
dbg('Saved', cb.id, data);
} catch (e) {
dbg('saveState failed for', cb.id, e);
}
}
function restoreOne(id, data) {
dbg('Attempting restoreOne for', id, data);
var cb = document.getElementById(id);
if (!cb) {
dbg('No checkbox element found for id', id);
return false;
}
cb.checked = !!data.checked;
var tsBox = ensureTsBox(cb);
if (tsBox) {
tsBox.textContent = data.timestamp || '';
dbg('Restored timestamp for', id, '→', tsBox.textContent);
} else {
dbg('Failed to create/find tsBox for', id);
}
return true;
}
function restoreAllOnce() {
dbg('restoreAllOnce: start');
try {
var keys = Object.keys(localStorage);
keys.forEach(function (k) {
if (k.indexOf(PREFIX) !== 0) return;
var id = k.slice(PREFIX.length);
var json = localStorage.getItem(k);
if (!json) {
dbg('No JSON for key', k);
return;
}
try {
var data = JSON.parse(json);
restoreOne(id, data);
} catch (e) {
dbg('Bad JSON for', k, e);
}
});
} catch (e) {
dbg('restoreAllOnce failed', e);
}
dbg('restoreAllOnce: done');
}
function initRestore() {
restoreAllOnce();
// MediaWiki hook (if available)
if (window.mw && mw.hook) {
try {
mw.hook('wikipage.content').add(function () {
dbg('mw.hook wikipage.content fired');
restoreAllOnce();
});
} catch (e) {
dbg('mw.hook attach failed', e);
}
}
// short retries (handles async injection)
setTimeout(function () { dbg('timeout retry 200ms'); restoreAllOnce(); }, 200);
setTimeout(function () { dbg('timeout retry 1200ms'); restoreAllOnce(); }, 1200);
// MutationObserver fallback
if (window.MutationObserver) {
try {
var observer = new MutationObserver(function (mutations) {
var want = false;
for (var i = 0; i < mutations.length && !want; i++) {
var added = mutations[i].addedNodes;
for (var j = 0; j < added.length && !want; j++) {
var node = added[j];
if (node.nodeType !== 1) continue;
if (node.matches && node.matches('.mw-checkbox-ts')) want = true;
if (node.querySelector && (node.querySelector('.mw-checkbox-ts') || node.querySelector('.mw-ts-box'))) want = true;
}
}
if (want) {
dbg('MutationObserver detected relevant nodes; restoring');
restoreAllOnce();
}
});
observer.observe(document.body, { childList: true, subtree: true });
dbg('MutationObserver attached');
} catch (e) {
dbg('MutationObserver attach failed', e);
}
}
}
// change handler — writes timestamp and saves state
document.addEventListener('change', function (ev) {
var cb = ev.target;
if (!cb || !(cb.matches && cb.matches('.mw-checkbox-ts'))) return;
dbg('change event for', cb.id, 'checked =', cb.checked);
var tsBox = ensureTsBox(cb);
tsBox.textContent = cb.checked ? formatTime(new Date()) : '';
saveState(cb, tsBox);
}, false);
// Start
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initRestore);
} else {
initRestore();
}
// Expose debug helpers
window.mwTimestamp.restoreAll = restoreAllOnce;
window.mwTimestamp.listSaved = function () {
return Object.keys(localStorage).filter(function (k) { return k.indexOf(PREFIX) === 0; });
};
window.mwTimestamp.getSaved = function (id) {
var j = localStorage.getItem(PREFIX + id);
try { return j ? JSON.parse(j) : null; } catch (e) { dbg('getSaved parse error', e); return null; }
};
dbg('mw-timestamp initialized. Use window.mwTimestamp.* to inspect.');
})();