simonw / Quora Answer Export

// ==UserScript==
// @name         Quora Answer Export
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Export Answers from your Quora Account
// @author       Simon Gardner / Simon Willison
// @license     MIT License; http://www.opensource.org/licenses/mit-license.php
// @copyright    Simon Gardner (http://holisticsystems.co.uk/)
// @match        https://www.quora.com/content?content_types=answers*
// @match        https://quora.com/content?content_types=answers*
// @run-at       document-end
// @grant        none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==

(function() {

    'use strict';
    
    //add export answers option to Quora menu
    $(".UserContentFilterContentType ul").append('<li class="filter_option" style="position:relative"><a id="exportAnswersLink" class="filter_option_link" href="#">Export Answers</a></li>');
    $("#exportAnswersLink").click(exportAnswers);
    console.log("script active");
    
    function exportAnswers() {
        
        showDialog();

        window.exportedAnswers = [];
        //extract a list of questions / hrefs from the content list page
        $(".UserContentMain .UserContentListItem A.question_link").each( function(i,ele){
            exportedAnswers.push({
                question:$(ele).text(),
                href:$(ele).attr("href"),
                meta:$(ele).parent().parent().parent().find(".metadata").text()
            });
        });
        //console.log(exportedAnswers.length + " answers found");
        $("#exportAnswersDialogAnswers").append("<p>exporting answer <span id=\"exportsAnswersProcessingCounter\">0</span> of "+exportedAnswers.length+"</p>");
        //recurse the list of questions retrieving the answers
        //could have used async.js to do this more nicely but didn't for reasons ...
        function asyncRecurse (idx) {
            if (idx < exportedAnswers.length) {
                var a = ary[idx];
                $.get(a.href, function(data){
                    $("#exportsAnswersProcessingCounter").html((idx+1));
                    a.answer = $(data).find(".AnswerBase .inline_editor_value .rendered_qtext:last").html();
                    a.originalQuestion = null;
                    a.originalQuestionUrl = null;
                    if ($(data).find(".AnswerBase .OriginallyAnsweredBanner a").length) {
                        var originalA = $(data).find(".AnswerBase .OriginallyAnsweredBanner a");
                        a.originalQuestion = originalA.text();
                        a.originalQuestionUrl = originalA.attr('href');
                    }
                    if ($("#exportAnswersDialog").length) {
                        //only contine processing if the exportAnswersDialog is present
                        setTimeout(function() { asyncRecurse(++idx); }, 1000); //pause for a while to be nice to Quora before fetching the next answer                    
                    }
                });
            } else {
                console.log("ary", ary);
                outputAnswers(ary);
            }
        }
        asyncRecurse(0);

        return false;
    }

    function outputAnswers(ary) {
      $("#exportAnswersDialogAnswers").append("<p>Your content has been exported below as raw Html. Use Cut/Paste to extract.</p><textarea cols='80' rows='6'>"+htmlTemplate(exportedAnswers.map(formatAnswer).join(""))+"</textArea>");
    }

    function formatAnswer(answer) {
        return "<section><h2 class=\"question_text\"><a href=\"https://www.quora.com/"+answer.href+"\">"+answer.question+"</a></h2>\r\n<div class=\"meta\">"+answer.meta+"</div>\r\n<div class=\"answer\">"+answer.answer+"</div>\r\n</section>\r\n";
    }
    
    function htmlTemplate(content) {
        return "<!DOCTYPE html><html lang=\"en\"><head><meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\" /></head><body>"+content+"</body></html>";
    }

    function showDialog() {
        $("#exportAnswersLink").parent().append("<div id=\"exportAnswersDialog\" style=\"position:absolute; right:50px;\"><div class=\"hover_menu white_bg show_nub\" style=\"width:500px;\"><div class=\"hover_menu_contents\" style=\"padding:1em;\"><h2>Exporting Answers ...</h2><div id=\"exportAnswersDialogAnswers\"></div><div style=\"text-align:right\"><a href=\"javascript:$('#exportAnswersDialog').remove();\" class=\"Button\">Done</a></div></div></div></div></div>");
    }
    
    function closeDialog() {
        //not currently used - this code directly embeded into html. //TODO: find a way to do this better
        $("#exportAnswersDialog").remove();
    }

})();