Clock2

Show time drag

  1. // ==UserScript==
  2. // @name Clock2
  3. // @description Show time drag
  4. // @author figuccio
  5. // @version 0.8
  6. // @namespace https://greasyfork.org/users/237458
  7. // @match *://*/*
  8. // @noframes
  9. // @grant GM_addStyle
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @grant GM_registerMenuCommand
  13. // @require https://code.jquery.com/jquery-3.6.0.min.js
  14. // @require https://code.jquery.com/ui/1.13.2/jquery-ui.js
  15. // @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
  16. // @license MIT
  17. // ==/UserScript==
  18. (function() {
  19. 'use strict';
  20. const $ = window.jQuery.noConflict();
  21. const body=document.body;
  22.  
  23. var todVisible = true; // Variabile per monitorare se l'orologio è visibile o meno
  24. // Funzione per salvare la posizione dell'orologio nella memoria locale
  25. function saveClockPosition(x, y) {
  26. GM_setValue('clockPosition', JSON.stringify({ x: x, y: y }));
  27. }
  28.  
  29. // Funzione per caricare la posizione dell'orologio dalla memoria locale
  30. function loadClockPosition() {
  31. const savedPosition = GM_getValue('clockPosition');
  32. if (savedPosition) {
  33. const position = JSON.parse(savedPosition);
  34. tod.style.left = position.x + 'px';
  35. tod.style.top = position.y + 'px';
  36. }
  37. }
  38.  
  39. var tod = document.createElement("div");
  40. tod.id = "todClock";
  41.  
  42. tod.setAttribute("style", `
  43. top: 0;
  44. color: black;
  45. font-family: "Droid Sans Mono";
  46. font-size: 16pt;
  47. line-height: 20px;
  48. position: fixed;
  49. text-align: center;
  50. z-index: 99999999999;
  51. background-color: green;
  52. -moz-user-select: none;
  53. cursor: move;
  54. `);
  55. let use12HourFormat = GM_getValue('use12HourFormat', false); // Default è il formato 24 ore
  56. let language = GM_getValue('language') || 'it'; // Recupera la lingua dal localStorage o usa 'it' come predefinita
  57.  
  58. const languages = {
  59. en: { weekday: 'short', month: 'short', day: '2-digit', year: 'numeric' },
  60. it: { weekday: 'short', month: '2-digit', day: '2-digit', year: 'numeric' }
  61. };
  62.  
  63. function tick() {
  64. const now = new Date();
  65. let hours = now.getHours();
  66. const minutes = String(now.getMinutes()).padStart(2, "0");
  67. const seconds = String(now.getSeconds()).padStart(2, "0");
  68. const milliseconds = String(now.getMilliseconds()).padStart(3, "0");
  69. const date = now.toLocaleString(language, languages[language]); // Usa la lingua selezionata per la data
  70.  
  71. let period = "";
  72.  
  73. if (!use12HourFormat) {
  74. period = hours >= 12 ? " PM" : " AM";
  75. hours = hours % 12 || 12; // Converte in formato 12 ore
  76. }
  77.  
  78. hours = String(hours).padStart(2, "0");
  79. tod.textContent =`${date} ${hours}:${minutes}:${seconds}:${milliseconds}${period}`;
  80. }
  81. function toggleLanguage() {
  82. language = language === 'it' ? 'en' : 'it'; // Cambia solo la lingua
  83. GM_setValue('language', language); // Salva la lingua scelta
  84. tick(); // Aggiorna immediatamente la visualizzazione
  85. }
  86. function toggleFormat() {
  87. //Cambia il formato orario
  88. use12HourFormat = !use12HourFormat;
  89. GM_setValue('use12HourFormat', use12HourFormat); // Salva lo stato del formato
  90. }
  91. // Menu commands
  92. GM_registerMenuCommand('Mostra/Nascondi orologio', function() {
  93. todVisible = !todVisible;
  94. $(tod).toggle(todVisible);
  95. });
  96. GM_registerMenuCommand("Cambia formato orario 12/24", toggleFormat);
  97. GM_registerMenuCommand("Cambia lingua", toggleLanguage);
  98. $(tod).draggable({
  99. containment: "window", // Assicura che l'elemento draggable sia confinato alla finestra del browser
  100. stop: function(event, ui) {
  101. saveClockPosition(ui.position.left, ui.position.top);
  102. }
  103. });
  104.  
  105. body.append(tod);
  106.  
  107. loadClockPosition(); // Carica la posizione dell'orologio dalla memoria locale
  108. tick();
  109. setInterval(tick, 70);
  110.  
  111. })();