badge granter

automates the granting of a badge to multiple names

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         badge granter
// @include      https://www.imvu.com/catalog/web_manage_badges.php?action=grant-revoke&*
// @namespace    oribadgegranter
// @version      0.3
// @description  automates the granting of a badge to multiple names
// @author       ori@imvu
// @grant        GM_setValue
// @grant        GM_getValue
// persistent variables:  count, shouldRun, grantMessages, names
// ==/UserScript==

(function() {
    'use strict';
    
    //text area and buttons
    document.body.innerHTML = document.body.innerHTML + "<textarea id=\"textstuff\" name=\"textstuff\" rows=\"20\"cols=\"25\"></textarea> <br><input type=\"button\" value=\" send \" id=\"load\" />     <input type=\"button\" value=\" results \" id=\"results\" />";

    //event listeners for buttons
    document.getElementById("load").addEventListener ("click", loadNames, false);
    document.getElementById("results").addEventListener ("click", showResults, false);

    //testing output
    console.log('count: ' + GM_getValue('count'));
    console.log('shouldRun: ' + GM_getValue('shouldRun'));

    //populate grantMessages array with message from page
    //this is skipped while count is 0 so will populate last, first, second, ect-- solved in showResults
    if(document.getElementsByClassName('grant-message').length > 0) {
        GM_setValue('grantMessages'[GM_getValue('count')],document.getElementsByClassName('grant-message')[0].innerHTML);
    }

    //continue sending badges after page refresh
    if(GM_getValue('shouldRun',false)) sendBadges();


    //places names into array -- delimited by new line
    function loadNames() {

        var textArea = document.getElementById("textstuff");
        GM_setValue('names',textArea.value.split("\n"));
        console.log("successfully created array of names");

        GM_setValue('grantMessages', new Array(GM_getValue('names').length));

        sendBadges();

    }

    //iterates through array and calls form .send()
    function sendBadges() {
        GM_setValue('shouldRun', true);
        while (GM_getValue('count',0) < GM_getValue('names').length) {
            document.getElementById('grant-userid').value = GM_getValue('names')[GM_getValue('count',0)];
            GM_setValue('count',(GM_getValue('count',0)+1)); //count++

            if(GM_getValue('count') == GM_getValue('names').length) {
                resetValues();
            }

            document.querySelectorAll('input[type="submit"]')[0].click();
            break;

        }
    }

    //display contents of grantMessages -- shows imvu 'result' message
    function showResults() {   //to do:  if contents are "" don't clear run.
        var lastInArray = parseInt(GM_getValue('grantMessages').length)-1;  //element - not value

        if (GM_getValue('grantMessages'[0]) !== "") {

            for(var a=1; a < GM_getValue('grantMessages').length; a++) {
                document.getElementById('textstuff').innerHTML += GM_getValue('grantMessages'[a]) + '\n\n';
            }
            //the last value is added first to the array because it's skipped when count is 0 but added after resetValues() is called
            document.getElementById('textstuff').innerHTML += GM_getValue('grantMessages'[0]) + '\n\n';
            var b = 0;
            while(b < GM_getValue('grantMessages').length) {
                GM_setValue('grantMessages'[b],"");
                b++;
            }
        }
    }

    //clears out persistent variables
    function resetValues() {

        GM_setValue('count',0);
        GM_setValue('shouldRun',false);

        for(var i=0; i < GM_getValue('names').length; i++) {
            GM_setValue('names',"");
        }
    }

})();