exclamations ([personal profile] exclamations) wrote in [community profile] ao3some 2020-09-15 04:59 am (UTC)

I know it's been ages since you posted this tool, but I started playing with it last night because it was driving me crazy that the summary feature would lose line breaks on a lot of fic because of inconsistent html used in the blockquote. I ended up with the code below, after banging my head for awhile. I suspect I will need to add more replacements to the list as I encounter more paragraph formatting, but this currently gives me reliable line breaks in my fic summaries!

I also added some modifications so that I will always only get the Fic Title and Author in the title, and also strip out any extraneous parts of the url, since I only ever want to bookmark the base AO3 url.

Sorry if any of this is clumsily done - I've never played with javascript before, but I thought I'd share what I came up with anyway.

javascript:(function($){
var q=location.href;
var base = q.split("\/");
q = base[0] + "\/\/" + base[2] + "\/" + base[3] + "\/" + base[4];

var p=document.title; 
var parts = p.split("-");
if ( parts[1].startsWith(" Chapter")) {parts[1] = parts[2]}
p = parts[0] + "-" + parts[1];

var d="";
if(document.getSelection){ 
	d=document.getSelection();
}
if ( d=="" ) {
	d+=$(".summary blockquote.userstuff").html() ;
	d=d.replace(/<\/p><p>/g, '\n\n');
	d=d.replace(/<\/p>/g, '\n');
	d=d.replace(/<p>|<i>|<\/i>|<b>|<\/b>/g, '');
	d=$.trim( d );
}

var options = {"wordcount_format_text":"(%words% words)","series_include_format_text":"[%series%]"};
if ( d != '' ) { d += '\n' }
let series = $(".work.meta .series .position").map(function() { return $.trim($( this ).text()) }).get().join( ", " );
if ( series != '' ) {
	series = options.series_include_format_text.replace('%series%', series );
	d += '\n' + series; 
} 

var wc = options.wordcount_format_text.replace('%words%', $(".stats dd.words").text() );
if ( d != '' ) { d += '\n' }
d += wc; 

void(open("https://pinboard.in/add?url=" + encodeURIComponent(q) + "&description=" + encodeURIComponent(d) + "&title=" + encodeURIComponent(p), "Pinboard", "toolbar=no,width=700,height=350"));
})(jQuery);

Post a comment in response:

If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting