Haxorsnake / Novel Planet Utilities

// ==UserScript==
// @name         Novel Planet Utilities
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Adds a first chapter button to the Novel Page. Adds auto loading of Novels on the front page.
// @author       Haxorsnake
// @match        https://novelplanet.com/*
// @grant        none
// @updateURL    https://openuserjs.org/meta/Haxorsnake/Novel_Planet_Utilities.meta.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    const novelPage = document.location.href;

    function getFirst() {
        let firstChapter = document.querySelectorAll("div.rowChapter")[document.querySelectorAll("div.rowChapter").length -1];
        let firstLink = firstChapter.getAttribute("alink");
        return firstLink;
    }

    function clickFunction(teyy) {
        var divLoading = $(teyy).parent().siblings('.divLoading');
        var divMore = $(teyy);
        var numItems = parseInt(divMore.attr('numItems'));

        divLoading.show();
        divMore.hide();
        var sortValue = $(teyy).attr('sort');
        $.ajax(
            {
                type: "POST",
                url: "/Home/GetNextNovels",
                dataType: "html",
                data: { id: numItems, sort: sortValue },
                success: function (message) {
                    if (message !== "") {
                        divMore.parent().parent().siblings('div').append(message);
                        numItems += 20;
                        divMore.attr('numItems', numItems);

                        divLoading.hide();
                        divMore.show();

                        //MapClick($('article'));
                    }
                    else
                    {
                        divLoading.html("That's all we have for now.");
                    }
                }
            });
    }

    function makeButton(link) {
        let button = document.createElement("span");
        let bText = document.createTextNode("First Chapter");
        let bLink = document.createElement("a");
        let locationNolog = document.querySelector("#spanBookmark");
        let locationLog = document.querySelector("#spanBookmarkManager");

        bLink.append(bText);
        bLink.setAttribute("href", link);
        bLink.setAttribute("class", "button small");
        button.append(bLink);

        if(locationNolog){
            locationNolog.parentElement.append(button);
        }else if(locationLog){
            locationLog.parentElement.append(button);
        }else {
            return false;
        }
    }

    if(novelPage.includes('Novel')){
        if(novelPage.includes('id') === false){
            makeButton(getFirst());
        }
    }else{
        $(window).scroll(function() {
            if($(window).scrollTop() + $(window).height() > $(document).height() - 1) {
                clickFunction('.btnMore');
            }
        });
    }

})();