Udemy Progress Percentage

Adds percentage of completion to Udemy progress text

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();