Greasy Fork

MyAnonamouse Forum Gifter

Add 1000 gift button next to name on forum topics list

目前为 2022-07-29 提交的版本。查看 最新版本

// ==UserScript==
// @name         MyAnonamouse Forum Gifter
// @version      0.3
// @description  Add 1000 gift button next to name on forum topics list
// @author       Guillermo
// @match        https://www.myanonamouse.net/f*
// @icon         https://cdn.myanonamouse.net/favicon.ico
// @grant        none
// @license      MIT
// @namespace http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';
    const GIFT_AMOUNT = 1000;
    function send_gift(user_id){
        // Make a new XMLHttpRequest
        var xhr = new XMLHttpRequest();
        // get timestamp
        var timestamp = new Date().getTime();
        // Set the request method to GET
        xhr.open("GET",
        "https://www.myanonamouse.net/json/bonusBuy.php/"+timestamp+"?spendtype=gift&amount="+GIFT_AMOUNT+"&timestamp="+timestamp+"&giftTo="+user_id,
        true);
        // Set the request header to application/json
        xhr.setRequestHeader("Content-Type", "application/json");
        // Send the request
        xhr.send();
        // Handle the response
        xhr.onreadystatechange = function() {
            console.log(xhr);
            if (xhr.readyState == 4 && xhr.status == 200 ) {
                // parse response text
                var response = JSON.parse(xhr.responseText);
                // check if response is ok
                if(response.success == true){
                    // Find span
                    var span = document.querySelector('span[user_id="'+user_id+'"]');
                    // Increase counter on local storage ids dictionary
                    increase_user_count_localstorage(user_id);
                    // Update the button text
                    span.innerText = get_user_count_innerText(user_id);
                }
            }
        }
    }
    const IDS_KEY = "gifted_ids";
    function get_user_count_localstorage(user_id){
        var ids = localStorage.getItem(IDS_KEY);
        if(ids == null){
            return 0;
        }
        ids = JSON.parse(ids);
        var user_id_count = ids[user_id];
        if(user_id_count == null){
            return 0;
        }
        return user_id_count;
    }

    function increase_user_count_localstorage(user_id){
        var ids = localStorage.getItem(IDS_KEY);
        if(ids == null){
            ids = {};
        }
        else{
            ids = JSON.parse(ids);
        }
        if(ids[user_id] == null){
            ids[user_id] = 0;
        }
        ids[user_id]++;
        localStorage.setItem(IDS_KEY, JSON.stringify(ids));
    }
    function get_user_count_innerText(user_id){
        return " 🏆 (" + get_user_count_localstorage(user_id)*GIFT_AMOUNT + ")"
    }
    // Select every 5th td in the table
    var td_list = document.querySelectorAll('table.forumViewTable td:nth-child(5)');
    // Loop through the list of td's
    for (var i = 0; i < td_list.length; i++) {
        // Get the current td
        var td = td_list[i];
        // Get the current link
        var link = td.querySelector('a');
        // Get the current href ("/u/userid")
        var href = link.getAttribute('href');
        // Get the user id
        var user_id = href.substring(3);
        // Create new button
        var new_button = document.createElement('span');
        // Cursor pointer
        new_button.style.cursor = "pointer";
        // Set the text
        new_button.innerText = get_user_count_innerText(user_id);
        new_button.setAttribute('user_id', user_id);
        // Set the onclick function
        new_button.onclick = function(){
            var this_user_id = this.getAttribute('user_id');
            send_gift(this_user_id);
        }
        // Add next to the current alink
        td.appendChild(new_button);
    }
})();