Guest User

Oncheures v0.2

a guest
Mar 18th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Oncheures
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Affichez les heures des posts
  6. // @author Annapurna
  7. // @match https://onche.org/topic/*
  8. // ==/UserScript==
  9.  
  10. //////////////// HISTORIQUE
  11. // 15.03
  12. // 0.1 : Heure du post
  13. // 0.2 : Heure maintenant affichée sous le pseudo, style JVC, copie dans le presse papier et scrolle jusqu'au message. Heure en bas à droite supprimée
  14.  
  15.  
  16. ///////////////// CODE, ne pas modifier
  17.  
  18. // Get all message elements with class 'message'
  19. const messages = document.querySelectorAll('.message');
  20.  
  21. // Get the href content of the first occurrence of the class 'active'
  22. const hrefContent = document.querySelector('.active').href;
  23.  
  24. // Loop through each message
  25. messages.forEach(message => {
  26. // Find the element with class 'message-date' within this message
  27. const messageDateElement = message.querySelector('.message-date');
  28.  
  29. // If such an element was found, extract the date and time from its title attribute using a regular expression
  30. if (messageDateElement) {
  31. const originalString = messageDateElement.getAttribute('title');
  32. const regex = /(\d{2}\/\d{2}\/\d{4} à \d{2}:\d{2}:\d{2})/;
  33. const match = regex.exec(originalString);
  34.  
  35. // If a match was found, create a new div element and set its inner HTML to the extracted date and time
  36. if (match) {
  37. const newLink = document.createElement('a');
  38. const messageID = message.getAttribute('data-id');
  39. newLink.href = hrefContent + '#message_' + messageID;
  40.  
  41. // Add CSS styles to the new link element
  42. newLink.style.color = '#79bcee';
  43. newLink.style.fontSize = '0.8125rem';
  44.  
  45. // Set the inner HTML of the link to the extracted date and time
  46. newLink.innerHTML = match[0];
  47.  
  48. // Find the element with class 'message-infos' within this message and append the new link element to it
  49. const messageInfosElement = message.querySelector('.message-infos');
  50. if (messageInfosElement) {
  51. messageInfosElement.appendChild(newLink);
  52. }
  53. messageDateElement.remove();
  54.  
  55. // Add a click event listener to the new link that scrolls to the target message and copies its href attribute to the clipboard
  56. newLink.addEventListener('click', event => {
  57. event.preventDefault();
  58. const href = event.target.getAttribute('href');
  59. const messageID = href.split('_')[1];
  60. const targetElement = document.querySelector(`[data-id="${messageID}"]`);
  61. if (targetElement) {
  62. targetElement.scrollIntoView({ behavior: 'smooth' });
  63. navigator.clipboard.writeText(href);
  64. }
  65. });
  66. }
  67. }
  68. });
Advertisement
Add Comment
Please, Sign In to add comment