Display Ora corrente con Milliseconds and AM/PM

Visualizza l'ora corrente in millisecondi e nel formato AM/PM (2024)

// ==UserScript==
// @name         Display Ora corrente con Milliseconds and AM/PM
// @namespace    https://greasyfork.org/users/237458
// @version      0.2
// @description  Visualizza l'ora corrente in millisecondi e nel formato AM/PM (2024)
// @author       figuccio
// @match        https://*/*
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @require      http://code.jquery.com/jquery-latest.js
// @require      https://code.jquery.com/ui/1.12.1/jquery-ui.js
// @icon         data:image/gif;base64,R0lGODlhEAAQAKECABEREe7u7v///////yH5BAEKAAIALAAAAAAQABAAAAIplI+py30Bo5wB2IvzrXDvaoFcCIBeeXaeSY4tibqxSWt2RuWRw/e+UQAAOw==
// @noframes
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    var $ = window.jQuery;
    function getCurrentTime() {
        const now = new Date();
        let hours = now.getHours();
        const minutes = now.getMinutes();
        const seconds = now.getSeconds();
        const milliseconds = now.getMilliseconds();
        const ampm = hours >= 12 ? 'PM' : 'AM';

        hours = hours % 12;
        hours = hours ? hours : 12; // 12-hour clock

        const timeString = hours.toString().padStart(2, '0') + ':' +
                           minutes.toString().padStart(2, '0') + ':' +
                           seconds.toString().padStart(2, '0') + ':' +
                           milliseconds.toString().padStart(3, '0') + ' ' + ampm;

        return timeString;
    }

    function displayTime() {
        const timeContainer = $('<div id="test"></div>');
        const savedPosition = GM_getValue('timeContainerPosition');
        if (savedPosition) {
            const [top, left] = savedPosition.split(',');
            timeContainer.css({
                'position': 'fixed',
                'top': top,
                'left': left
            });
        } else {
            timeContainer.css({
                'position': 'fixed',
                'top': '10px',
                'left': '10px'
            });
        }
        timeContainer.css({
            'background': 'black',
            'padding': '5px',
            'border': '2px solid gold',
            'borderRadius': '5px',
            'fontFamily': 'Arial, sans-serif',
            'fontSize': '14px',
            'color': 'lime',
            'zIndex': '9999',
            'cursor': 'move',
            'width': '110px '


        });
        $('body').append(timeContainer);

        timeContainer.draggable({
            stop: function() {
                savePosition(timeContainer);
            }
        });

        setInterval(function() {
            timeContainer.text(getCurrentTime());
        }, 100); // Update every 100 milliseconds
    }

    function savePosition(element) {
        const top = element.css('top');
        const left = element.css('left');
        GM_setValue('timeContainerPosition', `${top},${left}`);
    }

    function restorePosition() {
        const savedPosition = GM_getValue('timeContainerPosition');
        if (savedPosition) {
            const [top, left] = savedPosition.split(',');
            const timeContainer = $('#test');
            timeContainer.css({
                'top': top,
                'left': left
            });
        }
    }

    displayTime();
    restorePosition();

    function toggleVisibility() {
        const timeContainer = $('#test');
        if (timeContainer) {
            timeContainer.toggle();
            savePosition(timeContainer);
        }
    }

    GM_registerMenuCommand('mostra pulsante/nascondi', toggleVisibility);
})();