Misskey time fix

N分前って言われてもわかんない

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Misskey time fix
// @namespace   notoiro
// @match       https://misskey.niri.la/*
// @grant       none
// @version     1.0
// @author      notoiro
// @description N分前って言われてもわかんない
// @run-at      document-idle
// @license     CC0
// ==/UserScript==

// settings
const smart_date = true; // 今日なら時間だけ、そうじゃないなら日付も。
const load_wait = 2; // タイムラインロードされるまで何秒待つか。

const is_today = (date) => {
  if(!smart_date) return false;

  const now = new Date();

  const date_zero = new Date(date.toDateString()).getTime();
  const now_zero = new Date(now.toDateString()).getTime();

  return date_zero == now_zero;
}

const parse_date = (date) => {
  const abs_date = `${date.getFullYear()}/${('0' + (date.getMonth() + 1)).slice(-2)}/${('0' + date.getDate()).slice(-2)}`;
  const abs_time = `${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;

  return (!is_today(date)) ? `${abs_date} ${abs_time}` : abs_time;
}

const apply_time = (elements) => {
  for(const el of elements){
    el.innerText = parse_date(new Date(el.title));
  }
}

const callback = (mutations) => {
  for(const mut of mutations){
    const els = mut.target.querySelectorAll('header > div > a > time, div > button > time');

    apply_time(els);
  }
}

const start_apply = () => {
  const els = document.body.querySelectorAll('header > div > a > time, div > button > time');

  apply_time(els);
}

const main = () => {
  const obs_target = document.querySelector("main");
  const opt = {
    childList: true,
    attributes: true,
    subtree: true,
  }

  const observer = new MutationObserver(callback);
  observer.observe(obs_target, opt);

  start_apply();
}

setTimeout(main, load_wait * 1000);