goodreadsReply

Quote only the selected text on a Goodreads comment reply; allow user to use Markdown

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         goodreadsReply
// @namespace    http://pointerstop.ca/
// @version      0.6.3
// @description  Quote only the selected text on a Goodreads comment reply; allow user to use Markdown
// @author       [email protected]
// @match        https://www.goodreads.com/topic/*
// @match        https://www.goodreads.com/review/show/*
// @grant        none
// @require      https://cdnjs.cloudflare.com/ajax/libs/showdown/1.6.4/showdown.min.js
// ==/UserScript==
function makeHTML(comment){
    var options   = {simplifiedAutoLink: true,
                     excludeTrailingPunctuationFromURLs: true,
                     literalMidWordUnderscores: true,
                     strikethrough: true
                    };
    var converter = new showdown.Converter(options),
        text      = comment.val(),
        html      = converter.makeHtml(text).replace(/<p>/g, '\n').replace(/<\/p>/g, '');
    comment.val(html);
}
jQuery(function($) {
    'use strict';
    var preview = $("input:submit + span + a");
    var commentText = $('textarea');
    var clickHandler = preview.prop("onclick");
    preview.prop("onclick",null);
    $("input:submit").on('click', function(){
        makeHTML(commentText);
    });
    preview.on('click', function(){
        makeHTML(commentText);
        clickHandler();
    });
    $("#box #close").on('click',function(){ commentText.focus(); });
    // turn off all the old click handlers
    $("a[href='#comment_form']").prop("onclick", null);

    // attach the new click handlers
    $("a[href='#comment_form']").on('click', function (e) {
        // disable the default handler
        e.preventDefault();
        // find the parent comment, the author's name, and the comment text
        var comment = $(e.target).parents("div.comment");
        var author = comment.find("span.commentAuthor a").text();
        var text = comment.find("div.reviewText");
        // use the selected text, if it exists, else the first 200 characters of the comment
        var selection = "";
        try {
            // if we're selecting a text node... get the selection
            if (window.getSelection().anchorNode.parentElement==text[0]){
                selection = window.getSelection().toString();
            }
        } catch(e) { // we don't care about errors
        }
        // if nothing has been selected, use GoodRead's normal default of the first 200 chars
        if (!selection) {
            selection = text.text().trim();
            if (selection.length > 200) {
                selection = selection.substring(0, 200) + '...';
            }
        }
        var quoteText = '<i>' + author + ' wrote: "' + selection + '"</i>\n\n';

        // use 'text()' to get the value, but 'val()' to set it, because carriage
        // returns are stripped by val()
        commentText.val(commentText.val() + quoteText);
        commentText.focus();
    });
});