123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
// ==UserScript==
// @name Quote in locked threads
// @namespace https://bitcointalk.org
// @version 0.1
// @description Save headache when quoting from locked threads
// @author minifrij
// @match https://bitcointalk.org/index.php?topic=*
// @require https://code.jquery.com/jquery-3.4.1.slim.min.js
// @grant none
// ==/UserScript==
function post2BB(input){
//This deletes all quotes in the post
//I do this because I can't be arsed with them.
//https://www.youtube.com/watch?v=Jne9t8sHpUc
//Maybe it's something I'll fix later, if no one else has a go
//Don't hold your breath though
input = input.replace(/<div class="quoteheader">.*?<\/div>/g, '');
input = input.replace(/<div class="quote">.*<\/div>/g, '');
//FFS (For Formatting Sake)
let output = "\n";
//Match and replace some basic tags
input = input.replace(/<br>/g, "\n");
input = input.replace(/<hr>/g, "[hr]");
input = input.replace(/<span class="BTC">BTC<\/span>/g, "[btc]");
input = input.replace(/<b>([\S|\s]*?)<\/b>/gm, "[b]$1[/b]");
input = input.replace(/<i>([\S|\s]*?)<\/i>/gm, "[i]$1[/i]");
input = input.replace(/<span style="text-decoration: underline;">([\S|\s]*?)<\/span>/gm, "[u]$1[/u]");
input = input.replace(/<del>([\S|\s]*?)<\/del>/gm, "[s]$1[/s]");
//I felt like a genius when I figured this out. I'm not very good with regex
//If someone tries to make a link go over several lines this will break, but what sort of a barbarian would do that?
//Is that even possible?
input = input.replace(/<a.*href="(.*?)">(.*?)<\/a>/g, '[url=$1]$2[/url]');
//I'll reason with you, this doesn't really work too good
//If the layout of your post is broken, this part is probably why
//However, so long as you're not quoting the post of a crazed lunatic (or minerjones) you're probably alright
input = input.replace(/<span style="font-size: (\d*pt) !important; line-height:.*;">([\S|\s]*?)<\/span>/g, '[size=$1]$2[/size]');
input = input.replace(/<span style="background-color: (.*?);">([\S|\s]*?)<\/span>/g, '[glow=$1,2,300]$2[/glow]');
input = input.replace(/<span style="color: (.*);">([\S|\s]*?)<\/span>/g, '[color=$1]$2[/color]');
//Deal with the images and the image proxy
//Sometimes this doesn't properly match an image. I don't know why.
images = input.match(/<img class="userimg.*?".*?>/gm);
console.log(images);
if(images !== null){
for(let i=0;i<images.length;i++){
let image = /<img.*src="https:\/\/ip\.bitcointalk\.org\/\?u=(.*?)&t=.*&c=.*".*>/g.exec(images[i]);
input = input.replace(images[i], '[img]' + decodeURIComponent(image[1]) + '[/img]')
}
}
//Lazily delete any other tags that are left behind
input = input.replace(/<.*?>|<\/.*?>/g, '');
//Add the cooked parsed-a to the pan
output += input;
//Season it
output += "\n";
//Plate it
return output;
}
(function() {
'use strict';
//This script uses jQuery because I didn't feel like spending 2 weeks writing this thing
//Feel free to remove it if you really want to, I wish you the best of luck
if($('a[href^="https://bitcointalk.org/index.php?action=logout;"]').length == 0){
return;
}
let topicIcon = $('img[src^="https://bitcointalk.org/Themes/custom1/images/topic/"]');
if(topicIcon.length == 0 || !topicIcon.attr('src').includes('locked')){
return;
}
//If you've got this far then you're both logged in and on a locked topic. Way to go, champ!
//Create the quote button
let quoteButton = '<a style="cursor:pointer;" class="fake-quote"><img src="https://bitcointalk.org/Themes/custom1/images/frostee/frostee_quote.png" alt="Reply with quote" class="reply_button" style="vertical-align: middle;"></a>';
$('div[id^="ignmsgbttns"]').prepend(quoteButton);
//Make said quote button do things
$('.td_headerandpost').on('click', '.fake-quote', function(){
let quoteBB = '';
let postArea = $(this).parents('.td_headerandpost');
//Get all the necessary stuff for building a quote
let quoteUser = postArea.prev().find('a[title^="View the profile of"]').text().trim();
let quoteLink = postArea.find('.subject a').attr('href').split('?')[1].trim();
let quoteDate = Math.round(new Date(postArea.find('.subject').next().text()).getTime()/1000);
//If the post has been edited, it'll run this
if(quoteDate.toString() == 'NaN'){
quoteDate = Math.round(new Date(postArea.find('.subject').next().find('span:first-child').text()).getTime()/1000);
}
//Build the quote
quoteBB += '[quote author=' + quoteUser + ' link=' + quoteLink + ' date=' + quoteDate + ']';
//Enjoy your meal.
quoteBB += post2BB(postArea.find('.post').html());
quoteBB += '[/quote]';
//Create a textarea, make it fill the post and put the quote inside
let quoteArea = '<textarea style="position:absolute;width:calc(100% - 6px);height:calc(100% - 6px);top:0;left:0;">' + quoteBB + '</textarea>';
//Shove it in place of the post
postArea.find('.post').attr('style', 'position:relative;overflow:hidden;min-height:200px;');
postArea.find('.post').append(quoteArea);
});
})();