AO3 Random Nice Comments

Want to leave more kudos? Leave a random nice comment with the click of a button

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name         AO3 Random Nice Comments
// @namespace    http://tampermonkey.net/
// @version      1.0
// @license      MIT
// @description  Want to leave more kudos? Leave a random nice comment with the click of a button
// @match        *://*.archiveofourown.org/*
// @grant        none
// ==/UserScript==

let anonName = 'Anon';
let anonEmail = '[email protected]';

function niceComment() {
    let niceComments = [
        'Kudos! ♥',
        'I loved this!',
        'This was great ♥',
        '♥ ♥ ♥',
        '<3 <3 <3',
        'This is great ♥',
        'Loved this <3',
        'Thank you for sharing this ♥',
        'Kudos ♥'
        ]

    let n = Math.floor(Math.random() * niceComments.length)
    return niceComments[n]
}

function getInputsByValue(value) {
    // kudos button has a universal id but the comment button id is unique to the work
    var allInputs = document.getElementsByTagName("input");
    var results = [];
    for(var x=0;x<allInputs.length;x++) {
        if(allInputs[x].value == value) {
            results.push(allInputs[x]);
        }
    }
    return results;
}

(function() {
    'use strict';

    if (!getInputsByValue('Comment').length || !getInputsByValue('Kudos ♥').length) {
        return null;
    }

    var submitButton = getInputsByValue('Comment')[0]
    var kudosButton = getInputsByValue('Kudos ♥')[0]
    var workID = submitButton.id.split('_')[3]

    const extraKudosButton = document.createElement("button")
    extraKudosButton.textContent = 'Comment Kudos ♥'
    extraKudosButton.onclick = function() {
        if (document.querySelectorAll('#comment_name_for_' + workID).length) {
            document.querySelector('#comment_name_for_' + workID).value = anonName;
            document.querySelector('#comment_email_for_' + workID).value = anonEmail;
        }

        document.querySelector('#comment_content_for_' + workID).value = niceComment();
        submitButton.click();
        extraKudosButton.remove(); // prevent extra clicks
    }

    kudosButton.parentNode.parentNode.insertBefore(extraKudosButton,kudosButton.parentNode)

})();