austindh / Math 313 Homework

// ==UserScript==
// @name       Math 313 Homework
// @namespace  
// @version    0.1
// @description  To mark which homework problems I've already done
// @match      https://learningsuite.byu.edu/student*
// @copyright  2012+, Austin Hughes
// ==/UserScript==

	//Check if the table we're looking for is visible (Look for cell that says "Homework Problems");
	var checker = setInterval( function () {
    	var cellToTest = $( "tbody tr td" )[4];
        if ( cellToTest ) {
       		if ( $( cellToTest ).text() === "Homework Problems" ) {
				markHomework();
            	clearInterval( checker );
    		}
        }
    }, 500);
	
    //Function called once cell (and corresponding homework table) is found
    function markHomework () {
        //Gather all chapters and assignments
        var chapters = {};
        $( "tbody tr" ).each( function () {
            var chapter = parseFloat( $(this).find( "td" ).eq(3).text() );
            var problems = $(this).find( "td" ).eq(4).text().split( ", " );
            if ( !isNaN( chapter ) ) {
                if ( chapters[chapter] ) {
                	chapter = chapter + "a";
                    chapters[chapter] = problems;
					$(this).find( "td" ).eq(3).text( chapter ); //Change duplicate chapter to have "a" on the end
                } else {
                	chapters[chapter] = problems;
                }    
            	$(this).find( "td" ).eq(3).wrapInner( "<div class=\"chapter\"></div>" ); //wrap chapter class around chapter cells
                $(this).find( "td" ).eq(4).wrapInner( "<div class=\"problems\"></div>" );
            }
        });
        
        //Create object to hold chapters, and each problem, with a finished flag
        if ( localStorage.Math313HW ) {
        	var finishedProblems = JSON.parse( localStorage.Math313HW );
        } else {
            var finishedProblems = {};
        }
        
        for ( var chapter in chapters ) {
            
            for ( var problem in chapters[chapter] ) {
            	var num = chapters[chapter][problem];
                if ( !finishedProblems[chapter] ) {
                    finishedProblems[chapter] = {};
                }
                if ( !finishedProblems[chapter][num] ) {
                	finishedProblems[chapter][num] = false;
                }
            }
        }

        localStorage.Math313HW = JSON.stringify( finishedProblems );
        
        //Render which problems are done
        renderFinishedProblems( finishedProblems );
        
        $( ".chapter" ).on( "click", function () {
        	var finishedProblems = JSON.parse( localStorage.Math313HW );
            var chNum = $(this).text();
            var num = prompt( "Enter the problem(s) finished for Ch. " + chNum + " (separated by commas) OR \"all\" to mark all done." );
            if ( num === "all" ) {
                for ( var prob in finishedProblems[chNum] ) {
                	if ( finishedProblems[chNum][prob] ) {
                        finishedProblems[chNum][prob] = false;
                    } else {
                        finishedProblems[chNum][prob] = true;
                    }	
                }
            } else {
            	num = num.split( "," );
                for ( var i = 0; i < num.length; i++ ) {
                    if ( finishedProblems[chNum][num[i]] ) {
                        finishedProblems[chNum][num[i]] = false;
                    } else {
                        finishedProblems[chNum][num[i]] = true;
                    }
                }
            }
            
            
            renderFinishedProblems( finishedProblems );
            localStorage.Math313HW = JSON.stringify( finishedProblems );
        });
        
    }
	
	//
	function renderFinishedProblems( finishedProblems ) {
		$( "tbody tr" ).each( function () {
            var ch = $(this).find( ".chapter" ).text().trim();
            if ( ch.length > 0 ) {
                var finished = [];
                var notFinished = [];
                for ( var prob in finishedProblems[ch] ) {
                    var prblm = finishedProblems[ch][prob];
                    //console.log(prob, prblm);
                    if ( prblm ) {
                    	finished.push( prob );//if 'true' put in finished array
                    } else {
                    	notFinished.push( prob );
                    }
                }
                var done = finished.join( ", " );
                var not = notFinished.join( ", " );
                if ( done.length > 0 && not.length > 0 ) { not = ", " + not; };
                var problems = "<span class=\"done\">" + done + "</span>" + not;
               	$(this).find( ".problems" ).html( problems );
            }
            
        });
    }
	
	//CSS
    GM_addStyle('.done {background-color: lightgray;text-decoration: line-through;color:gray}');
	GM_addStyle('.chapter {font-weight: bold;background-color: #d1e4f6;border-radius: 5px;padding: 2px;margin: 2px;text-align: center;}');
    GM_addStyle('.problems{background-color: rgba(209, 228, 246, 0.56);padding: 5px;margin: -1px;margin-left: -7px;margin-right: -6px;}');
	GM_addStyle('.chapter:hover {cursor: pointer;}');