nickbeeton / garminstuff

Stuff for bulk analysis of Garmin FIT data
6 stars 2 forks source link

Script crashing #1

Closed RickBullotta closed 5 years ago

RickBullotta commented 5 years ago

userscript.html?id=d00796f7-6710-4e9e-be0f-a4a0349dae00:22 Uncaught TypeError: Cannot read property 'innerHTML' of undefined at window.f (userscript.html?id=d00796f7-6710-4e9e-be0f-a4a0349dae00:22) at HTMLButtonElement.window.f1 (userscript.html?id=d00796f7-6710-4e9e-be0f-a4a0349dae00:131)

It appears that this code, which loops looking for a script containing 'end_index', never finds a matching script, and then the next line has an invalid reference because "i" == the array size of the scriptsorig array.

window.stufforig = window.scriptsorig[i].innerHTML.split("\"achievement_description\":");
RickBullotta commented 5 years ago

If you replace the code at the start of the window.f function with this, it seems to work. It basically checks to see if the HTML you are looking for is found or not. If so, it parses it, if not, it initializes the "*orig" arrays to empty ones.

window.scriptsorig = window.document.getElementsByTagName("script");
var found;

for (var i = 0; i < window.scriptsorig.length; i++){
    console.log(i);
    console.log(window.scriptsorig[i].innerHTML);

    if (window.scriptsorig[i].innerHTML.search("end_index") >= 0){
            found = true;
            break;
    }
}

if(found) {
    window.stufforig = window.scriptsorig[i].innerHTML.split("\"achievement_description\":");
    window.namesorig = Array(window.stufforig.length - 1);
    window.start_indicesorig = Array(window.stufforig.length - 1);
    window.end_indicesorig = Array(window.stufforig.length - 1);
    for (i = 1; i < window.stufforig.length; i++) // skip the first
    {
        window.namesorig[i-1] = window.stufforig[i].match(/\"name\":\"[^\"]+/g)[0].replace("\"name\":\"","")
        window.start_indicesorig[i-1] = window.stufforig[i].match(/start_index\":[0-9]+/g)[0].replace("start_index\":","")
        window.end_indicesorig[i-1] = window.stufforig[i].match(/end_index\":[0-9]+/g)[0].replace("end_index\":","")
    }
}
else {
    window.stufforig = Array(1);
    window.namesorig = Array(window.stufforig.length - 1);
    window.start_indicesorig = Array(window.stufforig.length - 1);
    window.end_indicesorig = Array(window.stufforig.length - 1);
}
nickbeeton commented 5 years ago

Legend, thank you for that fix! Yeah, I hadn't accounted for the case where there are no ebike segments to start with (facepalm). Presumably it'll also crash when there are no bike segments, but in that case the script will have nothing to do anyway so I'll leave that case for now. Pushing your fix now.