IvyLearn Discussion Progress

Shows progress on discussion assignments in IvyLearn

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         IvyLearn Discussion Progress
// @version      1.0
// @description  Shows progress on discussion assignments in IvyLearn
// @author       j01t3d
// @namespace    https://github.com/j01t3d/ivylearn-discussion-progress
// @match        https://ivylearn.ivytech.edu/courses/*/assignments
// @grant        GM_xmlhttpRequest
// @connect      ivylearn.ivytech.edu
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const courseID = location.pathname.match(/courses\/(\d+)/)?.[1];

    let userID = 0;
    const discussionProgress = {};

    GM_xmlhttpRequest({
        method: "GET",
        url: "https://ivylearn.ivytech.edu/api/v1/users/self",
        onload: function(response) {
            const data = JSON.parse(response.responseText);
            userID = data.id;
            fetchDiscussions();
        }
    });

    function fetchDiscussions() {
        GM_xmlhttpRequest({
            method: "GET",
            url: `https://ivylearn.ivytech.edu/api/v1/courses/${courseID}/discussion_topics`,
            onload: function(response) {
                const topics = JSON.parse(response.responseText);
                const discussions = topics.filter(d => d.title && d.title.toLowerCase().includes("discussion"));

                if (discussions.length === 0) return;

                let completed = 0;
                discussions.forEach(d => {
                    fetchDiscussionProgress(d, () => {
                        completed++;
                        if (completed === discussions.length) {
                            waitForAssignments();
                        }
                    });
                });
            }
        });
    }

    function fetchDiscussionProgress(discussion, callback) {
        GM_xmlhttpRequest({
            method: "GET",
            url: `https://ivylearn.ivytech.edu/api/v1/courses/${courseID}/discussion_topics/${discussion.id}/view`,
            onload: function(resp) {
                const text = resp.responseText.trim();
                let initialPostMade = false;
                let replyCount = 0;

                if (text !== "require_initial_post") {
                    const data = JSON.parse(text);
                    const viewData = data.view || [];
                    viewData.forEach(post => {
                        if (post.user_id === userID) initialPostMade = true;
                        if (post.replies) {
                            post.replies.forEach(reply => {
                                if (reply.user_id === userID) replyCount++;
                            });
                        }
                    });
                }

                const totalPostsRequired = 4;
                const progress = Math.min(((initialPostMade ? 1 : 0) + replyCount) / totalPostsRequired, 1);
                discussionProgress[discussion.title.trim()] = Math.round(progress * 100);

                if (callback) callback();
            },
            onerror: function() {
                if (callback) callback();
            }
        });
    }

    function waitForAssignments() {
        let interval;

        const observer = new MutationObserver(() => {
            const assignments = document.querySelectorAll("li.assignment.sort-disabled.search_show");
            if (assignments.length > 0) {
                updateAssignmentDOM(assignments);
                observer.disconnect();
                clearInterval(interval);
            }
        });

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

        interval = setInterval(() => {
            const assignments = document.querySelectorAll("li.assignment.sort-disabled.search_show");
            if (assignments.length > 0) {
                updateAssignmentDOM(assignments);
                clearInterval(interval);
                observer.disconnect();
            }
        }, 100);
    }

    function updateAssignmentDOM(assignments) {
        assignments.forEach(assignment => {
            const infoDiv = assignment.querySelector('.ig-info');
            if (!infoDiv) return;

            const titleElement = infoDiv.querySelector('.ig-title');
            if (!titleElement) return;

            const title = titleElement.textContent.trim();

            const matchKey = Object.keys(discussionProgress)
                .find(d => title.toLowerCase().includes(d.toLowerCase()));

            if (matchKey) {
                const score = discussionProgress[matchKey];
                const detailsDiv = infoDiv.querySelector('.ig-details.rendered');
                if (!detailsDiv) return;
                if (detailsDiv.querySelector('.assignment-percentage')) return;

                const percentageDiv = document.createElement('div');
                percentageDiv.className = 'ig-details__item assignment-percentage';

                const strong = document.createElement('strong');
                strong.textContent = `${score}%`;
                strong.style.color = score === 100 ? 'green' : 'red';

                percentageDiv.appendChild(strong);
                detailsDiv.appendChild(percentageDiv);
            }
        });
    }

})();