New Userscript

try to take over the world!

2019-02-19 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.2.1
// @description  try to take over the world!
// @author       You
// @match        https://gitpd.paodingai.com/*
// @connect      cdnjs.cloudflare.com
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @require      https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

(function() {
  'use strict';
  document.onreadystatechange = function () {
    if (document.readyState == "complete") {
      replaceRelativeDateTime();
    }
  }

  const datetimePattern = /(\w+)\s(\d+),\s(\d{4}) (\d+):(\d+)([a|p]m) GMT([+-]\d{4})/;

  const monthMap = {
    'Jan': 1,
    'Feb': 2,
    'Mar': 3,
    'Apr': 4,
    'May': 5,
    'Jun': 6,
    'Jul': 7,
    'Aug': 8,
    'Sep': 9,
    'Oct': 10,
    'Nov': 11,
    'Dec': 12,
  }

  const timezonePattern = /([-|+])(\d{2})(\d{2})/;

  function calcRelativeMilliseconds(timezone) {
    const matched = timezone.match(timezonePattern);
    if (matched) {
      const milliseconds = (Number(matched[2]) * 60 + Number(matched[3])) * 60 * 1000;
      if (matched[1] === '+') {
        return Number(`-${milliseconds}`);
      } else {
        return milliseconds;
      }
    }
  }

  function parseDatetime(datetimeString) {
    const matched = datetimeString.match(datetimePattern);
    if (matched) {
      let hours = Number(matched[4]);
      if (matched[6] === 'pm') {
        hours += 12;
      }
      let timezone = matched[7];
      console.log('timezone', timezone);
      const relativeMilliseconds = calcRelativeMilliseconds(timezone);
      const datetime = {
        year: Number(matched[3]),
        day: Number(matched[2]),
        month: monthMap[matched[1]],
        hours,
        minutes: Number(matched[5]),
        seconds: 0
      };

      const parsedDatetime = new Date(datetime.year, datetime.month, datetime.day, datetime.hours, datetime.minutes, datetime.seconds);
      parsedDatetime.get

      console.log('parseDatetime', datetimeString, datetime);

      return new Date(datetime.year, datetime.month, datetime.day, datetime.hours, datetime.minutes, datetime.seconds);
    }
    return datetimeString;
  }

  function formatDatetime(datetime) {
    let formatted = datetime.getFullYear() + "-" + (datetime.getMonth() + 1) + "-" + datetime.getDate() + " " + datetime.getHours() + ":" + datetime.getMinutes() + ":" + datetime.getSeconds();
    return formatted;
  }

  function replaceRelativeDateTime() {
    const targetNode = document.getElementsByTagName('body')[0];

    // Options for the observer (which mutations to observe)
    const config = { attributes: true, childList: true, subtree: true };

    // Callback function to execute when mutations are observed
    const callback = function(mutationsList, observer) {
      for(let mutation of mutationsList) {
        const timeChildren = mutation.target.querySelectorAll('time');
        if (timeChildren.length > 0) {
          console.log(mutation, timeChildren);
          for (let t of timeChildren) {
            console.log('parsedDatetime', parseDatetime(t.dataset.originalTitle));
            t.textContent = t.dataset.originalTitle;
          }
          continue;
        } else {
          if (mutation.target.nodeName !== 'TIME') {
            continue;
          }
          console.log(mutation.target.textContent, mutation.target.dataset.originalTitle, mutation)
          if (mutation.type == 'childList' && mutation.target.nodeName === 'TIME') {
            const utcDatetime = mutation.target.getAttribute('datetime');
            const textContent = utcDatetime ? formatDatetime(new Date(utcDatetime)) : mutation.target.dataset.originalTitle;
            mutation.target.textContent = textContent;
          }
        }
      }
    };

    // Create an observer instance linked to the callback function
    const observer = new MutationObserver(callback);

    // Start observing the target node for configured mutations
    observer.observe(targetNode, config);

    const relativeDatetimeElements = document.getElementsByTagName('time');
    for (let element of relativeDatetimeElements) {
      element.textContent = element.dataset.originalTitle;
    }
  }
})();