如何解释朋友

时间:2017-03-25 08:14:25

标签: javascript

我最近有一个第三方为一个新的废弃软件编写注册码生成器(以及更准确的一系列产品),他们用HTML / JavaScript编写了整个代码。我理解页面的HTML一面,使所有的视觉元素都有效;我想知道的是所有JS在幕后所做的事情,所以我可以更好地向我的朋友解释它。任何人都可以将以下代码分解为我可以向我的朋友解释的基本陈述吗?我真的不知道任何JS,所以这会给我一些信息。

function generate() {
var vc = 178890;
var edition = [
    [223987, 543238],
    [998732, 215588],
    [776490, 366591]
];
var module = [
    [322547, 226789],
    [632788, 129874],
    [399872, 112256],
    [200876, 679032],
    [666634, 188897],
    [877334, 766952],
    [222990, 777778],
    [229347, 543832]
];
var i;
var sum = 0;
var name = $('#name').val();
var addr = $('#address').val();
for (i = 0; i < name.length; i++) {
    sum += name.charCodeAt(i);
}
for (i = 0; i < addr.length; i++) {
    sum += addr.charCodeAt(i);
}
var s = 12*(sum + Math.floor(sum*67/12472));
if ($('#product').val() === 'avs') {
    s += 886;
} else {
    s += 288;
    if ($('#timed').prop('checked')) {
        reg = moment($('#regdate').val(), 'MM/DD/YYYY');
        exp = moment($('#expdate').val(), 'MM/DD/YYYY');
        var date_re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
        var valid = true;
        $('#invalidreg').html(reg.creationData().input.search(date_re) >= 0 && reg.isValid() ? '' : (valid = false, ' invalid date'));
        if (exp.creationData().input.search(date_re) >= 0 && exp.isValid()) {
            if (reg.isSameOrAfter(exp)) {
                $('#invalidexp').html('expiration date should be after registration date');
                valid = false;
            }
            else {
                $('#invalidexp').html('');
            }
        } else {
            $('#invalidexp').html('invalid date');
            valid = false;
        }
        if (!valid) {
            clear();
            return;
        }
        var epoch = moment('1800-12-28');
        s += 3*exp.diff(epoch, 'days') - 2*reg.diff(epoch, 'days') - 34;
    }
}
$('#serialnum').html(67*sum);
$('#versioncode').html(vc + s);
var row = $('#edition tr:nth-child(1)');
for (i = 0; i < 3; i++) {
    row.find('td:nth-child(2)').html(edition[i][0] + s);
    row.find('td:nth-child(3)').html(edition[i][1] + s);
    row = row.next();
}
row = $('#modules tr:nth-child(1)');
for (i = 0; i < 8; i++) {
    row.find('td:nth-child(2)').html(module[i][0] + s);
    row.find('td:nth-child(3)').html(module[i][1] + s);
    row = row.next();
}

注意:编写此代码的第三方开发人员在被要求提供“设计时注释以进一步记录所使用的流程时”时说“他没有任何此类注释”

1 个答案:

答案 0 :(得分:0)

这是一个脚本怪物的丑陋孩子。每分钟的咒骂率只是呈指数上升。

function generate() {

    // Random (arrays of) magic numbers (probably the key to the universe)

    var vc = 178890;
    var edition = [
        [223987, 543238],
        [998732, 215588],
        [776490, 366591]
    ];
    var module = [
        [322547, 226789],
        [632788, 129874],
        [399872, 112256],
        [200876, 679032],
        [666634, 188897],
        [877334, 766952],
        [222990, 777778],
        [229347, 543832]
    ];

    // Retrieving the values of Name & Address form inputs as "name" and "addr" with an 
    // external library called jQuery

    var name = $('#name').val();
    var addr = $('#address').val();


    // Beginning of a block to calculate "sum" (set sum to zero) - the next couple of lines
    // seem straightforward, I just have no idea what objective these incantations have. 

    var i;
    var sum = 0;

    // Iterate over all the letters in the "name", retrieve the letter's 
    // character code and increase sum by the value of that character code

    for (i = 0; i < name.length; i++) {
        sum += name.charCodeAt(i);
    }   

    // Iterate over all the letters in the "addr", retrieve the letter's 
    // character code and increase sum by the value of that character code

    for (i = 0; i < addr.length; i++) {
        sum += addr.charCodeAt(i);
    }

    // Do some more magic number cacluations with sum, to retrieve a value "s"

    var s = 12*(sum + Math.floor(sum*67/12472));

    // If the value of the "product" input is avs, increase the value of s by magic number 886

    if ($('#product').val() === 'avs') {
        s += 886;
    } else {

    // Otherwise, increase it by 288

        s += 288;

    // If the "timed" checkbox has been selected ... 

        if ($('#timed').prop('checked')) {

    // Use the external library "moment" to normalize the input data from "regdate" and "expdate" fields

            reg = moment($('#regdate').val(), 'MM/DD/YYYY');
            exp = moment($('#expdate').val(), 'MM/DD/YYYY');

    // As we don't trust the "moment" date validation, we#re also going to use a regex to check the 
    // validity of "regdate"

    // The regex checks if the date is formatted with slashes, such as: 23/12/2004

            var date_re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

    // Somewhere in the murky depths further down there's probably a global check, if the input data
    // is valid, so we need a global flag for it. 

            var valid = true;

    // Now we apply the regex we defined earlier and use moment's own "isValid" method to check if the
    // "reg" date is actually valid. 

    // If it's not, we inject an error message into the HTML element "invalidreg" and set the global
    // valid flag to false

    // If "reg" is a valid date, nothing happens. 

        $('#invalidreg').html(reg.creationData().input.search(date_re) >= 0 && reg.isValid() ? '' : (valid = false, ' invalid date'));

    // Now we also validate the "exp" date in the same manner ...

        if (exp.creationData().input.search(date_re) >= 0 && exp.isValid()) {

    //  ... but we also check, if it is the same as the "reg" date. If they are the same, we output
    // an error message into the "invalidexp" HTML element and set the "valid" flag to false

    // If they are not the same, we reset the contents of that HTML element. 

            if (reg.isSameOrAfter(exp)) {
                $('#invalidexp').html('expiration date should be after registration date');
                valid = false;
            } else {
                $('#invalidexp').html('');
            }

    // If "exp" is not valid we display an error message in the "invalidexp" HTML 
    // element (and set the valid flag to false)

        } else {
            $('#invalidexp').html('invalid date');
            valid = false;
        }

    // If the global flag is false (ie "exp" or "reg" date are invalid or the same) ..

        if (!valid) {

    // Call a "clear" method (not defined in this fragment of code) and cease further processing 
    // by calling a return

            clear();
            return;
        }

    // If everything is valid (*yay*) - define a new magic date on the 28/12/1800 
    // (who know's why this particular date)

        var epoch = moment('1800-12-28');

    // And we're back to magic number caclulations on the value of "s"
    // where we are adding 3 times the difference between the magic epoch date and the "exp" date
    // and then subtracting 2 times the difference between the epoch and the "reg" date. 
    // Oh, and subtractign 34 ... the *bleeep* knows why. 

        s += 3*exp.diff(epoch, 'days') - 2*reg.diff(epoch, 'days') - 34;
    }
}

// We now wet an HMTL element "serialnum" to the 67 times the value of "sum" 
// (which we calculated earlier)

$('#serialnum').html(67*sum);

// The version code is the value of vc (you can find it right at the top of this script ... it's 178890)
// plus the value of s. We display this value in an HTML element "versioncode"

$('#versioncode').html(vc + s);

// We now grab a new variable and set it to the first row in an HTML table element. 

var row = $('#edition tr:nth-child(1)');

// For the first three rows of the table (starting with the row we just defined), we set the values of 
// the 2nd and 3rd table cells to a random number calculation of "s" and "edition" 
// (edition is defined right at the top of the script)

for (i = 0; i < 3; i++) {
    row.find('td:nth-child(2)').html(edition[i][0] + s);
    row.find('td:nth-child(3)').html(edition[i][1] + s);
    row = row.next();
}

// Same thing for a different table, and with the "module" magic number. 

row = $('#modules tr:nth-child(1)');
for (i = 0; i < 8; i++) {
    row.find('td:nth-child(2)').html(module[i][0] + s);
    row.find('td:nth-child(3)').html(module[i][1] + s);
    row = row.next();
}