// ==UserScript==
// @name AO3 author+tags quick-search
// @version 1.2
// @include https://archiveofourown.org/works*
// @include https://archiveofourown.org/chapters*
// @description Generates quick links from AO3 fics to more by the same author in the same fandom (or character/pairing/any other tag).
// @namespace rallamajoop
// @downloadURL none
// ==/UserScript==
/* Adds extra links to the tags section at the top of an AO3 work page, redirecting user to any use of same tags by the same author.
* ie. a quick way to find out if the writer of the great fic you just read has written anything else for the same fandom/pairing/trope/etc.
* If printCounts is set to 'true', code will also calculate and display the number of uses of each tag in the fandom/relationship/character sections.
* This will also make the links take longer to load, as code must pre-load each link individually. To limit load, I have not implemented this for freeform
* or other tags.
* Author-based search links are a bit of a hack, but most cases should now select correctly in the tags menu on the right. I've added a subheading identifing the tag to cover all bases.
*/
//Setting variables! Change these if you like
var printCounts = true; // setting this to false will prevent script for printing tag counts, which may decrease loading times and server load
var quickSearchText = "*"; // text for new links - change this if you want something other than an *. You can put TEXT to make it superscript
var autoSortResultsBy = ""; // valid options are "Kudos", "Word Count", "Date" or "" (none, usually defaults to date)
var importantTags = ["fandom","relationship","character"]; // these are the tags which the script prints counts for
//Proper code starts here
var loc = location.href;
var href="https://archiveofourown.org/works?utf8=%E2%9C%93&commit=Sort+and+Filter&work_search"; //[relationship_names]=";
var workPage=["https://archiveofourown.org/works/", "https://archiveofourown.org/chapters/"];
var scriptTag = "&greasemonkey"; //tag to flag that we've clicked an author quicktag link
var sortOpts = {"Kudos": "&work_search[sort_column]=kudos_count", "Word Count": "&work_search[sort_column]=word_count", "Date" : "&work_search[sort_column]=revised_at" };
if (isWorkPage(loc)) {
// Story page, add extra links user-based search to each tag
var h3=document.body.getElementsByClassName("byline heading");
var authors=h3.item(0).getElementsByTagName('a');
authors=parseAuthors(authors);
var allTags=document.body.getElementsByClassName("work meta group").item(0);
var dds = allTags.getElementsByTagName('dd');
var links = dds.item(1).getElementsByTagName('a');
for (var j=0; j=0; i--) {
for (var a=0; a1) {
text = "(" + count.toString() + ")";
}
else {
text = "(1)";
}
}
var toolTip = "'" + unformatTag(tag) + "' fic by " + author;
newlink.innerHTML = " " + text + "";
links.item(i).parentNode.appendChild(newlink);
}
}
}
}
}
else if (loc.includes(scriptTag)) {
// Quicktags search results page, add title to show which tag is in use
//var sTag = getBetween("work_search[relationship_names]=", loc, "&user_id=");
let sTag, tagType;
[tagType, sTag] = parseLoc(loc);
var main = document.getElementById("main");
var heading = main.getElementsByClassName("heading").item(0);
sTag = unformatTag(sTag);
var subTitle = "
Author QuickSearch: " + sTag;
var span=document.createElement('h3');
span.innerHTML = subTitle;
// const sortBys = sortLinks(loc);
// heading.parentNode.insertBefore(sortBys, heading.nextSibling); //not really necessary as of v1.2
heading.parentNode.insertBefore(span, heading.nextSibling);
checkTagBox(tagType, sTag);
}
//don't bother counting fics by orphan_account - these won't all be by same author anyway
function ignoreAuthor(author) {
if (author == "orphan_account") return true;
return false;
}
function getTagType(element) {
var name = element.className;
if (!name.includes(" tags")) return false;
name = name.replace(" tags", "");
return name;
}
//True if work or chapter
function isWorkPage(href) {
for (var i=0; i0) {
h=h[0];
var text = h.textContent;
if (text.includes(" of ")) {
text = getBetween(" of ", text, " Works");
return text; //could convert this into an integer, but not much point
}
}
return 20;
}
//May be multiple authors, may have pseudonyms. Returns as array
function parseAuthors(links) {
var authors = [""];
for (var i=0; i