Created
May 31, 2015 17:02
-
-
Save anonymous/0f395f42af57baefe5cd to your computer and use it in GitHub Desktop.
Previews a thread's opening post, & quotes previous poster.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name DuoForumHacks | |
// @namespace http://duolingo.wikia.com/ | |
// @version 1.0 | |
// @description Previews a thread's opening post, & quotes previous poster. | |
// @author Dessamator | |
// @include https://www.duolingo.com/* | |
// @grant Public domain | |
// ==/UserScript== | |
function inject(f) { //Inject the script into the document | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.setAttribute('name', 'DuoForumHacks'); | |
script.textContent = '(' + f.toString() + ')(jQuery)'; | |
document.head.appendChild(script); | |
} | |
inject(f); | |
function f($) { | |
function showPreview() { | |
var sComment =$(this).children('a').attr('href'); | |
if (sComment.match("comment")) { | |
var sUrl = $(location).attr('protocol') + $(this).children('a').attr('href').replace('comment', 'comments'); | |
var $hCurrLink = $(this); | |
if ($hCurrLink.attr("data-original-title") === undefined) { | |
$hCurrLink.attr({"data-placement": "auto", "data-original-title": "Loading preview"}); | |
$hCurrLink.tooltip('show'); | |
$.ajax({dataType: "json", url: sUrl}).done(function (data) { | |
var sMsg = (data.message.substring(0, 100) + " ..."); | |
if (data.translation) { | |
sMsg = (data.translation.substring(0, 100) + " ..."); | |
} | |
$hCurrLink.attr({"data-placement": "auto","data-original-title": sMsg}); | |
$('.tooltip').attr({"white-space": "pre-wrap","max-width": "200px","min-width":"200px"}); | |
$('.tooltip-inner').text(sMsg); | |
}).error(function (xhr, ajaxOptions, thrownError) { | |
$hCurrLink.attr({"data-placement": "auto","data-original-title": "Error ("+xhr.status+") loading preview. "}); | |
$('.tooltip-inner').text("Error ("+xhr.status+") loading preview. "); | |
}); | |
} | |
} | |
} | |
function loadQuotes($currComment){ | |
if( !$currComment.children(".footer a").hasClass('customquote')){ | |
if ($('#app').hasClass('community-topic') ) { | |
var $hQuote = ('<a class="customquote" href="javascript:;" >Quote</a>'); | |
$currComment.children(".footer .divider").before($hQuote); | |
} | |
} | |
$("div.footer").on('click','.customquote',function(e){ | |
var oParent= $(this).closest('li'); | |
var bCanReply = oParent.find('.footer a').hasClass('add-reply'); | |
var sPosterName=oParent.find('a.username')[0].innerHTML; | |
sPosterName = "["+sPosterName+"](/"+sPosterName+")"; | |
var sCommentText= oParent.find('bdi')[0].outerText; | |
if (!bCanReply) { | |
oParent = oParent.parent().closest('li'); | |
} | |
var sTextAreaId = oParent.find('.textarea-block:first'); | |
sTextAreaId.parent().attr('style','display:block;'); | |
$(sTextAreaId ).val(sPosterName+' wrote:\n\n'+sCommentText+'----'); | |
$(sTextAreaId ).attr('style','overflow: hidden; word-wrap: break-word; resize: none; height: '+($(sTextAreaId )[0].scrollHeight+4)+'px'); | |
}); | |
} | |
$('body').on("mouseenter", ".list-discussion-item-header", showPreview); | |
$('body').on("mouseenter", ".stream-item-content", showPreview); | |
//$('body').on('mouseover', ".discussion-comments-list-item", function (){ | |
// loadQuotes($(this)); | |
//}); | |
$('body').on('mouseover', ".footer", function (){ | |
loadQuotes($(this)); | |
}); | |
$('body').on('mouseover', ".add-reply", function (){ | |
loadQuotes($(this)); | |
}); | |
$('body').on('mouseleave', ".footer", function (){ | |
$('a.customquote').remove(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment