Need help simplifying my Tampermonkey script

- 1 answer

Ad

I would like some help simplifying the code in my script. I have a lot of repetitive parts of my code to deal with which isn't easy to keep up with. Here's the code:

// ==UserScript==
// @run-at document-idle
// @name         my script
// @version      0.1
// @author       me
// @match        http://website.com/
// @grant        GM_addStyle
// @grant GM_xmlhttpRequest
// @require http://code.jquery.com/jquery-latest.js
// @require https://raw.githubusercontent.com/sunnywalker/jQuery.FilterTable/master/jquery.filtertable.js
// ==/UserScript==
/* jshint -W097 */

setInterval(bprs, 600000);
setTimeout(bprs, 4000);

function bprs() {

    {
        var accountname1 = $('#accountName1').text(); // points to the first account field - please add more depending on how many accounts you have 'accountName2,accountName3,4,5...'
        var credits1 = $('#credits1').text(); // points to the first credits field - please add more depending on how many accounts you have 'credits2,credits3,4,5...'
        var accountname2 = $('#accountName2').text();
        var credits2 = $('#credits2').text();
        var accountname3 = $('#accountName3').text();
        var credits3 = $('#credits3').text();
        var accountname4 = $('#accountName4').text();
        var credits4 = $('#credits4').text();
        var accountname5 = $('#accountName5').text();
        var credits5 = $('#credits5').text();
        var accountname6 = $('#accountName6').text();
        var credits6 = $('#credits6').text();
        var date = new Date();
        var data = "date=" + date + 
            // "&accountname1=" + accountname1 + "&credits1=" + credits1 new entires must be added depending on how many accounts you have
            "&accountname1=" + accountname1 + "&credits1=" + credits1 +
            "&accountname2=" + accountname2 + "&credits2=" + credits2 + 
            "&accountname3=" + accountname3 + "&credits3=" + credits3 + 
            "&accountname4=" + accountname4 + "&credits4=" + credits4 + 
            "&accountname5=" + accountname5 + "&credits5=" + credits5 +
            "&accountname6=" + accountname6 + "&credits6=" + credits6;
        //Changes must also be added to the PHP script
        $.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
            options.async = true;
        });

        $.ajax({
            url: 'http://mywebsite.com/submit.php', // point to the php file here
            async:false,
            type: "POST",
            dataType: "json",
            data: data,
            success: function (result) {
                JSON.parse(result);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr);
            }
        });
    }
}  

Ideally I would like the credits and account name entries shortened to loops which the user could configure how many accounts to find on the page without having to set up the credits entries.

Ad

Answer

Ad

When you have this kind of repetitive patterns, and especially with a natural index (account1, 2, 3...) you should really make use of arrays.

Here is how you could simplify the repetitive parts and make the quantity configurable with a variable:

function bprs() {
        // Change this variable depending on how many accounts you have
        var accountsCount = 6;                       

        var accounts = [];
        for (var n = 1; n <= accountsCount; n++) {
            // Using objects literals to group your data is a great way to make your code clearer
            accounts[n] = {                          
                name: $('#accountName' + n).text(),
                credits: $('#credits' + n).text()
            };
        }


        var date = new Date();
        var data = "date=" + date + 
            accounts.reduce(function (prev, account, n) {
                return prev + "&accountname" + n + "=" + account.name +
                              "&credits" + n + "=" + account.credits;
            }, '');


        //...

I assume you could also automate setting the accountsCount variable, for example if the account elements have a specific class attached to them:

var accountsCount = document.getElementsByClassName('SPECIFIC_CLASS').length;
Ad
source: stackoverflow.com
Ad