// ==UserScript==
// @name sup/tg/ Archive Quote Functions
// @namespace https://greasyfork.org/users/2457-meganega
// @description Add Inline Quoting (mouse click) ~ Quote Preview (mouse hover) ~ Backlinks ~ Inline Image Expansion to suptg archives
// @include http://suptg.thisisnotatrueending.com/archive/*
// @require http://code.jquery.com/jquery-latest.min.js
// @version 1.03
// @grant none
// ==/UserScript==
$(document).ready(function(){
/* Create Backlinks */
$("blockquote a.quotelink").each(function(){
var t = $(this).attr("href").replace(/#p/,"");
var q = $(this).parent().attr("id").replace(/m/,"");
if($("#bl"+t).length == 0){
$("#pi"+t+" span.postNum.desktop").after( $("<div>").attr("id","bl"+t).addClass("backlink_") );
}
$("#bl"+t).append( $("<span>").attr("id","s"+t).append( $("<a>").attr("href","#p"+q).addClass("quotelink").append(">>"+q) ) );
});
/* Backlink Inline Quote */
$("div.backlink_ a.quotelink").click(function(event){
if (event.shiftKey) {
event.preventDefault();
window.location = this;
return ;
}
event.preventDefault();
var q = $(this).attr("href").replace(/#p/,"");
var t = $(this).parent().attr("id").replace(/s/,"");
if($(this).parents("#pi"+t).siblings("blockquote#m"+t).children("[id^=f"+q+"]").length) {
$(this).parents("#pi"+t).siblings("blockquote#m"+t).children("[id^=f"+q+"]").remove();
$(this).removeClass("qLinkOpen");
} else {
$(this).parents("#pi"+t).siblings("blockquote#m"+t).prepend($("#p"+q).clone(true).attr("id", "f"+ q +"t"+ t));
$(this).addClass("qLinkOpen");
$(this).parents("#pi"+t).siblings("blockquote#m"+t).children("[id^=f"+q+"]").find('[id$=t' + q + ']').remove();
}
});
/* Backlink Quote Preview */
$("div.backlink_ a.quotelink").hover(function(event){
var q = $(this).attr("href").replace(/#p/,"");
var t = $(this).parent().attr("id").replace(/s/,"");
$(this).parent().after($('#p' + q).clone().addClass('qPreview').attr("id",""));
$(this).parent().siblings('.qPreview').find('[id$=t' + q + ']').remove();
}, function(event){
$(".qPreview").remove();
});
});
/* Inline Quote */
$('blockquote a.quotelink').click(function (event) {
if (event.shiftKey) {
event.preventDefault();
window.location = this;
return ;
}
event.preventDefault();
var qPost = $(this).attr('href').replace('#p', '');
var tPost = $(this).parent().attr('id').replace(/[a-z]/, '');
if ($(this).next('[id^=f' + qPost + ']').length) {
$(this).next('[id^=f' + qPost + ']').remove();
$(this).removeClass("qLinkOpen");
} else {
if ($('#p' + qPost).hasClass('op')) {
$(this).after($('#p' + qPost).clone(true).attr('id', 'f' + qPost + 't' + tPost).addClass('reply'));
$(this).addClass("qLinkOpen");
} else {
$(this).after($('#p' + qPost).clone(true).attr('id', 'f' + qPost + 't' + tPost));
$(this).addClass("qLinkOpen");
$(this).next('[id^=f' + qPost + ']').find('[id$=t' + qPost + ']').remove();
}
}
});
/* Quote Preview */
$('blockquote a.quotelink').hover(function (event) {
var qPost = $(this).attr('href').replace('#p', '');
var tPost = $(this).parent().attr('id').replace(/[a-z]/, '');
var linkOffset = $("div#p"+tPost).children("blockquote#m"+tPost).children("a[href=#p"+qPost+"]").offset().left + 10;
if ($('#p' + qPost).hasClass('op')) {
$(this).before($('#p' + qPost).clone().addClass('qPreview reply').attr("id",""));
} else {
$(this).before($('#p' + qPost).clone().addClass('qPreview').attr("id",""));
$(this).siblings('.qPreview').find('[id$=t' + qPost + ']').remove();
}
var qpheight = $('.qPreview').height();
$('.qPreview').attr('style', 'margin-top: -' + qpheight / 2 + 'px !important; margin-left: '+ linkOffset +'px !important;');
var pb = ($(window).height() + $(window).scrollTop()) - ($('.qPreview').offset().top + qpheight);
var pt = $('.qPreview').offset().top - $(window).scrollTop();
qpheight = $('.qPreview').height();
if (pb < 0) {
var pos = qpheight / 2 - pb + 10;
$('.qPreview').attr('style', 'margin-top: -' + pos + 'px !important; margin-left: '+ linkOffset +'px !important;');
} else if (pt < 0) {
var pos = qpheight / 2 + pt - 10;
$('.qPreview').attr('style', 'margin-top: -' + pos + 'px !important; margin-left: '+ linkOffset +'px !important;');
}
}, function (event) {
$('.qPreview').remove();
});
/* Inline Image Expansion */
$("a.fileThumb").click(function(event){
event.preventDefault();
if($(this).hasClass("image_expanded")){
$(this).find(".expImage").remove();
$(this).children("img:first").css("display", "block");
$(this).removeClass("image_expanded");
} else {
var imgLink = $(this).attr("href");
var imgDim = $(this).siblings(".fileText").html().replace(/<a.*a>/,"").match(/[0-9]{1,6}x[0-9]{1,6}/);
var imgW = imgDim[0].replace(/x[0-9]{1,6}/,"");
var imgH = imgDim[0].replace(/[0-9]{1,6}x/,"");
if(imgW > $(window).width()){
imgH = (($(window).width() - ($(this).offset().left * 2)) / imgW) * imgH;
imgW = $(window).width() - ($(this).offset().left * 2);
}
$(this).children("img:first").css("display", "none");
$(this).addClass("image_expanded").children("img:first").after( $("<img>").attr({
"src" :imgLink,
"href" : imgLink
}).css({
"width" : imgW +"px",
"height" : imgH +"px"
}).addClass("expImage") );
}
});
var css = $("<link>").attr({
"rel": "stylesheet",
"type": "text/css",
"href": "data:text/css," +
".qPreview { position: absolute; font-size: 10pt; }" +
".postContainer, .postInfo:after { content: ''; display: block; clear: both; }" +
".backlink_ > span { font-size: 80%; margin: 0 0.4em 0 0; }" +
".backlink_ { display: inline-block; margin-left: 0.2em; }" +
"a.qLinkOpen{ opacity: 0.5; }" +
"div.reply{ border-style: solid; border-width: 1px; border-color: rgb(180,180,180) !important; }" +
"a.quotelink{ color: rgb(20,80,100) !important; }" +
".backlink_ span{ display: inline-block; }"
}).appendTo("head");