User:Mathnerd314159/displayLibrary.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Mathnerd314159/displayLibrary. |
/* Helper for the "10+ edits in past 30 days" criterion for the Wikipedia Library. Displays how long you can go without editing until access expires. */
$(document).ready(function () {
// Ensure that mediawiki.user and mediawiki.api modules are loaded before proceeding.
mw.loader.using(['mediawiki.user', 'mediawiki.api'], function() {
// Get the current username from MediaWiki configuration.
const username = mw.config.get('wgUserName');
// Check if a username is available. If not, there's no user to query.
if (!username) {
console.log('No user logged in or username not available.');
return;
}
// Initialize a new MediaWiki API object.
(new mw.Api()).get({
action: 'query', // Specify the action as 'query'
list: 'usercontribs', // Request a list of user contributions
ucuser: username, // Specify the user whose contributions to query
uclimit: 10, // Limit the results to the 10 most recent contributions
ucprop: 'timestamp' // Request the timestamp property for each contribution
}).done(function(result) {
// Check if the query was successful and user contributions were returned.
if (result.query && result.query.usercontribs && result.query.usercontribs.length >= 10) {
// The 10th most recent edit is at index 9 in a 0-based array.
const tenthEdit = result.query.usercontribs[9];
const tenthEditTimestamp = tenthEdit.timestamp;
// Create a Date object from the timestamp of the 10th edit.
// MediaWiki timestamps are typically in ISO 8601 format.
const originalDate = new Date(tenthEditTimestamp);
// Add 30 days to the original date to get the target date.
// setDate() handles month and year rollovers automatically.
originalDate.setDate(originalDate.getDate() + 30);
// Get the current date, set to midnight for accurate day comparison.
const now = new Date();
// Calculate the difference in milliseconds.
const diffMs = originalDate.getTime() - now.getTime();
// Convert milliseconds to days.
// Divide by milliseconds in a day (1000 ms/s * 60 s/min * 60 min/hr * 24 hr/day).
// Round to tenths
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24) * 10) / 10;
let countdownText = '';
if (diffDays <= 0) {
countdownText = 'Edit today!';
} else {
countdownText = `${diffDays} day${diffDays === 1 ? '' : 's'} left`;
}
// Find the 'My contributions' list item element.
const contribsListItem = document.getElementById('pt-mycontris');
if (contribsListItem) {
const countdownEl = document.createElement('div');
countdownEl.textContent = countdownText;
// countdownEl.style.fontSize = '0.8em'; // Optional: make text slightly smaller
contribsListItem.append(countdownEl);
}
} else {
console.log('Could not retrieve 10th most recent edit, or user has less than 10 edits.');
}
}).fail(function(jqXHR, textStatus, errorThrown) {
// Log any errors that occur during the API request.
console.error('MediaWiki API request failed:', textStatus, errorThrown);
});
});
});