Yet Another Udemy Course Creation Date Getter

Shows the creation date of the course below the latest update date.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Yet Another Udemy Course Creation Date Getter
// @namespace    <https://github.com/lundeen-bryan>
// @version      2.0.0
// @description  Shows the creation date of the course below the latest update date.
// @author       lundeen-bryan based on a script by scriptbug
// @match        https://www.udemy.com/course/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=udemy.com
// @grant        GM_addStyle
// @license      GPL-2.0-or-later
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// ==/UserScript==

var $ = window.$;
var x = document.body.getAttribute("data-clp-course-id");
$.get("https://www.udemy.com/api-2.0/courses/" + x + "/?fields[course]=created", function (data) {
    var date = new Date(data.created);
    var formattedDate = (date.getMonth() + 1) + "/" + date.getFullYear(); // Adjust the format as needed

    // Function to create the date element
    function createDateElement() {
        var creationDateDiv = document.createElement("div");
        creationDateDiv.className = "clp-lead__element-item";
        creationDateDiv.innerHTML = '<span>Created on ' + formattedDate + '</span>';
        return creationDateDiv;
    }

    // Function to insert the creation date
    function insertCreationDate() {
        var updateDateElement = document.querySelector("[data-purpose='last-update-date']");
        if (updateDateElement) {
            updateDateElement.parentNode.insertBefore(createDateElement(), updateDateElement.nextSibling);
            return true;
        }
        return false;
    }

    // Try to insert immediately
    if (!insertCreationDate()) {
        // Use MutationObserver as the first fallback
        var observer = new MutationObserver(function (mutations, me) {
            if (insertCreationDate()) {
                me.disconnect(); // stop observing when done
            }
        });

        observer.observe(document, {
            childList: true,
            subtree: true
        });

        // Additional fallback: try again after a delay
        setTimeout(function () {
            if (!insertCreationDate()) {
                console.error("Could not insert the creation date after delay.");
            }
        }, 5000); // Wait for 5 seconds as the last resort
    }
});