Advertisement
Nikou

Resaltar comentarios de usuarios recientes en Menéame

Apr 9th, 2020 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        meneame.net - Destacar comentarios de usuarios recientes
  3. // @namespace    http://tampermonkey.net/
  4. // @version     1.0
  5. // @description Destaca los comentarios de aquellos usuarios creados hace menos de 90 días.
  6. // @author      Niko
  7. // @match       *://*.meneame.net/*
  8. // @grant       GM.setValue
  9. // @grant       GM.getValue
  10. // ==/UserScript==
  11.  
  12. /*jshint esversion: 6 */
  13.  
  14. // ---- SCRIPT CONFIG ----
  15. const NOOB_DAYS = 90;
  16. const BGPLAIN_COLOR = '#EEE';
  17. const BG_STRIPES_COLOR1 = '#fff';
  18. const BG_STRIPES_COLOR2 = '#fff5ed';
  19. const STYLE = "STRIPES"; // PLAIN or STRIPES
  20.  
  21. // ---- API values ----
  22. const USERNAME_CLASS = '.username';
  23. const REQUEST_URL = 'https://www.meneame.net/backend/get_user_info?id=';
  24.  
  25. window.onload = function () {
  26.     let usernames = document.querySelectorAll(USERNAME_CLASS);
  27.     usernames.forEach( function(node) {
  28.         processUserNode(node);
  29.     });
  30. };
  31.  
  32. function processUserNode(node) {
  33.     const user = node.textContent;
  34.     (async () => {
  35.         let date = await GM.getValue(user, "");
  36.         if (date === "") {
  37.             requestRegistrationDate(node, user);
  38.         } else {
  39.             checkIfNoobUser(node, date);
  40.         }
  41.     })();
  42. }
  43.  
  44. function requestRegistrationDate(node, user) {
  45.     let xhttp = new XMLHttpRequest();
  46.     xhttp.onreadystatechange = function() {
  47.         if (this.readyState === 4 && this.status === 200) {
  48.             const dateRegex = /((0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-[12]\d{3})/;
  49.             const date = this.responseText.match(dateRegex);
  50.             GM.setValue(user, date[0]);
  51.             checkIfNoobUser(node, date[0]);
  52.         }
  53.     };
  54.     xhttp.open("GET", REQUEST_URL + user, true);
  55.     xhttp.send();
  56. }
  57.  
  58. function checkIfNoobUser(node, dateStr) {
  59.     const dateArray = dateStr.toString().split('-');
  60.     const date = new Date(dateArray[2], dateArray[1] - 1, dateArray[0]);
  61.     const daysSinceRegistration = (Date.now() - date.getTime()) / 3600000 / 24;
  62.     if (daysSinceRegistration < NOOB_DAYS) {
  63.         highlightUser(node);
  64.     }
  65. }
  66.  
  67. function highlightUser(node) {
  68.     switch(STYLE) {
  69.         case "PLAIN":
  70.             node.parentNode.parentNode.style.backgroundColor = BGPLAIN_COLOR;
  71.             break;
  72.         case "STRIPES":
  73.             const BGSTRIPES = "repeating-linear-gradient( 45deg, " + BG_STRIPES_COLOR1 + ", " + BG_STRIPES_COLOR1 + " 10px, " + BG_STRIPES_COLOR2 + " 10px, " + BG_STRIPES_COLOR2 + " 20px );";
  74.             node.parentNode.parentNode.setAttribute("style", "background-image: " + BGSTRIPES);
  75.             break;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement