atob不会解码bota编码的base64字符串

时间:2018-07-05 14:02:28

标签: javascript base64

我正在尝试创建一个类似cookie clicker的游戏。我现在正尝试使用botaatob来创建和解码保存代码。我的保存代码是一个字符串,其中所有变量都用句点分隔。我可以使用bota对该字符串进行编码,而不会发生意外。将编码后的字符串放入base64 decode时,得到的结果是9999.1.0.0。当我转到网站(Localhost)并输入保存代码时,我得到

  

未捕获的DOMException:无法在“窗口”上执行“ atob”:要解码的字符串未正确编码。

我仅使用数字和句点,因此使用非ASCII字符应该不会有问题。有人可以帮忙吗?

index.js:

//on page load function
function onLoad() {
    var name = prompt("Please name your Universe");
}

//Clicking
var atoms = 9999;
var clickValue = 1;

function atomClick() {
  atoms = atoms + clickValue;
  document.getElementById("atoms").innerHTML = abbrNum(atoms , true , 2);
};

//upgrades
function upgradeClickValue () {
clickValue = clickValue * 2;
};

//Auto click modifiers
//create veriables
var elmts = 0;
var molecules = 0;

function buyElement() {
    var elementCost = Math.floor(10 * Math.pow(1.1,elmts));
    if(atoms >= elementCost) {
        elmts++;
        atoms = atoms - elementCost;
        document.getElementById('elmts').innerHTML = abbrNum(elmts , true , 2);
        document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
    };
    var nextECost = Math.floor(10 * Math.pow(1.1,elmts));
    document.getElementById('elementCost').innerHTML = abbrNum(nextECost , true , 2);
};

function buyMolecule() {
    var moleculeCost = Math.floor(100 * Math.pow(1.2,molecules));
    if(atoms >= moleculeCost) {
        molecules++;
        atoms = atoms - moleculeCost;
        document.getElementById('molecules').innerHTML = abbrNum(molecules , true , 2);
        document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
    };
    var nextMCost = Math.floor(100 * Math.pow(1.2,molecules));
    document.getElementById('moleculeCost').innerHTML = abbrNum(nextMCost , true , 2);
};

window.setInterval(function() {

    data = atoms + "." + clickValue + "." + elmts + "." + molecules;

    atoms = atoms + (elmts * 1) + (molecules * 2);
    document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);

    document.title  = "Atoms: " + abbrNum(atoms , true , 2);

}, 1000);

//round numbers
const COUNT_ABBRS = [ '', 'K', 'M', 'B', 'T', 'Q', 'Qi', 'Se', 'Sp', 'Ot', 'No', 'De'];

function abbrNum(count, withAbbr = false, decimals = 2) {
    const i     = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000));
    let result  = parseFloat((count / Math.pow(1000, i)).toFixed(decimals));
    if(withAbbr) {
        result += `${COUNT_ABBRS[i]}`; 
    }
    return result;
}

function reset() {
    atoms = 0;
    clickValue = 1;
    elmts = 0;
    elementCost = 10;
    molecules = 0;
    moleculeCost = 100;
}

//initalising save var
var data = atoms + "." + clickValue + "." + elmts + "." + molecules;
console.log(data);
var encodedString = btoa(data);


function save() {
    // Encode the String
    encodedString = btoa(data);
    console.log(encodedString);
    alert("Save Code: " + encodedString);
}
function load() {
    var pastedValue = prompt("Past Your Save Code Here.");
    // Decode the String
    if (pastedValue === "") {
        alert("You left the text box blank.");
    } else {
        if (pastedValue = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/) {
            var decodedString = atob(pastedValue);
            console.log(decodedString);
            var splitRslt = decodedString.split(".");
            console.log(splitRslt)
            atoms = splitRslt[0];
            clickValue = splitRslt[1];
            elmts = splitRslt[2];
            molecules = splitRst[3];
        } else {
            alert("Please enter a valid save code.");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

查看代码中的这一行:

if (pastedValue = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/) {

您正在为pastedValue分配一个正则表达式(并且此if的结果始终为true)。

您要做的就是在正则表达式上匹配pastedValue

if (pastedValue.match(/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/)) {
相关问题