let locationName = "student-research-reports";
let nextButton = null;
let totalPages = 0;
let pageNumber = 1;

function scanButtons()
{
    // Scan the page for anchor elements that act as page buttons.
    nextButton = null;
    let filename = null;
    let firstButton = null;
    let previousButton = null;
    let lastButton = null;
    let anchors = document.getElementsByTagName("a");
    for (let i = 0i < anchors.lengthi++) {
        let attributes = anchors[i].attributes;
        for (let j = 0j < attributes.lengthj++) {
            if (attributes[j].name == "data-dt-idx") {
                let className = anchors[i].className;
                if (className.indexOf("disabled") == -1) {
                    if (attributes[j].value == "0") {
                        // Set the first button.
                        firstButton = anchors[i];
                    }
                    else if (attributes[j].value == "1") {
                        // Set the previous button.
                        previousButton = anchors[i];
                    }
                    else if (attributes[j].value == "2") {
                        // Set the next button.
                        nextButton = anchors[i];
                    }
                    else if (attributes[j].value == "3") {
                        // Set the last button.
                        lastButton = anchors[i];
                    }
                }
            }
        }
    }
    if (firstButton != null) {
        // Add the href attribute to the first button.
        filename = locationName + "-" + 1 + ".html";
        firstButton.setAttribute("href"filename);
    }
    if (previousButton != null) {
        // Add the href attribute to the previous button.
        filename = locationName + "-" + (pageNumber - 1) + ".html";
        previousButton.setAttribute("href"filename);
    }
    if (nextButton != null) {
        // Add the href attribute to the next button.
        filename = locationName + "-" + (pageNumber + 1) + ".html";
        nextButton.setAttribute("href"filename);
    }
    if (lastButton != null) {
        // Add the href attribute to the last button.
        filename = locationName + "-" + totalPages + ".html";
        lastButton.setAttribute("href"filename);
    }
}

function savePage()
{
    // Scan the buttons and build the file name.
    scanButtons();
    let filename = locationName + "-" + pageNumber + ".html";
    window.webkit.messageHandlers.progress.postMessage(pageNumber/totalPages);
    if (nextButton == null || pageNumber == totalPages) {
        // If the page does not have a next button, tell SiteSucker to save the last page.
        window.webkit.messageHandlers.saveLast.postMessage(filename);
    }
    else {
        // If the page has a next button, tell SiteSucker to save the page.
        window.webkit.messageHandlers.save.postMessage(filename);
        // Wait 3 seconds for the page to be saved and then click the next button.
        setTimeout(clickButton3000);
    }
    pageNumber++;
}

function clickButton()
{
    // Click the next button, wait 3 seconds for the page to change, then save the page.
    nextButton.click();
    setTimeout(savePage3000);
}

function start()
{
    // After the page has loaded, get the total number of pages and scan the buttons.
    totalPages = parseInt(document.getElementById("total_pages").innerText10);
    scanButtons();
    if (nextButton != null) {
        // If the page has a next button, build the file name and tell SiteSucker to save the page.
        let filename = locationName + "-" + pageNumber + ".html";
        window.webkit.messageHandlers.save.postMessage(filename);
        window.webkit.messageHandlers.progress.postMessage(pageNumber/totalPages);
        // Wait 3 seconds for the page to be saved and then click the next button.
        setTimeout(clickButton3000);
        pageNumber++;
    }
}

// Wait 3 seconds before starting.
setTimeout(start3000);