GitHub Creation Date

Displays the creation date of a GitHub repository when clicked.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        GitHub Creation Date
// @namespace   Violentmonkey Scripts
// @match       https://github.com/*/*
// @grant       none
// @version     1.0
// @author      -
// @description Displays the creation date of a GitHub repository when clicked.
// @run-at      document-end
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  const createButton = (text, styles = {}) => {
    const button = document.createElement("button");
    button.textContent = text;
    Object.assign(button.style, {
      marginLeft: "10px",
      padding: "3px 6px",
      cursor: "pointer",
      backgroundColor: "#0366d6",
      color: "#fff",
      border: "none",
      borderRadius: "3px",
      fontSize: "12px",
      lineHeight: "1.5",
      ...styles,
    });
    return button;
  };

  const fetchRepoInfo = async (owner, repo) => {
    const apiUrl = `https://api.github.com/repos/${owner}/${repo}`;
    const response = await fetch(apiUrl, {
      headers: { Accept: "application/vnd.github.v3+json" },
    });

    if (!response.ok) {
      throw new Error(
        `Failed to fetch repository info (Status: ${response.status})`
      );
    }
    return response.json();
  };

  const init = () => {
    const repoTitleComponent = document.querySelector(
      'div[id="repo-title-component"]'
    );
    if (!repoTitleComponent) {
      console.error("Could not find the repository title component.");
      return;
    }

    const button = createButton("Creation Date");
    repoTitleComponent.appendChild(button);

    button.addEventListener("click", async () => {
      const [, owner, repo] = window.location.pathname.split("/");

      if (!owner || !repo) {
        alert("Invalid repository URL.");
        return;
      }

      try {
        const repoInfo = await fetchRepoInfo(owner, repo);
        alert(
          `'${repo}' was created on: ${new Date(
            repoInfo.created_at
          ).toLocaleString()}`
        );
      } catch (error) {
        alert(`Error: ${error.message}`);
      }
    });
  };

  init();
})();