Literotica Downloader

Single page HTML download for Literotica with improved readability

As of 2021-04-11. See the latest version.

// ==UserScript==
// @name        Literotica Downloader
// @description Single page HTML download for Literotica with improved readability
// @namespace   literotica_downloader
// @include     https://www.literotica.com/stories/memberpage.php*
// @require     https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @version     2.7
// @author      Improved by a random redditor and @nylonmachete, originally by Patrick Kolodziejczyk
// ==/UserScript==
var isUsernameInFilename = true;
var isDescriptionInFilename = false;
var isNoteInFilename = false;
var isCategoryInFilename = false;
var SEPARATOR = '_';
var PREFIX_NOTE = 'NOTE_';
var bodyStyle = ' style="background-color:#333333; color: #EEEEEE; font-family: Helvetica,Arial,sans-serif; width: 50%; margin: 0 auto; line-height: 1.5em; font-size:2.2em; padding: 50px 0 50px 0;" ';
var chapterStyle = ' style="line-height: 1.4em;" ';
var descriptionStyle = ' style="line-height: 1.2em;" ';
// Creating style for a download icon
var iconDonwload = ' style=\'background-image: url("https://marcoceppi.github.io/bootstrap-glyphicons/img/glyphicons-halflings.png");     background-position: -120px -24px;cursor: pointer;cursor: hand;    background-repeat: no-repeat;    display: inline-block;    height: 14px;    line-height: 14px;    vertical-align: text-bottom;    width: 14px;}\'';

function buildFilename(title, author, description, note, category) {
    var toReturn = title;
    if (isUsernameInFilename) {
        toReturn = toReturn + SEPARATOR + author;
    }
	if(isDescriptionInFilename){
		if (description != null && description != "") {
			toReturn = toReturn + SEPARATOR + description.replace(/[^\w\s]/gi, '');
		}
	}
	if(isNoteInFilename){
		if (note != null && note != "") {
		toReturn = toReturn + SEPARATOR +PREFIX_NOTE+ note;
		}
	}
	if(isCategoryInFilename){
		if (category != null && category != "") {
		toReturn = toReturn + SEPARATOR + category;
		}
	}
    // Add file extension;
    toReturn = toReturn + '.html';
	console.log("buildFilename ->" +toReturn);
	console.log("category ->" +category);
    return toReturn;
}
$(document).ready(function() {

    // Function used to return content as a file for the user.
    function saveTextAsFile(textToWrite, fileNameToSaveAs) {
        var textFileAsBlob = new Blob([textToWrite], {
            type: 'text/javascript'
        });
        var downloadLink = document.createElement('a');
        downloadLink.download = fileNameToSaveAs;
        downloadLink.innerHTML = 'Download File';
        // Firefox requires the link to be added to the DOM
        // before it can be clicked.
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        //downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = 'none';
        document.body.appendChild(downloadLink);
        downloadLink.click();
    }
    // Function parsing all pages to get the storie based
    function getContentOfStoie(baseURL) {
        console.log("Fetching " + baseURL);
        var remote;
        $.ajax({
            url: baseURL,
            type: 'GET',
            async: false,
            crossDomain: true,
            success: function(data) {
                if ($(data).find('a.l_bJ.l_bL').size() > 0) {
                    console.log($(data).find('a.l_bJ.l_bL').size());
                    remote = $(data).find('.panel.article.aa_eQ .aa_ht').html() + getContentOfStoie($(data).find('a.l_bJ.l_bL')[0].href);
                } else {
                    remote = $(data).find('.panel.article.aa_eQ .aa_ht').html();
                }
            }
        });
        return remote;
    }

    function getABookForSerieDiv(myDiv) {
        var title = $.trim(myDiv.text().split(':')[0]);
		// Get the X Part Series
		var descriptionSeries = $.trim(myDiv.text().split(':')[1]);
        var author = $('.contactheader').text();
        alert("Starting building file for " + title + " of " + author + ".\nPlease wait...");
        var book = '<html>\n<head>\n<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">\n';
        book += '<title>' + title + '</title>';
        book += '<meta content="' + author + '" name="author">';
        book += '</head>\n<body ' + bodyStyle + ' >';

        function addChapter(element, index, array) {
            if ($(this).find('a').size() > 0) {
                var chapeterTitle = $($(this).find('td a')[0]).text();
                var description = $($(this).find('td')[1]).text();
                book += '<h1 class=\'chapter\'' + chapterStyle + '>' + chapeterTitle + '</h1>';
                book += '<h2 class=\'chapter\'' + descriptionStyle + '>' + description + '</h2>';
                var link = $($(this).find('a')[0]);
                book += getContentOfStoie(link.attr('href'));
            }
        }
        myDiv.nextUntil('.ser-ttl,.root-story').each(addChapter);
        saveTextAsFile(book, buildFilename(title, author, descriptionSeries, ));
    }

    function getABookForStoryDiv(myDiv) {
        var title = $.trim($($(myDiv)).text().split('(')[0]);
		var note = $.trim($($(myDiv)).text().split('(')[1]).replace(")", "");
		if ($(myDiv).find('a').size() >=0) {
			var chapterTitle = $($(myDiv.parent()).find('td a')[0]).text();
			var description = $($(myDiv.parent()).find('td')[1]).text();
			var category = $($(myDiv.parent()).find('td')[2]).text();
        }
        var author = $('.contactheader').text();
        var book = '<html>\n<head>\n<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">\n';
        console.log('title' + title);
        book += '<title>' + title + '</title>';
        book += '<meta content="' + author + '" name="author">';
		book += '<meta content="' + author + '" name="author">';
        book += '</head>\n<body ' + bodyStyle + '>';
		book += '<h1 class=\'chapter\'' + chapterStyle + '>' + chapterTitle + '</h1>';
		book += '<h2 class=\'chapter\'' + descriptionStyle + '>' + description + '</h2>';
		var link = $($(myDiv).find('a')[0]);
        book += getContentOfStoie(link.attr('href'));

        saveTextAsFile(book, buildFilename(title, author, description, note, category));
    }
    $('.ser-ttl td:nth-child(1)').prepend('<span ' + iconDonwload + '></span>');
    $('.ser-ttl td:nth-child(1) span').click(function() {
        getABookForSerieDiv($(this).parent().parent());
    });
    var idIcon = Math.floor(Math.random() * 100);
    $('.root-story td:nth-child(1), .sl td:nth-child(1)').prepend('<span ' + iconDonwload + ' id="' + idIcon + '"></span>');
    $('.root-story td:nth-child(1), .sl td:nth-child(1) span').click(function() {
        getABookForStoryDiv($(this).parent());
    });
});
z