Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name AO3 author+tags quick-search
- // @version 1.1
- // @include https://archiveofourown.org/works*
- // ==/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,
- * though 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, so the search tag WON'T be selected in the tags menu on the right.
- * I've added a subheading to identify the tag instead.
- */
- var quickSearchText = "*"; // text for new links - change this if you want something other than an * You can put <sup>TEXT</sup> to make it superscript
- var printCounts = true; // setting this to false will prevent script for printing tag counts, which may decrease loading times and server load
- 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/";
- var scriptTag = "&greasemonkey"; //tag to flag that we've clicked an author quicktag link
- if (loc.includes(workPage)) {
- // 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<dds.length; j++) {
- var tags = dds.item(j);
- if (getTagType(tags) == "stats") break;
- links = tags.getElementsByTagName('a');
- for (var i=links.length-1; i>=0; i--) {
- for (var a=0; a<authors.length; a++) {
- var author=authors[a];
- var tag = getTag(links.item(i).href);
- var newHref = buildSearch(tag);
- var text = quickSearchText;
- var newlink=document.createElement('span');
- if (printCounts && printCountFor(tags)) {
- var count = getNumFics(newHref);
- if (count>1) {
- text = "<b><sup>(" + count.toString() + ")<sup></b>";
- }
- else {
- text = "<sup>(1)<sup></b>";
- }
- }
- newlink.innerHTML = " <a href=\"" + newHref + "\">" + text + "</a>";
- 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=");
- var main = document.getElementById("main");
- var heading = main.getElementsByClassName("heading").item(0);
- sTag = unformatTag(sTag);
- sTag = " Author QuickSearch: " + sTag;
- var span=document.createElement('h3');
- span.innerHTML = sTag;
- heading.parentNode.insertBefore(span, heading.nextSibling);
- }
- function getTagType(element) {
- var name = element.className;
- name = name.replace(" tags", "");
- return name;
- }
- //Limit tag counts to fandom, relationship and character tags
- function printCountFor(element) {
- var name = getTagType(element);
- var types = ["fandom","relationship","character"];
- for (var i=0; i<types.length; i++) {
- if (types[i] == name) return true;
- }
- return false;
- }
- //Retrieve page as javascript object
- function getSourceAsDOM(url)
- {
- var xmlhttp=new XMLHttpRequest();
- xmlhttp.open("GET",url,false);
- xmlhttp.send();
- var parser=new DOMParser();
- return parser.parseFromString(xmlhttp.responseText,"text/html");
- }
- //Counts results of author+tag search
- function getNumFics(url) {
- var results = getSourceAsDOM(url);
- var works = results.getElementsByClassName("work index group")[0];
- if (works.children.length < 20) {
- return works.children.length;
- }
- var h=results.getElementsByTagName("h2");
- if (h.length>0) {
- 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;
- }
- function parseAuthors(links) {
- var authors = [""];
- for (var i=0; i<links.length; i++) {
- var text = links.item(i).toString();
- authors[i] = getBetween("/users/", text, "/pseuds");
- }
- return authors;
- }
- function getBetween(tag1, str, tag2) {
- var ret = str.split(tag1);
- if (ret.length<2) return "";
- ret=ret[1];
- ret = ret.split(tag2);
- if (ret.length<1) return "";
- ret = ret[0];
- return ret;
- }
- //Format tag to pass to search
- function getTag(href){
- var tag = getBetween("/tags/", href, "/works");
- tag = replaceAll(tag, "*s*","%2F");
- tag = replaceAll(tag, "*d*",".");
- tag = replaceAll(tag, "%20","+");
- return tag;
- }
- // Format tag from href for display in title
- function unformatTag(tag) {
- tag = replaceAll(tag, "*s*","/");
- tag = replaceAll(tag, "*a*","&");
- tag = replaceAll(tag, "*d*",".");
- tag = replaceAll(tag, "%2F","/");
- tag = replaceAll(tag, "+", " ");
- tag = replaceAll(tag, "%7C", "|");
- tag = replaceAll(tag, "%22", "\"");
- return tag;
- }
- function replaceAll(str, search, replacement) {
- return str.split(search).join(replacement);
- }
- function buildSearch(tag){
- var href="https://archiveofourown.org/works?utf8=%E2%9C%93&commit=Sort+and+Filter&work_search[relationship_names]="
- href+=tag + "&user_id=" + author + scriptTag;
- return href;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement