Aperture / AsianFanfics Suite

// ==UserScript==
// @name AsianFanfics Suite
// @updateURL https://openuserjs.org/meta/Aperture/AsianFanfics_Suite.meta.js
// @version 1.0.1
// @author Aperture
// @description Adds handy AJAX functionality to many of AsianFanfics's user pages.
// @include http://www.asianfanfics.com/*
// @include https://www.asianfanfics.com/*
// @grant none
// @run-at document-start
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js#sha256=hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=
// ==/UserScript==

// ==OpenUserJS==
// @author Aperture
// ==/OpenUserJS==

function AddTitleMeta(force) {
    if (typeof force == "undefined") {
        force = false;
    }
    if (typeof idStory == "undefined" || (!force && $(".title-meta").length > 0)) {
        return;
    }
    GetBaseStoryPage(function(html) {
        if (force) {
            $(".title-meta").remove();
        }
        $(".main-meta").prepend(html.find(".title-meta, #unsub-comment-form"));

        function ResultHandler(result) {
            if (result == "ok") {
                AddTitleMeta(true);
            } else {
                showError(result);
            }
        }

        $("#upvote, #cancel_upvote").click(function(e) {
            e.preventDefault();
            var target = $(this);
            $.get($(this).attr('href'), "", function(result) {
                if (result == "ok") {
                    target.hide();
                    $("#upvote, #cancel_upvote").not(target).show();
                } else {
                    showError(result);
                }
            });
        });

        $("#subscribe").click(function(e) {
            e.preventDefault();
            $.get($(this).attr('href'), "", ResultHandler);
        });

        $("#unsubscribe").click(function() {
            $.get($("#unsub-comment-form").find("#unsubscribe").attr('href'), "", ResultHandler);
        });
    });
}

function GetBaseStoryPage(callback) {
    if (typeof idStory == "undefined") {
        return;
    }
    //Get base story link
    var link = $(".widget--chapters__foreword-link a").attr('href');
    $.get(link, "", function(data) {
        var html = $(data);
        callback(html);
    });
}

function AddStoryDescriptions() {
    if (!location.pathname.startsWith("/favorite/view")) {
        return;
    }
    $(".excerpt a[href^='/favorite/remove_favorite']").click(function(e) {
        e.preventDefault();
        var story = $(this).parents(".excerpt");
        $.get($(this).attr('href'), "", function(result) {
            story.fadeOut(400);
        });
    });
    $(".excerpt").each(function(i, el) {
        var story = $(this);
        var link = story.find(".excerpt__title a");
        if (link.length === 0) {
            return true; //Continue loop
        }
        $.get(link.attr('href'), "", function(data) {
            var html = $(data);
            var desc = html.find("#story-description").addClass("excerpt__text");
            desc.text(desc.text().substring(0, 500)); //Strip images and shorten
            story.find(".excerpt__meta").after(desc);
        });
    });
}

function AddStoryAjax() {
    if (typeof idStory == "undefined" || $(".click-to-read-full").length === 0) {
        return;
    }
    $(".click-to-read-full a").click(function(e) {
        e.preventDefault();
        $.get($(this).attr('href'), "", function(result) {
            if (result == "ok") {
                location.reload(true); //Refresh to get full story
            } else {
                showError(result);
            }
        });
    });
}

$(function() {
    AddTitleMeta();
    AddStoryDescriptions();
    AddStoryAjax();
    console.log("AFF suite loaded");
});