Origin

Display the creation time of the github repository.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Origin
// @version      0.1
// @description  Display the creation time of the github repository.
// @author       Sh-Fang
// @match        https://github.com/*
// @grant        none
// @source       https://github.com/Sh-Fang/Origin
// @license MIT
// @namespace https://greasyfork.org/users/1232796
// ==/UserScript==

(function() {
    'use strict';

    // Parsing username and repository name
    var pathArray = window.location.pathname.split('/');
    var username = pathArray[1];
    var repository = pathArray[2];

    // Check that the username and repository name both exist and are not empty
    if (username && repository) {

        // Construct a unique identifier for storing data in localStorage
        var storageKey = 'githubInfo_' + username + '_' + repository;

        // Trying to get saved information from localStorage
        var storedInfo = localStorage.getItem(storageKey);

        // Use XPath to find the specified element
        var xpathResult = document.evaluate(
            '/html/body/div[1]/div[6]/div/main/div/div[1]/div[1]/div[1]/strong/a',
            document,
            null,
            XPathResult.FIRST_ORDERED_NODE_TYPE,
            null
        );

        // Get the found element
        var repoLink = xpathResult.singleNodeValue;

        // Check if the element is found
        if (repoLink) {

            // Get the text content of the element
            var repoName = repoLink.textContent;

            // If the information has been stored, the stored information is displayed directly
            if (storedInfo) {
                repoLink.textContent = repoName + ' (Created at: ' + storedInfo + ')';
            } else {
                // GitHub API request URLs
                var apiUrl = 'https://api.github.com/repos/' + username + '/' + repository;

                // GitHub API request
                fetch(apiUrl)
                    .then(response => response.json())
                    .then(data => {

                    // Get "created_at" item
                    var createdAt = data.created_at;

                    // Convert timestamps to human-readable date strings (containing only the year, month, and day)
                    var createdDate = new Date(createdAt).toLocaleDateString();

                    // Add a timestamp after repository name
                    repoLink.textContent = repoName + ' (Created at: ' + createdDate + ')';

                    // Save timestamp to localStorage
                    localStorage.setItem(storageKey, createdDate);
                })
                    .catch(error => console.error('Error fetching GitHub API:', error));
            }

        }


    }

})();