Jump to content

User:Mathnerd314159/displayLibrary.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mathnerd314159 (talk | contribs) at 17:31, 23 June 2025 (new script). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* 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);
        });
    });
});