Greasy Fork

AO3: highlight fandoms in user page

Highlight favourite fandoms in user page

目前为 2021-04-11 提交的版本。查看 最新版本

// ==UserScript==
// @name         AO3: highlight fandoms in user page
// @namespace    ao3
// @version      1.0
// @description  Highlight favourite fandoms in user page
// @author       CertifiedDiplodocus
// @require      http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @match     http://archiveofourown.org/users/*
// @match     https://archiveofourown.org/users/*
// @exclude   http://archiveofourown.org/users/CertifiedDiplodocus/*
// @exclude   https://archiveofourown.org/users/CertifiedDiplodocus/*
// @grant        none
// ==/UserScript==

    //cannibalised from fangirlishness's ao3 Highlight tags, with thanks and apologies

/* eslint-env jquery */ //           allows jQuery
/* jshint esversion:6 */ //          allows "let"

(function($) {
    'use strict';

    /****** CONFIG ****************************************************/

    // Fill in your own username in the two @exclude lines above.

    // Favourite fandoms list (regexp can be used)
    let fandomsToHighlight = ["Original Work", "Critical Role", "Harry Potter", "Naruto", "Scooby Doo",
                              "Stranger Things", "Holmes", "^Avatar:"];

    // !!!! BUG: can't seem to escape \( or \). Might be the same for the other brackets.
    // !!!! Temporary workaround: use "." (= "any character" in regex).
//      fandomsToHighlight = ["The Avengers \(Marvel", "Doctor Who"]; // this breaks the script

    fandomsToHighlight = ["Original Work",
                          "Alice in Wonderland .2010.", "All Creatures Great and Small", "Attack on Titan",
                          "^Avatar:", "Black Books", "Blake's 7", "Broadchurch", "Buffy the Vampire Slayer",
                          "Cabin Pressure", "Cinderbrush", "Colbert", "Critical Role",
                          "Daily Show", "Discworld", "Dishonored .Video Games.", "Doctor Who", "Dresden Files",
                          "Fake News", "Firefly", "Fullmetal Alchemist", "Good Omens", "Gravity Falls", "Harry Potter", "Hornblower", "House M\.D\.",
                          "Indiana Jones", "Jonathan Strange & Mr Norrell", "Kung Fu Panda", "Life On Mars",
                          "MASH", "Mob Psycho 100", "Monty Python's Flying Circus", "Mushishi", "My Hero Academia",
                          "Marvel Cinematic Universe", "^Marvel$", //                             **MCU**
                          "The Avengers .Marvel", "The Avengers - Ambiguous", //                  **MCU**
                          "^Captain America", "Guardians of the Galaxy", "^Iron Man .Movies.", // **MCU**
                          "^Thor .Movies.", "^Doctor Strange", "^Captain Marvel", //              **MCU**
                          "Naruto", "One-Punch Man", "Pirates of the Caribbean", "Pokemon - All Media Types", "Pokemon .Anime.",
                          "Red Dwarf", "Rick and Morty", "Sagas of Sundry: Dread",
                          "Scoob and Shag", "Scooby.Doo", "Sherlock", "Sleepy Hollow", "Spider-Man: Into the Spider-Verse",
                          "^Star Trek$", "Star Trek: Alternate Original Series", "Star Trek: The Next Generation", "Star Trek: The Original Series",
                          "Star Wars", "Steven Universe", "Stranger Things", "Swallows and Amazons",
                          "Team Fortress 2", "The Brittas Empire", "The Magnus Archives", "The Thick of It",
                          "The Umbrella Academy", "The X-Files", "Tintin", "Welcome to Night Vale"];

    // Fandoms to highlight in a different colour (specify colour for each).
    //    - overrides the default colour
    //    - you can add fandoms here without removing them from the first list

    let fandomsInColour = {"Fandom1":"#fda7d1", // pink
                           "Fandom2":"#adf7d1", // light green
                           "^Putin RPF":"red", // regexp patterns can be used
                           "somethingelse": "blue", // named colors work too
                          "Naruto": "orange"
                          };

    // SOME NOTES ON REGEXP AND PATTERN MATCHING

    // By default, the search matches any string containing the search text:
    //     "Sherlock" matches "Sherlock (TV)", "Sherlock Holmes" and "Young Sherlock Holmes"
    // To match exactly that, use the regex symbols "^" (string start) and/or "$" (string end):
    //     "^Sherlock" matches "Sherlock Holmes" but not "Young Sherlock Holmes"
    //     "^Star Trek$" matches only "Star Trek", not "Star Trek: The Original Series" or "Star Trek: Picard"

    // SPECIAL CHARACTERS: If a fandom contains any of the following characters
    // . + * ? ^ $ ( ) [ ] { } | \
    // they must be preceded (escaped) with a backslash (e.g. "House M\.D\.")
    // for the script to work.

    // Use bold text, highlighting, or both?
    let boldIsOn = true;
    let highlightIsOn = true;
    let customHighlightIsOn = true; // enable or disable custom highlighting

    let highlightDefaultCol = 'LightYellow'; // default highlight colour

    /****************************************************************/

    $('.fandom.listbox.group li').each(function() { //do I actually need "li"?

        let $list = $(this);
        $list.find('a').each(function() {
            let $fandom = $(this);
            let text = $fandom.text();
//            let $fandomLine = $(this).closest("li"); //whole list item (including number of fics) - currently not formatted

            // alternative approach: run regexp on all fandoms ("Naruto"|"Harry Potter"|...) instead of iterating through the array
//            let pattern1 = new RegExp(fandomsToHighlight.join("|"));

            for (var i = 0; i<fandomsToHighlight.length; i++) {
                let pattern = new RegExp(fandomsToHighlight[i], "i", "g") //case-insensitive
                if(text.match(pattern) != null) {
                    if(highlightIsOn) highlightFandom($fandom, highlightDefaultCol)
                    if(boldIsOn) boldFandom($fandom)
                }
            }

            // fandoms with custom highlighting, if applicable
            if (customHighlightIsOn) {
                for (let key in fandomsInColour) {
                    let pattern2 = new RegExp(key, "i", "g") //case-insensitive
                    if(text.match(pattern2) != null) {
                        highlightFandom($fandom, fandomsInColour[key]);
                    }
                }
            }

        });
    });

    function highlightFandom($fandom, color) {
        $fandom.css('background-color', color);
    }
    function boldFandom($fandom) {
        $fandom.css('font-weight', 'bold');
    }
    function comboFormat($fandom, color, weight) { // should I specify defaults? e.g. weight = 'normal'
        $fandom.css({'background-color': color, 'font-weight': weight});
    }

})(jQuery);