Candidate Vote Difference with Formatting and Styling

Calculate and display vote difference between two candidates with formatted numbers, improved styling, and time

  1. // ==UserScript==
  2. // @name Candidate Vote Difference with Formatting and Styling
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Calculate and display vote difference between two candidates with formatted numbers, improved styling, and time
  6. // @author You
  7. // @match https://prezenta.roaep.ro/prezidentiale24112024/pv/romania/results/
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Function to extract candidate data
  15. function extractData() {
  16. // Candidates you're interested in
  17. const candidates = ["ION-MARCEL CIOLACU", "ELENA-VALERICA LASCONI"];
  18.  
  19. // Wait for the page to load properly and then execute the logic
  20. const rows = document.querySelectorAll("tr");
  21.  
  22. let candidateVotes = {};
  23.  
  24. rows.forEach(row => {
  25. const nameElement = row.querySelector("span");
  26. if (nameElement) {
  27. const candidateName = nameElement.innerText.trim();
  28.  
  29. // If it's one of the candidates we're tracking
  30. if (candidates.includes(candidateName)) {
  31. const numbers = row.querySelectorAll("span");
  32. if (numbers.length >= 2) {
  33. const votes = parseInt(numbers[1].innerText.trim().replace(/\D/g, '')); // Remove non-numeric characters
  34. candidateVotes[candidateName] = votes;
  35. }
  36. }
  37. }
  38. });
  39.  
  40. // Check if both candidates' votes were found
  41. if (candidateVotes["ION-MARCEL CIOLACU"] && candidateVotes["ELENA-VALERICA LASCONI"]) {
  42. const ciolacuVotes = candidateVotes["ION-MARCEL CIOLACU"];
  43. const lasconiVotes = candidateVotes["ELENA-VALERICA LASCONI"];
  44. const voteDifference = ciolacuVotes - lasconiVotes;
  45.  
  46. // Display the result on the page
  47. displayResult(ciolacuVotes, lasconiVotes, voteDifference);
  48. }
  49. }
  50.  
  51. // Function to format numbers with a dot as a thousands separator
  52. function formatNumber(number) {
  53. return number.toLocaleString('en-GB'); // 'en-GB' uses a dot as the thousands separator
  54. }
  55.  
  56. // Function to get the current time in a readable format
  57. function getCurrentTime() {
  58. const now = new Date();
  59. return now.toLocaleTimeString(); // Returns time in the format HH:MM:SS AM/PM
  60. }
  61.  
  62. // Function to display the result on the page
  63. function displayResult(ciolacuVotes, lasconiVotes, voteDifference) {
  64. // Create a div element to show the result
  65. const resultDiv = document.createElement("div");
  66. resultDiv.style.position = "fixed";
  67. resultDiv.style.top = "10px";
  68. resultDiv.style.right = "10px";
  69. resultDiv.style.backgroundColor = "#333"; // Dark background for better readability
  70. resultDiv.style.color = "#fff"; // White text for contrast
  71. resultDiv.style.borderRadius = "15px"; // Rounded corners
  72. resultDiv.style.padding = "20px"; // Padding around the content
  73. resultDiv.style.boxShadow = "0 4px 8px rgba(0, 0, 0, 0.2)"; // Subtle shadow for depth
  74. resultDiv.style.fontFamily = "'Arial', sans-serif"; // Clean font style
  75. resultDiv.style.zIndex = "9999";
  76. resultDiv.style.fontSize = "16px"; // Slightly larger text for better readability
  77. resultDiv.style.maxWidth = "300px"; // Limit the width for better layout
  78.  
  79. resultDiv.innerHTML = `
  80. <h3 style="font-size: 18px; font-weight: bold;">Vote Difference:</h3>
  81. <p style="color: rgb(201, 35, 43);">ION-MARCEL CIOLACU Votes: <strong>${formatNumber(ciolacuVotes)}</strong></p>
  82. <p style="color: rgb(45, 111, 163);">ELENA-VALERICA LASCONI Votes: <strong>${formatNumber(lasconiVotes)}</strong></p>
  83. <p>Vote Difference: <strong>${formatNumber(voteDifference)}</strong></p>
  84. <p style="font-size: 14px;">Last Updated: <strong>${getCurrentTime()}</strong></p>
  85. `;
  86.  
  87. // Append the result div to the body
  88. document.body.appendChild(resultDiv);
  89. }
  90.  
  91. // Wait for the page to load and then extract data
  92. window.addEventListener('load', () => {
  93. setTimeout(extractData, 3000); // Wait a few seconds for the page to fully load
  94. });
  95. })();