Ellililunch AO3 Bookmark Maker

Automatically populates fic info in bookmark notes! Adds title, author, workID, and summary to any existing bookmark notes (but not if the summary has already been added!) Great for tracking bookmarks. Helpful for re-readers! Or to figure out what that deleted fic used to be!

目前為 2023-07-08 提交的版本,檢視 最新版本

// ==UserScript==
    // @name         Ellililunch AO3 Bookmark Maker
    // @namespace    Ellililunch AO3 Bookmark Maker
    // @description  Automatically populates fic info in bookmark notes! Adds title, author, workID, and summary to any existing bookmark notes (but not if the summary has already been added!) Great for tracking bookmarks. Helpful for re-readers! Or to figure out what that deleted fic used to be!
    // @version      0.4
    // @history      0.0 modified Bairdel's Bookmark Maker to autopopulate title, author, and summary
    // @history      0.1 added workID to autopopulated info (on suggestion of oliver t)
    // @history      0.2 added if-else if-else statement so existing notes that don't include fic info will get fic info added but if editing an existing bookmark that already has the fic info/summary in the notes it won't be duplicated (friendlier to rereaders)
    // @history      0.3 patched error where Anonymous accounts break the author variable on the suggestions of  w4tchdoge on greasyfork
    // @history      0.4 added nested if-else statement where if summary has already been added but without workID, workID will be added in front of the reread date.
    // @author       Ellililunch
    // @match        *archiveofourown.org/works/*
    // @match        *archiveofourown.org/series/*
                     // note oliver t has found that it did NOT work if you use the typical userscripts extension app (they downloaded Stay - Userscripts Extension which is a “Tampermonkey for Safari” thing) but also found changing @match to @include worked as well
    // @license      GNU GPLv3
// ==/UserScript==


    //////////THE BOOKMARK SAVIOR/////////////
    ////////////automatically adds title, author and summary above exisiting notes in bookmark////////////
    // new users - scroll right to the bottom

    (function() {
        // automatically checks the Private Bookmark checkbox. Set to false if you don't want this.
        document.getElementById("bookmark_private").checked = true;
        // keeps any bookmark notes you've made previously. Must be above the "Read: ".
        // this updates the date you last read the fic each time.
        var bookmarkNotes = (document.getElementById("bookmark_notes").innerHTML);

        ////////////////////////// customisations ///////////////////////////////// DO NOT WORRY ABOUT
        // get the current date. should be in local time. you could add HH:MM if you wanted.
        var currdate = new Date();
        var dd = String(currdate.getDate()).padStart(2, '0');
        var mm = String(currdate.getMonth() + 1).padStart(2, '0'); //January is 0
        var yyyy = currdate.getFullYear();
        var hh = String(currdate.getHours()).padStart(2, '0');
        var mins = String(currdate.getMinutes()).padStart(2, '0');
        // change to preferred date format
        var date;
        //date = dd + '/' + mm + '/' + yyyy + " " + hh + ":" + mins;
        date = mm + '/' + dd + '/' + yyyy; //this is the USA standard date format
        console.log(date);

        var author;
        var words;
        var status;
        var title;
        var summary;
        var series_notes;
        var lastChapter;
        var workID;

        workID = window.location.href.split("/")[4]; //the code was suggested by oliver t, and is useful if a work is deleted and you want to recreate the URL for the wayback machine

        /*
        What should it do?
        if story title, author, and info is already there, don't add anything
        if there are bookmark notes but no story info, add story info
        if it's a series, it should get all the titles + summaries?
                     i think the class for the series "summary" is notes
                     so this would be
                     var series_notes;
                     series_notes = = document.getElementsByClassName("notes")[0].innerHTML; // series "summary" attempt
        */

        // checks if series
        var seriesTrue = document.getElementsByClassName("current")[0];
        if (seriesTrue != undefined) {
            // options for series bookmark notes

            title = document.getElementsByTagName("h2")[0].innerHTML.trim();
            words = document.getElementsByClassName("stats")[2].getElementsByTagName("dd")[0].textContent;
            author = document.querySelectorAll('[rel="author"]')[0].innerHTML.trim(); // fic author
            summary = document.getElementsByClassName("series meta group.userstuff")[0].innerHTML; // series notes "summary" attempt

            /*var complete = document.getElementsByClassName("stats")[2].getElementsByTagName("dd")[2].textContent;
            var updated = document.getElementsByClassName("series meta group")[0].getElementsByTagName("dd")[2].textContent
            // var status
            if (complete == "No") {
                status = "Updated: " + updated;
            } else if (complete == "Yes") {
                status = "Completed: " + updated;
            }*/


        } else {
            // options for fics
            lastChapter = "Chapter " + document.getElementsByClassName("chapters")[1].innerHTML.split("/")[0];
            title = document.getElementsByClassName("title heading")[0].innerHTML.trim(); // fic name
            words = document.getElementsByClassName("words")[1].innerHTML; // fic wordcount
            author = document.querySelector("#workskin > .preface .byline").textContent.trim(); // new way of finding fic author regardless of Anonymous suggested by w4tchdog on greasyfork, this was the old way if that breaks it for you: document.querySelectorAll('[rel="author"]')[0].innerHTML.trim(); // old way of finding fic author
            summary = document.getElementsByClassName("summary")[0].innerHTML; // summary attempt

            /*// status i.e. Completed: 2020-08-23, Updated: 2022-05-08, Published: 2015-06-29
            if (document.getElementsByClassName("status").length != 0) {
                // for multichapters
                status = document.getElementsByClassName("status")[0].innerHTML + " " + document.getElementsByClassName("status")[1].innerHTML;
            } else{
                // for single chapter fics
                status = document.getElementsByClassName("published")[0].innerHTML + " " + document.getElementsByClassName("published")[1].innerHTML;
            }
            */
        }




        //////////////////// THIS IS THE BIT FOR YOU TO LOOK AT /////////////////////////////////////////////////////////////////////////////////////

        // puts it all together. feel free to change this format to whatever you like.
        // <br> puts the next text on a new line
        // options for variables are:
    // date = current date
    // current time
    // current chapter count of fic / current number of parts in series
    // title = title of fic
    // author = author of fic (note that author "Anonymous" breaks this, but "orphan_account" doesn't
    // word count of fic
    // summary = fic summary
    // bookmarkNotes = existing bookmark notes
    // newBookmarkNotes = this is the variable for the new bookmark notes
    // status of fic i.e. Completed: 2020-08-23, Updated: 2022-05-08, Published: 2015-06-29


        /////// examples //////////
        //    bookmarkNotes = bookmarkNotes + "<br>Last Read: " + date + "<br>Chapter " + lastChapter;
        //    bookmarkNotes = bookmarkNotes + "<br>Last Read: " + date + "<br>Chapter " + lastChapter + "<br><br>" + title + " by " + author;
        //    bookmarkNotes + "<br>Last Read: " + date + "<br>" + lastChapter + "<br><br>" + title + " by " + author + "<br>" + status;


        /////////////////SELECT YOU OPTIONS//////////////////////////////

        ///// if currently no bookmark notes, populates fic info. if there are bookmark notes that already has the fic info, keep the existing info + adds the current date as the reread date. if there are already bookmark notes, but no fic info, adds the fic info in front of the bookmark notes
        if (bookmarkNotes === "") {  ///learned this from: https://stackoverflow.com/questions/154059/how-do-i-check-for-an-empty-undefined-null-string-in-javascript
        // if no existing bookmarks, then add all info:
            var newBookmarkNotes = title + " by " + author + " (workID: " + workID + ")<br>" + summary + "<br><br>";

        } else if (bookmarkNotes.includes("Summary:")) {    ///learned this from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
            if (bookmarkNotes.includes("orkID:")) {
                var newBookmarkNotes = bookmarkNotes + "<br>" + "Reread: " + date; ///trying to see if I can add reread date to existing bookmark notes... we'll see babes
            } else {
                var newBookmarkNotes = bookmarkNotes + "<br>" + "WorkID: " + workID + "<br>" + "Reread: " + date;
            }

        } else {    ///learned this from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
            var newBookmarkNotes = title + " by " + author + " (workID: " + workID + ")<br>" + summary + "<br><br>" + bookmarkNotes;
        }

        // fills in the bookmark notes box.
        document.getElementById("bookmark_notes").innerHTML = newBookmarkNotes;
    })();