Udemy Progress Percentage

Adds percentage of completion to Udemy progress text

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Udemy Progress Percentage
// @namespace    https://greasyfork.org/en/users/1444872-tlbstation
// @version      1.0
// @description  Adds percentage of completion to Udemy progress text
// @author       TLBSTATION
// @match        https://www.udemy.com/course/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=udemy.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to calculate percentage and update the text
    function updateProgress() {
        // Select the element with the progress text
        const progressElement = document.querySelector('.ud-heading-sm');

        if (progressElement) {
            const text = progressElement.textContent.trim();

            // Match the format "X of Y complete."
            const match = text.match(/^(\d+) of (\d+) complete\.$/);

            if (match) {
                const completed = parseInt(match[1], 10);
                const total = parseInt(match[2], 10);

                // Calculate the percentage
                const percentage = ((completed / total) * 100).toFixed(1);

                // Update the text with the percentage
                progressElement.textContent = `${completed} of ${total} (${percentage}%) complete.`;
            }
        }
    }

    // Run the function when the page loads
    window.addEventListener('load', updateProgress);

    // Optional: Run periodically in case the element is dynamically updated
    setInterval(updateProgress, 1000);
})();