Age Converter

Converts age in days to years, months, and days format with enhanced styling on Torn.com.

当前为 2024-09-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Age Converter
  3. // @description Converts age in days to years, months, and days format with enhanced styling on Torn.com.
  4. // @version 2.0
  5. // @namespace https://github.com/skillerious
  6. // @license MIT
  7. // @match https://www.torn.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. // JavaScript to dynamically add CSS to the document
  12. const style = document.createElement('style');
  13. style.textContent = `
  14. .cont.bottom-round.cont-gray {
  15. min-height: 420px; /* Further increase height to accommodate more content */
  16. padding-bottom: 20px; /* Add more padding at the bottom */
  17. }
  18. .cool-converted-age {
  19. font-size: 11px; /* Set the font size */
  20. color: #0092CD; /* Blue color */
  21. text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); /* Adds a shadow effect */
  22. text-align: center;
  23. margin-top: 10px;
  24. font-family: 'Arial', sans-serif; /* Use a modern font */
  25. letter-spacing: 1px; /* Space out the letters a bit */
  26. }
  27. `;
  28. document.head.appendChild(style);
  29.  
  30. // Function to calculate and display converted age
  31. function convertAndDisplayAge() {
  32. const digitElements = document.querySelectorAll('.box-info.age .box-value .digit');
  33. console.log('Digit Elements:', digitElements); // Log the elements to check if they are correctly selected
  34.  
  35. if (digitElements && digitElements.length > 0) {
  36. let ageInDaysStr = '';
  37. digitElements.forEach(digitElement => {
  38. const digitText = digitElement.textContent.trim();
  39. console.log('Digit Text:', digitText); // Log each digit text
  40. ageInDaysStr += digitText;
  41. });
  42.  
  43. console.log('Age in Days String:', ageInDaysStr); // Log the concatenated string
  44. const ageInDays = parseInt(ageInDaysStr);
  45.  
  46. if (!isNaN(ageInDays)) {
  47. const years = Math.floor(ageInDays / 365);
  48. const months = Math.floor((ageInDays % 365) / 30);
  49. const days = ageInDays % 30;
  50.  
  51. console.log(`Converted Age: Y: ${years}, M: ${months}, D: ${days}`); // Log the converted age
  52.  
  53. const ageConverted = document.createElement('div');
  54. ageConverted.className = 'cool-converted-age'; // Apply the new CSS class for cool styling
  55. ageConverted.textContent = `${years} Years, ${months} Months, ${days} Days`;
  56.  
  57. const ageContainer = document.querySelector('.box-info.age');
  58. if (ageContainer) {
  59. ageContainer.appendChild(ageConverted);
  60. console.log('Age converted element added to the container'); // Confirm the addition
  61. } else {
  62. console.error('Age container not found'); // Log error if container is not found
  63. }
  64. } else {
  65. console.error('Unable to parse age from text content'); // Log error if parsing fails
  66. }
  67. } else {
  68. console.error('Age digit elements not found'); // Log error if elements are not found
  69. }
  70. }
  71.  
  72. // Function to observe changes in the DOM and run the script when the elements are loaded
  73. function observeDOMChanges() {
  74. const observer = new MutationObserver((mutationsList, observer) => {
  75. const ageContainer = document.querySelector('.box-info.age');
  76. const digitElements = document.querySelectorAll('.box-info.age .box-value .digit');
  77. if (ageContainer && digitElements.length > 0) {
  78. observer.disconnect(); // Stop observing once the elements are found
  79. convertAndDisplayAge(); // Call the function to convert and display age
  80. } else {
  81. console.log('Waiting for age container and digits to appear...');
  82. }
  83. });
  84.  
  85. // Observe changes in the document body
  86. observer.observe(document.body, { childList: true, subtree: true });
  87. }
  88.  
  89. // Start observing the DOM changes once the page is loaded
  90. window.addEventListener('load', function () {
  91. observeDOMChanges();
  92. });