Surye / mfp-keto-userscript

MyFitnessPal Keto Script
164 stars 54 forks source link

"Undefined" lines in My Fitness Pal Keto script, not running, please help thanks! #51

Closed dsarah3 closed 5 years ago

dsarah3 commented 5 years ago

Hi, I installed the mfpketo.user.js and there are a number of "not running" yellow triangles in the script. Does anyone have a solution, please? Thanks!

ghost commented 5 years ago

You can ignore them - the script runs fine anyway.

However, there is a bug in the script in the way it calculates the Total Net Carbs. If you are interested I can send you an updated version that fixes the bug.

Surye commented 5 years ago

@pauljhobbs if you think you have a fix, I'd be happy to look at a PR

Surye commented 5 years ago

number of "not running" yellow triangles in the script

This is because there's two scripts injected in runtime, google's API and jquery, so the Tapermonkey UI doesn't know about them, but this shouldn't be a problem.

dsarah3 commented 5 years ago

Why is this happening? Apparently it used to be functional. Someone messed with it?:( Is there anyone who can fix it?

Surye commented 5 years ago

It is working... does your diary page not show net carbs?

dsarah3 commented 5 years ago

No, I have it installed, and uninstalled and reinstalled several times. No net carbs showing. @pauljhobbs would love for you to share the fix, thanks!

Surye commented 5 years ago

When you are on the diary page, and you click the Tapermonkey icon, does it show the script is enabled and running for that page:

image

dsarah3 commented 5 years ago

Got it now, thanks so much! Last night it didn't work, just reinstalled and it's all there:) Much appreciation:) On Thursday, January 24, 2019, 12:11:21 PM PST, Vincent Castellano notifications@github.com wrote:

When you are on the diary page, and you click the Tapermonkey icon, does it show the script is enabled and running for that page:

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

dsarah3 commented 5 years ago

So happy, thanks again!

Surye commented 5 years ago

Glad it got worked out!

ghost commented 5 years ago

Here is a link to my fixed version. There is probably a better way to fix the calculation issue, but this seems to work. https://drive.google.com/file/d/1Q9XezLJHs4HxogI73ynSETR2EZYoOS-w/view?usp=sharing

ghost commented 5 years ago

Hi Vincent,

This is probably not the most elegant of fixes, but it seems to work.

https://drive.google.com/file/d/1Q9XezLJHs4HxogI73ynSETR2EZYoOS-w/view?usp=sharing

Cheers,

Paul +61 431 986 756

On Fri, 25 Jan 2019 at 04:34, Vincent Castellano notifications@github.com wrote:

@pauljhobbs https://github.com/pauljhobbs if you think you have a fix, I'd be happy to look at a PR

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-457306983, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOdguzM20_3SxXIBJPCier7lMwX7jks5vGfyYgaJpZM4aNmUF .

dsarah3 commented 5 years ago

Hi Paul! The script suddenly stopped working, any ideas? I had a red square notification with #1 in it on TamperMonkey, script was disabled. I re-enabled it, no red notification anymore, I quit Chrome and restarted. Still not working. Please help if you can! I know I can just subtract the fiber from the carbs, but it's so much nicer having the script running:) Thanks!Sarah

On Thursday, January 24, 2019, 1:20:34 PM PST, pauljhobbs notifications@github.com wrote:

Hi Vincent,

This is probably not the most elegant of fixes, but it seems to work.

https://drive.google.com/file/d/1Q9XezLJHs4HxogI73ynSETR2EZYoOS-w/view?usp=sharing

Cheers,

Paul +61 431 986 756

On Fri, 25 Jan 2019 at 04:34, Vincent Castellano notifications@github.com wrote:

@pauljhobbs https://github.com/pauljhobbs if you think you have a fix, I'd be happy to look at a PR

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-457306983, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOdguzM20_3SxXIBJPCier7lMwX7jks5vGfyYgaJpZM4aNmUF  .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

ghost commented 5 years ago

Hi Vincent,

I've tested the new version of the script (v1.16), and I think there is still an issue. It is now adding the Net Carbs correctly, but the Daily Goal is wrong, which is throwing out the Remaining value. I have taken two screenshots of the same day for me - one is with version 1.16, and the other is with my modified version. You'll notice I have also shifted the position of the Net Carbs column - I think it makes more sense to have it to the right of the Carbs and Fibre columns.

[image: image.png]

Using the script with my modifications, the same day looks like this:

[image: image.png]

I have attached the modified script for your reference (added txt extension so Google would allow it to be attached).

Cheers,

Paul +61 431 986 756

On Fri, 25 Jan 2019 at 04:34, Vincent Castellano notifications@github.com wrote:

@pauljhobbs https://github.com/pauljhobbs if you think you have a fix, I'd be happy to look at a PR

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-457306983, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOdguzM20_3SxXIBJPCier7lMwX7jks5vGfyYgaJpZM4aNmUF .

// ==UserScript== // @name MyFitnessPal Percentages and Net Carbs // @version 1.16 // @namespace surye // @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs" // @downloadURL https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js // @include http://www.myfitnesspal.com/food/diary // ==/UserScript==

/*

function exec(fn) { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up }

function startRun() { // Load Google API for Charts var script = document.createElement("script"); script.setAttribute("src", "//www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script);

// Load jQuery
script = document.createElement("script");
script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js");
script.addEventListener('load', function() {
    exec("jQuery.noConflict()");
}, false);
document.body.appendChild(script);

// Inject this script into page.
script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = main;
document.body.appendChild(script);

}

startRun();

function jsapiLoaded() { google.load("visualization", "1", { packages: ["corechart"], "callback": main }); }

function main() { //phobbs 2019-01-23: new variables to track running totals var carbs_running_total = 0; // Tracks the carbs running total var fibre_running_total_lt = 0; // Tracks the fiber when carbs < fibre var fibre_running_total_gte = 0; // Tracks the fiber when carbs >= fibre var net_carbs_new = 0; // Calculation: net_carbs_new = carbs_running_total - (fibre_running_total_gte - fibre_running_total_lt)

var calories_i = 0;
var net_carbs_i = 0;
var carbs_i = 0;
var fiber_i = 0;
var protein_i = 0;
var fat_i = 0;

var daily_total_carbs = 0;
var daily_total_protein = 0;
var daily_total_fat = 0;

var net_carb_total = 0;
var net_carb_total_goal = 0;

var header_tr_element = jQuery('.food_container tr.meal_header:first');

var elem_i = 0;
header_tr_element.find('td').each(function() {
    var myval = jQuery(this).text().toLowerCase().trim();
    if (myval.indexOf('calories') !== -1) { calories_i = elem_i; }
    if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; }
    if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; }
    if (myval.indexOf('fat') !== -1) { fat_i = elem_i; }
    if (myval.indexOf('protein') !== -1) { protein_i = elem_i; }

    elem_i += 1;
});

// Add new column for net carbs
var net_carb_tr_elements = jQuery('tr');
net_carb_tr_elements.each(function() {
    var tds = jQuery(this).find('td');
    jQuery('<td></td>').insertBefore(tds.eq(fat_i));

});

// Recalculate offsets
net_carbs_i = fat_i;
calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i;
carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i;
fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i;
protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i;
fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i;

// Set header
header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs");
header_tr_element.find('td').eq(net_carbs_i).addClass("alt");
header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs
var footer_tr_element = jQuery('tfoot tr');
footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs");
footer_tr_element.find('td').eq(net_carbs_i).addClass("alt");
header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0];
var alreadyCountedFiberIdx = 0;
var food_tr_elements = jQuery('tr');

food_tr_elements.each(function() {
    if($(this).hasClass('bottom')) {
        alreadyCountedFiberIdx++;
        alreadyCountedFiber[alreadyCountedFiberIdx] = 0;
    }

    var tds = jQuery(this).find('td');
    var carbs = parseFloat(tds.eq(carbs_i).text());
    var fiber = parseFloat(tds.eq(fiber_i).text());

    // Find only food rows!
    var delete_td = tds.eq(tds.length - 1);
    if (delete_td.hasClass('delete')) {
        var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase();

        tds.eq(net_carbs_i).text(carbs - fiber);
        //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs >= fibre (not Totals)
        carbs_running_total = carbs_running_total + carbs;    // phobbs
        fibre_running_total_gte = fibre_running_total_gte + fiber;    // phobbs
        if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) {
            alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber);
            tds.eq(net_carbs_i).text(carbs);
            //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs < fibre (not Totals)
            fibre_running_total_lt = fibre_running_total_lt + fiber;    // phobbs
        }
    }
});

var totalAlreadyCountedFiber = 0;
for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total');
var meal_idx = 0;
var nctg_test = 0;  // phobbs
bottom_tr_elements.each(function() {

    if (jQuery(this).hasClass('remaining')) {
        return false; /* continue */
    }

    var tds = jQuery(this).find('td');
    var cals = parseFloat(tds.eq(calories_i).text());
    var carbs = 0;
    if($(this).hasClass('bottom')) {
        var carbs = parseFloat(tds.eq(carbs_i).text()) + alreadyCountedFiber[meal_idx];
    } else {
        var carbs = parseFloat(tds.eq(carbs_i).text());
    }
    var fiber = parseFloat(tds.eq(fiber_i).text());
    var protein = parseFloat(tds.eq(protein_i).text());
    var fat = parseFloat(tds.eq(fat_i).text());

    var net_carbs = carbs;
    //var net_carbs = carbs - totalAlreadyCountedFiber; // phobbs => affects "Your Daily Goal" Total ONLY
    //net_carbs = carbs_running_total;
    //net_carbs = fibre_running_total_lt;
    //net_carbs = fibre_running_total_gte;
    //net_carbs = fiber;
    net_carbs_new = carbs_running_total - (fibre_running_total_gte - fibre_running_total_lt); // phobbs This is the correct formula

    // HACK to show net carbs
    if (!jQuery(this).hasClass('alt')) {
        net_carbs = carbs - fiber; // phobbs *** Bottom "Totals" for Net Carbs
        if (!isNaN(net_carbs)) {
            tds.eq(net_carbs_i).text(net_carbs);
            //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect
        } else if (jQuery(this).hasClass("total")) {
            tds.eq(net_carbs_i).text("0");
            //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect
            //nctg_test = nctg_test +  net_carbs;    // phobbs
        }
    } else {
        // record goal
        net_carb_total_goal = net_carbs - totalAlreadyCountedFiber ;  // Bottom Total "Remaining" for Net Carbs
        //net_carb_total_goal = nctg_test;  // phobbs => Will affect Meal Rows Totals ONLY for Net Carbs (not Meal Rows)
    }

    /* do nothing if cannot calculate for the row */
    if (isNaN(cals) ||
        isNaN(carbs) ||
        isNaN(protein) ||
        isNaN(fat) ||
        isNaN(fiber) ||
        isNaN(net_carbs) ||
        cals === 0) {
        meal_idx++;
        return true;

    }

    //tds.eq(net_carbs_i).text(net_carbs);

    //tds.eq(net_carbs_i).text(tds.length);  // phobbs => Totals ONLY for Net Carbs (except "Remaining")
    if (jQuery(this).hasClass('total') &&
        !jQuery(this).hasClass('total alt')) {
        tds.eq(net_carbs_i).text(net_carbs_new);  // phobbs => Totals ONLY for Net Carbs (except "Remaining")
        } else {
        tds.eq(net_carbs_i).text(net_carbs);
        //tds.eq(net_carbs_i).text(999);  // phobbs => Totals ONLY for Net Carbs (except "Remaining")
        }
    // if (net_carbs == 0 &&
    //     protein == 0 &&
    //     fat == 0) {
    //     return true;
    // }

    var carb_cals = (net_carbs * 4);
    var protein_cals = (protein * 4);
    var fat_cals = (fat * 9);

    if (jQuery(this).hasClass('total') &&
        !jQuery(this).hasClass('alt') &&
        daily_total_carbs === 0) {

        daily_total_carbs = carb_cals;
        daily_total_protein = protein_cals;
        daily_total_fat = fat_cals;
        net_carb_total = net_carbs - totalAlreadyCountedFiber; // phobbs Bottom Total "Remaining" for Net Carbs

    }

    var real_cals = carb_cals + protein_cals + fat_cals;

    var carb_pct = ((carb_cals / real_cals) * 100).toFixed(2);
    var fat_pct = ((fat_cals / real_cals) * 100).toFixed(2);
    var protein_pct = ((protein_cals / real_cals) * 100).toFixed(2);

    //alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total);

    carb_pct = Math.round(carb_pct);
    fat_pct = Math.round(fat_pct);
    protein_pct = Math.round(protein_pct);

    tds.each(function() {
        jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;">&nbsp;</div>');
    });

    tds.eq(0).find('div.myfp_us').html("");

    /*tds.eq(calories_i).find('div.myfp_us').html(real_cals);*/

    if (!isNaN(carb_pct)) {
        tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%");
    }

    if (!isNaN(fat_pct)) {
        tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%");
    }

    if (!isNaN(protein_pct)) {
        tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%");
    }

    meal_idx++;
});

var remaining_tr_elements = jQuery('.food_container tr.total.remaining');

remaining_tr_elements.each(function() {

    // Show remaining as net carbs
    var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber;
    //net_carbs = 222;  // phobbs affects "Remaining Total" ONLY
    var tds = jQuery(this).find('td');
    tds.eq(net_carbs_i).text(parseInt(net_carbs));

    // Fix color
    tds.eq(net_carbs_i).removeClass("positive");
    tds.eq(net_carbs_i).removeClass("negative");

    if (net_carbs < 0) {
        tds.eq(net_carbs_i).addClass("negative");
    } else {
        tds.eq(net_carbs_i).addClass("positive");
    }

});

if (daily_total_carbs !== 0 ||
    daily_total_protein !== 0 ||
    daily_total_fat !== 0) {

    jQuery('.food_container').append('<div id="google_graph_1"></div>');

    var data1 = new google.visualization.DataTable();
    data1.addColumn('string', 'Type');
    data1.addColumn('number', 'Cals');
    data1.addRows(
        [
            ['Net Carbs', daily_total_carbs],
            ['Protein', daily_total_protein],
            ['Fat', daily_total_fat]
        ]
    );

    var chart = new google.visualization.PieChart(document.getElementById('google_graph_1'));
    chart.draw(data1, {
        width: 350,
        height: 300,
        title: 'Daily Totals by Calories (This is what you use for your macro ratios)'
    });
    document.getElementById('google_graph_1').style.cssFloat = "left";

    jQuery('.food_container').append('<div id="google_graph_2"></div>');

    var carb_grams = daily_total_carbs / 4;
    var pro_grams = daily_total_protein / 4;
    var fat_grams = daily_total_fat / 9;

    var data2 = new google.visualization.DataTable();
    data2.addColumn('string', 'Type');
    data2.addColumn('number', 'Grams');
    data2.addRows(
        [
            ['Net Carbs (' + carb_grams + 'g)', carb_grams],
            ['Protein (' + pro_grams + 'g)', pro_grams],
            ['Fat (' + fat_grams + 'g)', fat_grams]
        ]
    );

    var chart2 = new google.visualization.PieChart(document.getElementById('google_graph_2'));
    chart2.draw(data2, {
        width: 350,
        height: 300,
        title: 'Daily Totals by Grams'
    });
    document.getElementById('google_graph_2').style.cssFloat = "right";
}

}

dsarah3 commented 5 years ago

Hi Paul,  Any update on the current issue with the script? I've reinstalled MyFitnessPal a few times and this is still happening (after it worked perfectly for a while and I loved it:):

I've tested the new version of the script (v1.16), and I think there is still an issue. It is now adding the Net Carbs correctly, but the Daily Goal is wrong, which is throwing out the Remaining value. I have taken two screenshots of the same day for me - one is with version 1.16, and the other is with my modified version. You'll notice I have also shifted the position of the Net Carbs column - I think it makes more sense to have it to the right of the Carbs and Fibre columns.

[image: image.png]

Using the script with my modifications, the same day looks like this:

[image: image.png]

I have attached the modified script for your reference (added txt extension so Google would allow it to be attached).

Cheers,

Paul +61 431 986 756

On Fri, 25 Jan 2019 at 04:34, Vincent Castellano notifications@github.com wrote:

@pauljhobbs https://github.com/pauljhobbs if you think you have a fix, I'd be happy to look at a PR

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-457306983, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOdguzM20_3SxXIBJPCier7lMwX7jks5vGfyYgaJpZM4aNmUF .

// ==UserScript== // @name MyFitnessPal Percentages and Net Carbs // @version 1.16 // @namespace surye // @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs" // @downloadURL https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js // @include http://www.myfitnesspal.com/food/diary // ==/UserScript==

/*

function exec(fn) { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up }

function startRun() { // Load Google API for Charts var script = document.createElement("script"); script.setAttribute("src", "//www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script);

// Load jQuery script = document.createElement("script"); script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script);

// Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script); }

startRun();

function jsapiLoaded() { google.load("visualization", "1", { packages: ["corechart"], "callback": main }); }

function main() { //phobbs 2019-01-23: new variables to track running totals var carbs_running_total = 0; // Tracks the carbs running total var fibre_running_total_lt = 0; // Tracks the fiber when carbs < fibre var fibre_running_total_gte = 0; // Tracks the fiber when carbs >= fibre var net_carbs_new = 0; // Calculation: net_carbs_new = carbs_running_total - (fibre_running_total_gte - fibre_running_total_lt)

var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0;

var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0;

var net_carb_total = 0; var net_carb_total_goal = 0;

var header_tr_element = jQuery('.food_container tr.meal_header:first');

var elem_i = 0; header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; }

elem_i += 1; });

// Add new column for net carbs var net_carb_tr_elements = jQuery('tr'); net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(fat_i));

});

// Recalculate offsets net_carbs_i = fat_i; calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i; carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i; fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i; protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i; fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i;

// Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs var footer_tr_element = jQuery('tfoot tr'); footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0]; var alreadyCountedFiberIdx = 0; var food_tr_elements = jQuery('tr');

food_tr_elements.each(function() { if($(this).hasClass('bottom')) { alreadyCountedFiberIdx++; alreadyCountedFiber[alreadyCountedFiberIdx] = 0; }

var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text());

// Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase();

tds.eq(net_carbs_i).text(carbs - fiber); //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs >= fibre (not Totals) carbs_running_total = carbs_running_total + carbs; // phobbs fibre_running_total_gte = fibre_running_total_gte + fiber; // phobbs if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) { alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber); tds.eq(net_carbs_i).text(carbs); //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs < fibre (not Totals) fibre_running_total_lt = fibre_running_total_lt + fiber; // phobbs } } });

var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total'); var meal_idx = 0; var nctg_test = 0; // phobbs bottom_tr_elements.each(function() {

if (jQuery(this).hasClass('remaining')) { return false; / continue / }

var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { var carbs = parseFloat(tds.eq(carbs_i).text()) + alreadyCountedFiber[meal_idx]; } else { var carbs = parseFloat(tds.eq(carbs_i).text()); } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text());

var net_carbs = carbs; //var net_carbs = carbs - totalAlreadyCountedFiber; // phobbs => affects "Your Daily Goal" Total ONLY //net_carbs = carbs_running_total; //net_carbs = fibre_running_total_lt; //net_carbs = fibre_running_total_gte; //net_carbs = fiber; net_carbs_new = carbs_running_total - (fibre_running_total_gte - fibre_running_total_lt); // phobbs This is the correct formula

// HACK to show net carbs if (!jQuery(this).hasClass('alt')) { net_carbs = carbs - fiber; // phobbs *** Bottom "Totals" for Net Carbs if (!isNaN(net_carbs)) { tds.eq(net_carbs_i).text(net_carbs); //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect //nctg_test = nctg_test + net_carbs; // phobbs } } else { // record goal net_carb_total_goal = net_carbs - totalAlreadyCountedFiber ; // Bottom Total "Remaining" for Net Carbs //net_carb_total_goal = nctg_test; // phobbs => Will affect Meal Rows Totals ONLY for Net Carbs (not Meal Rows) }

/ do nothing if cannot calculate for the row / if (isNaN(cals) || isNaN(carbs) || isNaN(protein) || isNaN(fat) || isNaN(fiber) || isNaN(net_carbs) || cals === 0) { meal_idx++; return true;

}

//tds.eq(net_carbs_i).text(net_carbs);

//tds.eq(net_carbs_i).text(tds.length); // phobbs => Totals ONLY for Net Carbs (except "Remaining") if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('total alt')) { tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => Totals ONLY for Net Carbs (except "Remaining") } else { tds.eq(net_carbs_i).text(net_carbs); //tds.eq(net_carbs_i).text(999); // phobbs => Totals ONLY for Net Carbs (except "Remaining") } // if (net_carbs == 0 && // protein == 0 && // fat == 0) { // return true; // }

var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat * 9);

if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('alt') && daily_total_carbs === 0) {

daily_total_carbs = carb_cals; daily_total_protein = protein_cals; daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; // phobbs Bottom Total "Remaining" for Net Carbs

}

var real_cals = carb_cals + protein_cals + fat_cals;

var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) * 100).toFixed(2);

//alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total);

carb_pct = Math.round(carb_pct); fat_pct = Math.round(fat_pct); protein_pct = Math.round(protein_pct);

tds.each(function() { jQuery(this).append('

 
'); });

tds.eq(0).find('div.myfp_us').html("");

/tds.eq(calories_i).find('div.myfp_us').html(real_cals);/

if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); }

if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); }

if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); }

meal_idx++; });

var remaining_tr_elements = jQuery('.food_container tr.total.remaining');

remaining_tr_elements.each(function() {

// Show remaining as net carbs var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; //net_carbs = 222; // phobbs affects "Remaining Total" ONLY var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs));

// Fix color tds.eq(net_carbs_i).removeClass("positive"); tds.eq(net_carbs_i).removeClass("negative");

if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); }

});

if (daily_total_carbs !== 0 || daily_total_protein !== 0 || daily_total_fat !== 0) {

jQuery('.food_container').append('

');

var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows( [ ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat] ] );

var chart = new google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left";

jQuery('.food_container').append('

');

var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9;

var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows( [ ['Net Carbs (' + carb_grams + 'g)', carb_grams], ['Protein (' + pro_grams + 'g)', pro_grams], ['Fat (' + fat_grams + 'g)', fat_grams] ] );

var chart2 = new google.visualization.PieChart(document.getElementById('google_graph_2')); chart2.draw(data2, { width: 350, height: 300, title: 'Daily Totals by Grams' }); document.getElementById('google_graph_2').style.cssFloat = "right"; } }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

ghost commented 5 years ago

Hi Sarah,

My first attempt to send the file failed because Google blocked it. I'll try again with a link instead.

I'm not sure what is causing the issues you are seeing.

I am using my modified version, and it works fine.

I suspect that it isn't possible to eat "negative net carbs". It would probably be more accurate to say you've consumed zero net carbs, which is what the modified script would show.

I've attached the version that I am using - it is basically the updated version (1.16) with a few changes. The way it works is to keep a running total of carbs and fibre, and it adds up the individual net carb calculations (carbs - fibre).

If you've installed the script in Chrome with Tampermonkey, and it is displaying the Net Carbs column when you view your diary, but the numbers are wrong, try using the version I have attached. I have added a .txt extension so that Gmail won't block it.

As for the goal % being wrong, I'm not sure what is happening there. I set the goals in the Android app on my phone, and the web page that shows Net Carbs just displays it (in the Net Carbs column, not the Carbs column).

[image: image.png]

When you say you've reinstalled MyFitnessPal a few times, do you mean the mobile app or the Chrome script?

Here is the link to the modified script: https://drive.google.com/file/d/1Q9XezLJHs4HxogI73ynSETR2EZYoOS-w/view?usp=sharing

Cheers,

Paul +61 431 986 756

On Thu, 7 Feb 2019 at 09:39, dsarah3 notifications@github.com wrote:

Hi Paul, Any update on the current issue with the script? I've reinstalled MyFitnessPal a few times and this is still happening (after it worked perfectly for a while and I loved it:):

  • It's stating that my net carb goal is 10% when it's actually set at 5%.

  • I actually have a negative net carb total for today so far, but it's saying I have consumed 16 net carbs (could it be that the script can't track negative carbs, maybe? I have had 17 grams of fiber and 15 grams of carbs...so, negative 2 net carbs.) Thanks in advance for your reply! Much appreciated!!! Sarah

On Thursday, January 31, 2019, 4:20:00 PM PST, pauljhobbs < notifications@github.com> wrote:

Hi Vincent,

I've tested the new version of the script (v1.16), and I think there is still an issue. It is now adding the Net Carbs correctly, but the Daily Goal is wrong, which is throwing out the Remaining value. I have taken two screenshots of the same day for me - one is with version 1.16, and the other is with my modified version. You'll notice I have also shifted the position of the Net Carbs column - I think it makes more sense to have it to the right of the Carbs and Fibre columns.

[image: image.png]

Using the script with my modifications, the same day looks like this:

[image: image.png]

I have attached the modified script for your reference (added txt extension so Google would allow it to be attached).

Cheers,

Paul +61 431 986 756

On Fri, 25 Jan 2019 at 04:34, Vincent Castellano <notifications@github.com

wrote:

@pauljhobbs https://github.com/pauljhobbs if you think you have a fix, I'd be happy to look at a PR

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-457306983 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AsnJOdguzM20_3SxXIBJPCier7lMwX7jks5vGfyYgaJpZM4aNmUF

.

// ==UserScript== // @name MyFitnessPal Percentages and Net Carbs // @version 1.16 // @namespace surye // @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs" // @downloadURL https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js // @include http://www.myfitnesspal.com/food/diary // ==/UserScript==

/*


  • Much credit to Bompus, author of the original script!
  • Thanks to kt123 and Wickity for the fixes.

  • */

function exec(fn) { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up }

function startRun() { // Load Google API for Charts var script = document.createElement("script"); script.setAttribute("src", "//www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script);

// Load jQuery script = document.createElement("script"); script.setAttribute("src", "// ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script);

// Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script); }

startRun();

function jsapiLoaded() { google.load("visualization", "1", { packages: ["corechart"], "callback": main }); }

function main() { //phobbs 2019-01-23: new variables to track running totals var carbs_running_total = 0; // Tracks the carbs running total var fibre_running_total_lt = 0; // Tracks the fiber when carbs < fibre var fibre_running_total_gte = 0; // Tracks the fiber when carbs >= fibre var net_carbs_new = 0; // Calculation: net_carbs_new = carbs_running_total

  • (fibre_running_total_gte - fibre_running_total_lt)

var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0;

var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0;

var net_carb_total = 0; var net_carb_total_goal = 0;

var header_tr_element = jQuery('.food_container tr.meal_header:first');

var elem_i = 0; header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; }

elem_i += 1; });

// Add new column for net carbs var net_carb_tr_elements = jQuery('tr'); net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(fat_i));

});

// Recalculate offsets net_carbs_i = fat_i; calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i; carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i; fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i; protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i; fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i;

// Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs var footer_tr_element = jQuery('tfoot tr'); footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0]; var alreadyCountedFiberIdx = 0; var food_tr_elements = jQuery('tr');

food_tr_elements.each(function() { if($(this).hasClass('bottom')) { alreadyCountedFiberIdx++; alreadyCountedFiber[alreadyCountedFiberIdx] = 0; }

var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text());

// Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase();

tds.eq(net_carbs_i).text(carbs - fiber); //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs >= fibre (not Totals) carbs_running_total = carbs_running_total + carbs; // phobbs fibre_running_total_gte = fibre_running_total_gte + fiber; // phobbs if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) { alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber); tds.eq(net_carbs_i).text(carbs); //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs < fibre (not Totals) fibre_running_total_lt = fibre_running_total_lt + fiber; // phobbs } } });

var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total'); var meal_idx = 0; var nctg_test = 0; // phobbs bottom_tr_elements.each(function() {

if (jQuery(this).hasClass('remaining')) { return false; / continue / }

var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { var carbs = parseFloat(tds.eq(carbs_i).text()) + alreadyCountedFiber[meal_idx]; } else { var carbs = parseFloat(tds.eq(carbs_i).text()); } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text());

var net_carbs = carbs; //var net_carbs = carbs - totalAlreadyCountedFiber; // phobbs => affects "Your Daily Goal" Total ONLY //net_carbs = carbs_running_total; //net_carbs = fibre_running_total_lt; //net_carbs = fibre_running_total_gte; //net_carbs = fiber; net_carbs_new = carbs_running_total - (fibre_running_total_gte - fibre_running_total_lt); // phobbs This is the correct formula

// HACK to show net carbs if (!jQuery(this).hasClass('alt')) { net_carbs = carbs - fiber; // phobbs *** Bottom "Totals" for Net Carbs if (!isNaN(net_carbs)) { tds.eq(net_carbs_i).text(net_carbs); //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect //nctg_test = nctg_test + net_carbs; // phobbs } } else { // record goal net_carb_total_goal = net_carbs - totalAlreadyCountedFiber ; // Bottom Total "Remaining" for Net Carbs //net_carb_total_goal = nctg_test; // phobbs => Will affect Meal Rows Totals ONLY for Net Carbs (not Meal Rows) }

/ do nothing if cannot calculate for the row / if (isNaN(cals) || isNaN(carbs) || isNaN(protein) || isNaN(fat) || isNaN(fiber) || isNaN(net_carbs) || cals === 0) { meal_idx++; return true;

}

//tds.eq(net_carbs_i).text(net_carbs);

//tds.eq(net_carbs_i).text(tds.length); // phobbs => Totals ONLY for Net Carbs (except "Remaining") if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('total alt')) { tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => Totals ONLY for Net Carbs (except "Remaining") } else { tds.eq(net_carbs_i).text(net_carbs); //tds.eq(net_carbs_i).text(999); // phobbs => Totals ONLY for Net Carbs (except "Remaining") } // if (net_carbs == 0 && // protein == 0 && // fat == 0) { // return true; // }

var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat * 9);

if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('alt') && daily_total_carbs === 0) {

daily_total_carbs = carb_cals; daily_total_protein = protein_cals; daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; // phobbs Bottom Total "Remaining" for Net Carbs

}

var real_cals = carb_cals + protein_cals + fat_cals;

var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) * 100).toFixed(2);

//alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total);

carb_pct = Math.round(carb_pct); fat_pct = Math.round(fat_pct); protein_pct = Math.round(protein_pct);

tds.each(function() { jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;"> 

'); });

tds.eq(0).find('div.myfp_us').html("");

/tds.eq(calories_i).find('div.myfp_us').html(real_cals);/

if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); }

if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); }

if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); }

meal_idx++; });

var remaining_tr_elements = jQuery('.food_container tr.total.remaining');

remaining_tr_elements.each(function() {

// Show remaining as net carbs var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; //net_carbs = 222; // phobbs affects "Remaining Total" ONLY var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs));

// Fix color tds.eq(net_carbs_i).removeClass("positive"); tds.eq(net_carbs_i).removeClass("negative");

if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); }

});

if (daily_total_carbs !== 0 || daily_total_protein !== 0 || daily_total_fat !== 0) {

jQuery('.food_container').append('

');

var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows( [ ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat] ] );

var chart = new google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left";

jQuery('.food_container').append('

');

var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9;

var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows( [ ['Net Carbs (' + carb_grams + 'g)', carb_grams], ['Protein (' + pro_grams + 'g)', pro_grams], ['Fat (' + fat_grams + 'g)', fat_grams] ] );

var chart2 = new google.visualization.PieChart(document.getElementById('google_graph_2')); chart2.draw(data2, { width: 350, height: 300, title: 'Daily Totals by Grams' }); document.getElementById('google_graph_2').style.cssFloat = "right"; } }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-461232350, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOd0lyztSTq7xRDb2DkI5QZ9OpgVDks5vK2fKgaJpZM4aNmUF .

dsarah3 commented 5 years ago

Hi Paul, Thanks so much! I installed on Chrome. Now what I'm getting is a 6% new carb goal, when I have it set to 5%! 6% is kinda weird LOL! I realize that neg net carbs may have thrown some stuff off, but the goals should be aligned with my settings in MFP, right?

Am I perhaps installing the new script incorrectly? I cut and pasted your new script into the TamperMonkey dashboard by copying your updated script and then pasting it into the settings area after I click select all and delete the script I was running. Much appreciation! Sarah [image: Screen Shot 2019-02-09 at 7.35.29 PM.png]

[image: Screen Shot 2019-02-07 at 1.04.25 AM.png]

On Sat, Feb 9, 2019 at 11:53 AM pauljhobbs notifications@github.com wrote:

Hi Sarah,

My first attempt to send the file failed because Google blocked it. I'll try again with a link instead.

I'm not sure what is causing the issues you are seeing.

I am using my modified version, and it works fine.

I suspect that it isn't possible to eat "negative net carbs". It would probably be more accurate to say you've consumed zero net carbs, which is what the modified script would show.

I've attached the version that I am using - it is basically the updated version (1.16) with a few changes. The way it works is to keep a running total of carbs and fibre, and it adds up the individual net carb calculations (carbs - fibre).

If you've installed the script in Chrome with Tampermonkey, and it is displaying the Net Carbs column when you view your diary, but the numbers are wrong, try using the version I have attached. I have added a .txt extension so that Gmail won't block it.

As for the goal % being wrong, I'm not sure what is happening there. I set the goals in the Android app on my phone, and the web page that shows Net Carbs just displays it (in the Net Carbs column, not the Carbs column).

[image: image.png]

When you say you've reinstalled MyFitnessPal a few times, do you mean the mobile app or the Chrome script?

Here is the link to the modified script:

https://drive.google.com/file/d/1Q9XezLJHs4HxogI73ynSETR2EZYoOS-w/view?usp=sharing

Cheers,

Paul +61 431 986 756

On Thu, 7 Feb 2019 at 09:39, dsarah3 notifications@github.com wrote:

Hi Paul, Any update on the current issue with the script? I've reinstalled MyFitnessPal a few times and this is still happening (after it worked perfectly for a while and I loved it:):

  • It's stating that my net carb goal is 10% when it's actually set at 5%.

  • I actually have a negative net carb total for today so far, but it's saying I have consumed 16 net carbs (could it be that the script can't track negative carbs, maybe? I have had 17 grams of fiber and 15 grams of carbs...so, negative 2 net carbs.) Thanks in advance for your reply! Much appreciated!!! Sarah

On Thursday, January 31, 2019, 4:20:00 PM PST, pauljhobbs < notifications@github.com> wrote:

Hi Vincent,

I've tested the new version of the script (v1.16), and I think there is still an issue. It is now adding the Net Carbs correctly, but the Daily Goal is wrong, which is throwing out the Remaining value. I have taken two screenshots of the same day for me - one is with version 1.16, and the other is with my modified version. You'll notice I have also shifted the position of the Net Carbs column - I think it makes more sense to have it to the right of the Carbs and Fibre columns.

[image: image.png]

Using the script with my modifications, the same day looks like this:

[image: image.png]

I have attached the modified script for your reference (added txt extension so Google would allow it to be attached).

Cheers,

Paul +61 431 986 756

On Fri, 25 Jan 2019 at 04:34, Vincent Castellano < notifications@github.com

wrote:

@pauljhobbs https://github.com/pauljhobbs if you think you have a fix, I'd be happy to look at a PR

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <

https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-457306983

, or mute the thread <

https://github.com/notifications/unsubscribe-auth/AsnJOdguzM20_3SxXIBJPCier7lMwX7jks5vGfyYgaJpZM4aNmUF

.

// ==UserScript== // @name MyFitnessPal Percentages and Net Carbs // @version 1.16 // @namespace surye // @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs" // @downloadURL https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js // @include http://www.myfitnesspal.com/food/diary // ==/UserScript==

/*


  • Much credit to Bompus, author of the original script!
  • Thanks to kt123 and Wickity for the fixes.

  • */

function exec(fn) { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up }

function startRun() { // Load Google API for Charts var script = document.createElement("script"); script.setAttribute("src", "//www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script);

// Load jQuery script = document.createElement("script"); script.setAttribute("src", "// ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script);

// Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script); }

startRun();

function jsapiLoaded() { google.load("visualization", "1", { packages: ["corechart"], "callback": main }); }

function main() { //phobbs 2019-01-23: new variables to track running totals var carbs_running_total = 0; // Tracks the carbs running total var fibre_running_total_lt = 0; // Tracks the fiber when carbs < fibre var fibre_running_total_gte = 0; // Tracks the fiber when carbs >= fibre var net_carbs_new = 0; // Calculation: net_carbs_new = carbs_running_total

  • (fibre_running_total_gte - fibre_running_total_lt)

var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0;

var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0;

var net_carb_total = 0; var net_carb_total_goal = 0;

var header_tr_element = jQuery('.food_container tr.meal_header:first');

var elem_i = 0; header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; }

elem_i += 1; });

// Add new column for net carbs var net_carb_tr_elements = jQuery('tr'); net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(fat_i));

});

// Recalculate offsets net_carbs_i = fat_i; calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i; carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i; fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i; protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i; fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i;

// Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs var footer_tr_element = jQuery('tfoot tr'); footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0]; var alreadyCountedFiberIdx = 0; var food_tr_elements = jQuery('tr');

food_tr_elements.each(function() { if($(this).hasClass('bottom')) { alreadyCountedFiberIdx++; alreadyCountedFiber[alreadyCountedFiberIdx] = 0; }

var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text());

// Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase();

tds.eq(net_carbs_i).text(carbs - fiber); //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs >= fibre (not Totals) carbs_running_total = carbs_running_total + carbs; // phobbs fibre_running_total_gte = fibre_running_total_gte + fiber; // phobbs if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) { alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber); tds.eq(net_carbs_i).text(carbs); //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs < fibre (not Totals) fibre_running_total_lt = fibre_running_total_lt + fiber; // phobbs } } });

var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total'); var meal_idx = 0; var nctg_test = 0; // phobbs bottom_tr_elements.each(function() {

if (jQuery(this).hasClass('remaining')) { return false; / continue / }

var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { var carbs = parseFloat(tds.eq(carbs_i).text()) + alreadyCountedFiber[meal_idx]; } else { var carbs = parseFloat(tds.eq(carbs_i).text()); } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text());

var net_carbs = carbs; //var net_carbs = carbs - totalAlreadyCountedFiber; // phobbs => affects "Your Daily Goal" Total ONLY //net_carbs = carbs_running_total; //net_carbs = fibre_running_total_lt; //net_carbs = fibre_running_total_gte; //net_carbs = fiber; net_carbs_new = carbs_running_total - (fibre_running_total_gte - fibre_running_total_lt); // phobbs This is the correct formula

// HACK to show net carbs if (!jQuery(this).hasClass('alt')) { net_carbs = carbs - fiber; // phobbs *** Bottom "Totals" for Net Carbs if (!isNaN(net_carbs)) { tds.eq(net_carbs_i).text(net_carbs); //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect //nctg_test = nctg_test + net_carbs; // phobbs } } else { // record goal net_carb_total_goal = net_carbs - totalAlreadyCountedFiber ; // Bottom Total "Remaining" for Net Carbs //net_carb_total_goal = nctg_test; // phobbs => Will affect Meal Rows Totals ONLY for Net Carbs (not Meal Rows) }

/ do nothing if cannot calculate for the row / if (isNaN(cals) || isNaN(carbs) || isNaN(protein) || isNaN(fat) || isNaN(fiber) || isNaN(net_carbs) || cals === 0) { meal_idx++; return true;

}

//tds.eq(net_carbs_i).text(net_carbs);

//tds.eq(net_carbs_i).text(tds.length); // phobbs => Totals ONLY for Net Carbs (except "Remaining") if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('total alt')) { tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => Totals ONLY for Net Carbs (except "Remaining") } else { tds.eq(net_carbs_i).text(net_carbs); //tds.eq(net_carbs_i).text(999); // phobbs => Totals ONLY for Net Carbs (except "Remaining") } // if (net_carbs == 0 && // protein == 0 && // fat == 0) { // return true; // }

var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat * 9);

if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('alt') && daily_total_carbs === 0) {

daily_total_carbs = carb_cals; daily_total_protein = protein_cals; daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; // phobbs Bottom Total "Remaining" for Net Carbs

}

var real_cals = carb_cals + protein_cals + fat_cals;

var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) * 100).toFixed(2);

//alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total);

carb_pct = Math.round(carb_pct); fat_pct = Math.round(fat_pct); protein_pct = Math.round(protein_pct);

tds.each(function() { jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;"> 

'); });

tds.eq(0).find('div.myfp_us').html("");

/tds.eq(calories_i).find('div.myfp_us').html(real_cals);/

if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); }

if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); }

if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); }

meal_idx++; });

var remaining_tr_elements = jQuery('.food_container tr.total.remaining');

remaining_tr_elements.each(function() {

// Show remaining as net carbs var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; //net_carbs = 222; // phobbs affects "Remaining Total" ONLY var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs));

// Fix color tds.eq(net_carbs_i).removeClass("positive"); tds.eq(net_carbs_i).removeClass("negative");

if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); }

});

if (daily_total_carbs !== 0 || daily_total_protein !== 0 || daily_total_fat !== 0) {

jQuery('.food_container').append('

');

var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows( [ ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat] ] );

var chart = new google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left";

jQuery('.food_container').append('

');

var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9;

var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows( [ ['Net Carbs (' + carb_grams + 'g)', carb_grams], ['Protein (' + pro_grams + 'g)', pro_grams], ['Fat (' + fat_grams + 'g)', fat_grams] ] );

var chart2 = new google.visualization.PieChart(document.getElementById('google_graph_2')); chart2.draw(data2, { width: 350, height: 300, title: 'Daily Totals by Grams' }); document.getElementById('google_graph_2').style.cssFloat = "right"; } }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-461232350 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AsnJOd0lyztSTq7xRDb2DkI5QZ9OpgVDks5vK2fKgaJpZM4aNmUF

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-461377432, or mute the thread https://github.com/notifications/unsubscribe-auth/AsxHKo0Y5KI4hk4OOQGlA6VlzmJo8DNtks5vLAlmgaJpZM4aNmUF .

ghost commented 5 years ago

Hi Sarah,

It is weird that the web page is showing a goal of 6% when you set it to 5%. I can't really explain that. But I suspect it has nothing to do with the script in Tampermonkey. I suggest logging a support ticket with MFP Support. They will be able to look at your account and might be able to figure out what is going on.

Now that you've installed my modified script, is it calculating the Net Carbs correctly?

Cheers,

Paul +61 431 986 756

On Sun, 10 Feb 2019 at 13:40, dsarah3 notifications@github.com wrote:

Hi Paul, Thanks so much! I installed on Chrome. Now what I'm getting is a 6% new carb goal, when I have it set to 5%! 6% is kinda weird LOL! I realize that neg net carbs may have thrown some stuff off, but the goals should be aligned with my settings in MFP, right?

Am I perhaps installing the new script incorrectly? I cut and pasted your new script into the TamperMonkey dashboard by copying your updated script and then pasting it into the settings area after I click select all and delete the script I was running. Much appreciation! Sarah [image: Screen Shot 2019-02-09 at 7.35.29 PM.png]

[image: Screen Shot 2019-02-07 at 1.04.25 AM.png]

On Sat, Feb 9, 2019 at 11:53 AM pauljhobbs notifications@github.com wrote:

Hi Sarah,

My first attempt to send the file failed because Google blocked it. I'll try again with a link instead.

I'm not sure what is causing the issues you are seeing.

I am using my modified version, and it works fine.

I suspect that it isn't possible to eat "negative net carbs". It would probably be more accurate to say you've consumed zero net carbs, which is what the modified script would show.

I've attached the version that I am using - it is basically the updated version (1.16) with a few changes. The way it works is to keep a running total of carbs and fibre, and it adds up the individual net carb calculations (carbs - fibre).

If you've installed the script in Chrome with Tampermonkey, and it is displaying the Net Carbs column when you view your diary, but the numbers are wrong, try using the version I have attached. I have added a .txt extension so that Gmail won't block it.

As for the goal % being wrong, I'm not sure what is happening there. I set the goals in the Android app on my phone, and the web page that shows Net Carbs just displays it (in the Net Carbs column, not the Carbs column).

[image: image.png]

When you say you've reinstalled MyFitnessPal a few times, do you mean the mobile app or the Chrome script?

Here is the link to the modified script:

https://drive.google.com/file/d/1Q9XezLJHs4HxogI73ynSETR2EZYoOS-w/view?usp=sharing

Cheers,

Paul +61 431 986 756

On Thu, 7 Feb 2019 at 09:39, dsarah3 notifications@github.com wrote:

Hi Paul, Any update on the current issue with the script? I've reinstalled MyFitnessPal a few times and this is still happening (after it worked perfectly for a while and I loved it:):

  • It's stating that my net carb goal is 10% when it's actually set at 5%.

  • I actually have a negative net carb total for today so far, but it's saying I have consumed 16 net carbs (could it be that the script can't track negative carbs, maybe? I have had 17 grams of fiber and 15 grams of carbs...so, negative 2 net carbs.) Thanks in advance for your reply! Much appreciated!!! Sarah

On Thursday, January 31, 2019, 4:20:00 PM PST, pauljhobbs < notifications@github.com> wrote:

Hi Vincent,

I've tested the new version of the script (v1.16), and I think there is still an issue. It is now adding the Net Carbs correctly, but the Daily Goal is wrong, which is throwing out the Remaining value. I have taken two screenshots of the same day for me - one is with version 1.16, and the other is with my modified version. You'll notice I have also shifted the position of the Net Carbs column - I think it makes more sense to have it to the right of the Carbs and Fibre columns.

[image: image.png]

Using the script with my modifications, the same day looks like this:

[image: image.png]

I have attached the modified script for your reference (added txt extension so Google would allow it to be attached).

Cheers,

Paul +61 431 986 756

On Fri, 25 Jan 2019 at 04:34, Vincent Castellano < notifications@github.com

wrote:

@pauljhobbs https://github.com/pauljhobbs if you think you have a fix, I'd be happy to look at a PR

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <

https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-457306983

, or mute the thread <

https://github.com/notifications/unsubscribe-auth/AsnJOdguzM20_3SxXIBJPCier7lMwX7jks5vGfyYgaJpZM4aNmUF

.

// ==UserScript== // @name MyFitnessPal Percentages and Net Carbs // @version 1.16 // @namespace surye // @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs" // @downloadURL

https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js // @include http://www.myfitnesspal.com/food/diary // ==/UserScript==

/*


  • Much credit to Bompus, author of the original script!
  • Thanks to kt123 and Wickity for the fixes.

  • */

function exec(fn) { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up }

function startRun() { // Load Google API for Charts var script = document.createElement("script"); script.setAttribute("src", "//www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script);

// Load jQuery script = document.createElement("script"); script.setAttribute("src", "// ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script);

// Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script); }

startRun();

function jsapiLoaded() { google.load("visualization", "1", { packages: ["corechart"], "callback": main }); }

function main() { //phobbs 2019-01-23: new variables to track running totals var carbs_running_total = 0; // Tracks the carbs running total var fibre_running_total_lt = 0; // Tracks the fiber when carbs < fibre var fibre_running_total_gte = 0; // Tracks the fiber when carbs >= fibre var net_carbs_new = 0; // Calculation: net_carbs_new = carbs_running_total

  • (fibre_running_total_gte - fibre_running_total_lt)

var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0;

var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0;

var net_carb_total = 0; var net_carb_total_goal = 0;

var header_tr_element = jQuery('.food_container tr.meal_header:first');

var elem_i = 0; header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; }

elem_i += 1; });

// Add new column for net carbs var net_carb_tr_elements = jQuery('tr'); net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(fat_i));

});

// Recalculate offsets net_carbs_i = fat_i; calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i; carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i; fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i; protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i; fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i;

// Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt");

header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs var footer_tr_element = jQuery('tfoot tr'); footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt");

header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0]; var alreadyCountedFiberIdx = 0; var food_tr_elements = jQuery('tr');

food_tr_elements.each(function() { if($(this).hasClass('bottom')) { alreadyCountedFiberIdx++; alreadyCountedFiber[alreadyCountedFiberIdx] = 0; }

var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text());

// Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase();

tds.eq(net_carbs_i).text(carbs - fiber); //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs >= fibre (not Totals) carbs_running_total = carbs_running_total + carbs; // phobbs fibre_running_total_gte = fibre_running_total_gte + fiber; // phobbs if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) { alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber); tds.eq(net_carbs_i).text(carbs); //tds.eq(net_carbs_i).text(888); // phobbs => goes to Meal Rows ONLY where carbs < fibre (not Totals) fibre_running_total_lt = fibre_running_total_lt + fiber; // phobbs } } });

var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total'); var meal_idx = 0; var nctg_test = 0; // phobbs bottom_tr_elements.each(function() {

if (jQuery(this).hasClass('remaining')) { return false; / continue / }

var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { var carbs = parseFloat(tds.eq(carbs_i).text()) + alreadyCountedFiber[meal_idx]; } else { var carbs = parseFloat(tds.eq(carbs_i).text()); } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text());

var net_carbs = carbs; //var net_carbs = carbs - totalAlreadyCountedFiber; // phobbs => affects "Your Daily Goal" Total ONLY //net_carbs = carbs_running_total; //net_carbs = fibre_running_total_lt; //net_carbs = fibre_running_total_gte; //net_carbs = fiber; net_carbs_new = carbs_running_total - (fibre_running_total_gte - fibre_running_total_lt); // phobbs This is the correct formula

// HACK to show net carbs if (!jQuery(this).hasClass('alt')) { net_carbs = carbs - fiber; // phobbs *** Bottom "Totals" for Net Carbs if (!isNaN(net_carbs)) { tds.eq(net_carbs_i).text(net_carbs); //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); //tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => No Effect //nctg_test = nctg_test + net_carbs; // phobbs } } else { // record goal net_carb_total_goal = net_carbs - totalAlreadyCountedFiber ; // Bottom Total "Remaining" for Net Carbs //net_carb_total_goal = nctg_test; // phobbs => Will affect Meal Rows Totals ONLY for Net Carbs (not Meal Rows) }

/ do nothing if cannot calculate for the row / if (isNaN(cals) || isNaN(carbs) || isNaN(protein) || isNaN(fat) || isNaN(fiber) || isNaN(net_carbs) || cals === 0) { meal_idx++; return true;

}

//tds.eq(net_carbs_i).text(net_carbs);

//tds.eq(net_carbs_i).text(tds.length); // phobbs => Totals ONLY for Net Carbs (except "Remaining") if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('total alt')) { tds.eq(net_carbs_i).text(net_carbs_new); // phobbs => Totals ONLY for Net Carbs (except "Remaining") } else { tds.eq(net_carbs_i).text(net_carbs); //tds.eq(net_carbs_i).text(999); // phobbs => Totals ONLY for Net Carbs (except "Remaining") } // if (net_carbs == 0 && // protein == 0 && // fat == 0) { // return true; // }

var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat * 9);

if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('alt') && daily_total_carbs === 0) {

daily_total_carbs = carb_cals; daily_total_protein = protein_cals; daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; // phobbs Bottom Total "Remaining" for Net Carbs

}

var real_cals = carb_cals + protein_cals + fat_cals;

var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) * 100).toFixed(2);

//alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total);

carb_pct = Math.round(carb_pct); fat_pct = Math.round(fat_pct); protein_pct = Math.round(protein_pct);

tds.each(function() { jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;"> 

'); });

tds.eq(0).find('div.myfp_us').html("");

/tds.eq(calories_i).find('div.myfp_us').html(real_cals);/

if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); }

if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); }

if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); }

meal_idx++; });

var remaining_tr_elements = jQuery('.food_container tr.total.remaining');

remaining_tr_elements.each(function() {

// Show remaining as net carbs var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; //net_carbs = 222; // phobbs affects "Remaining Total" ONLY var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs));

// Fix color tds.eq(net_carbs_i).removeClass("positive"); tds.eq(net_carbs_i).removeClass("negative");

if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); }

});

if (daily_total_carbs !== 0 || daily_total_protein !== 0 || daily_total_fat !== 0) {

jQuery('.food_container').append('

');

var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows( [ ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat] ] );

var chart = new

google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left";

jQuery('.food_container').append('

');

var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9;

var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows( [ ['Net Carbs (' + carb_grams + 'g)', carb_grams], ['Protein (' + pro_grams + 'g)', pro_grams], ['Fat (' + fat_grams + 'g)', fat_grams] ] );

var chart2 = new

google.visualization.PieChart(document.getElementById('google_graph_2')); chart2.draw(data2, { width: 350, height: 300, title: 'Daily Totals by Grams' }); document.getElementById('google_graph_2').style.cssFloat = "right"; } }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <

https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-461232350

, or mute the thread <

https://github.com/notifications/unsubscribe-auth/AsnJOd0lyztSTq7xRDb2DkI5QZ9OpgVDks5vK2fKgaJpZM4aNmUF

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub < https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-461377432 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AsxHKo0Y5KI4hk4OOQGlA6VlzmJo8DNtks5vLAlmgaJpZM4aNmUF

.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-462100781, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOUVyiWDDJqmJsETByM0afL4-7bUxks5vL5SPgaJpZM4aNmUF .

dsarah3 commented 5 years ago

Hi Paul,Thanks for the message. No, actually the net carb count is totally off! It says I had 0 net carbs when I had 14. I'm wondering if I installed the new script properly? I opened the script in Settings, selected all, deleted the old one and cut and pasted your new one in. Perhaps that doesn't save the new script? Newbie here LOL!  This is in there now: // ==UserScript==// @name            MyFitnessPal Percentages and Net Carbs// @version         1.16// @namespace       surye// @description     Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs"// @downloadURL     https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js// @include         http://www.myfitnesspal.com/food/diary// ==/UserScript== /   ------------------------------------------------------------   Much credit to Bompus, author of the original script!   Thanks to kt123 and Wickity for the fixes.   ------------------------------------------------------------ /

function exec(fn) {    var script = document.createElement('script');    script.setAttribute("type", "application/javascript");    script.textContent = '(' + fn + ')();';    document.body.appendChild(script); // run the script    document.body.removeChild(script); // clean up} function startRun() {    // Load Google API for Charts    var script = document.createElement("script");    script.setAttribute("src", "//www.google.com/jsapi");    script.addEventListener('load', function() {        exec(jsapiLoaded);    }, false);    document.body.appendChild(script);     // Load jQuery    script = document.createElement("script");    script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js");    script.addEventListener('load', function() {        exec("jQuery.noConflict()");    }, false);    document.body.appendChild(script);     // Inject this script into page.    script = document.createElement('script');    script.setAttribute("type", "application/javascript");    script.textContent = main;    document.body.appendChild(script);} startRun(); function jsapiLoaded() {    google.load("visualization", "1", { packages: ["corechart"], "callback": main });} function main() {    var calories_i = 0;    var net_carbs_i = 0;    var carbs_i = 0;    var fiber_i = 0;    var protein_i = 0;    var fat_i = 0;     var daily_total_carbs = 0;    var daily_total_protein = 0;    var daily_total_fat = 0;     var net_carb_total = 0;    var net_carb_total_goal = 0;     var header_tr_element = jQuery('.food_container tr.meal_header:first');     var elem_i = 0;    header_tr_element.find('td').each(function() {        var myval = jQuery(this).text().toLowerCase().trim();        if (myval.indexOf('calories') !== -1) { calories_i = elem_i; }        if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; }        if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; }        if (myval.indexOf('fat') !== -1) { fat_i = elem_i; }        if (myval.indexOf('protein') !== -1) { protein_i = elem_i; }         elem_i += 1;    });

    // Add new column for net carbs    var net_carb_tr_elements = jQuery('tr');    net_carb_tr_elements.each(function() {        var tds = jQuery(this).find('td');        jQuery('').insertBefore(tds.eq(carbs_i));     });     // Recalculate offsets    net_carbs_i = carbs_i;    calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i;    carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i;    fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i;    protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i;    fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i;     // Set header    header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs");    header_tr_element.find('td').eq(net_carbs_i).addClass("alt");    header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

    // Change to say net carbs    var footer_tr_element = jQuery('tfoot tr');    footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs");    footer_tr_element.find('td').eq(net_carbs_i).addClass("alt");    header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

    var alreadyCountedFiber = [0];    var alreadyCountedFiberIdx = 0;    var food_tr_elements = jQuery('tr');     food_tr_elements.each(function() {        if($(this).hasClass('bottom')) {            alreadyCountedFiberIdx++;            alreadyCountedFiber[alreadyCountedFiberIdx] = 0;        }         var tds = jQuery(this).find('td');        var carbs = parseFloat(tds.eq(carbs_i).text());        var fiber = parseFloat(tds.eq(fiber_i).text());         // Find only food rows!        var delete_td = tds.eq(tds.length - 1);        if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase(); tds.eq(net_carbs_i).text(carbs - fiber);             if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) {                alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber);                tds.eq(net_carbs_i).text(carbs);            }        }    });     var totalAlreadyCountedFiber = 0;    for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

    var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total');    var meal_idx = 0;    bottom_tr_elements.each(function() {         if (jQuery(this).hasClass('remaining')) {            return false; / continue /        }         var tds = jQuery(this).find('td');        var cals = parseFloat(tds.eq(calories_i).text());        var carbs = 0;        if($(this).hasClass('bottom')) {            carbs = parseFloat(tds.eq(carbs_i).text());        } else {            carbs = parseFloat(tds.eq(carbs_i).text()) + totalAlreadyCountedFiber;        }        var fiber = parseFloat(tds.eq(fiber_i).text());        var protein = parseFloat(tds.eq(protein_i).text());        var fat = parseFloat(tds.eq(fat_i).text());         var net_carbs = carbs;         // HACK to show net carbs        if (!jQuery(this).hasClass('alt')) {            net_carbs = carbs - fiber + alreadyCountedFiber[meal_idx];            if (!isNaN(net_carbs)) {                tds.eq(net_carbs_i).text(net_carbs);            } else if (jQuery(this).hasClass("total")) {                tds.eq(net_carbs_i).text("0");            }        } else {            // record goal            net_carb_total_goal = net_carbs;        }

        / do nothing if cannot calculate for the row /        if (isNaN(cals) ||            isNaN(carbs) ||            isNaN(protein) ||            isNaN(fat) ||            isNaN(fiber) ||            isNaN(net_carbs) ||            cals === 0) {            meal_idx++;            return true;         }         tds.eq(net_carbs_i).text(net_carbs);

        // if (net_carbs == 0 &&        //     protein == 0 &&        //     fat == 0) {        //     return true;        // }         var carb_cals = (net_carbs 4);        var protein_cals = (protein 4);        var fat_cals = (fat 9);         if (jQuery(this).hasClass('total') &&            !jQuery(this).hasClass('alt') &&            daily_total_carbs === 0) {             daily_total_carbs = carb_cals;            daily_total_protein = protein_cals;            daily_total_fat = fat_cals;            net_carb_total = net_carbs - totalAlreadyCountedFiber;         }         var real_cals = carb_cals + protein_cals + fat_cals;         var carb_pct = ((carb_cals / real_cals) 100).toFixed(2);        var fat_pct = ((fat_cals / real_cals) 100).toFixed(2);        var protein_pct = ((protein_cals / real_cals) 100).toFixed(2);         //alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total);         carb_pct = Math.round(carb_pct);        fat_pct = Math.round(fat_pct);        protein_pct = Math.round(protein_pct);         tds.each(function() {            jQuery(this).append('

 
');        });         tds.eq(0).find('div.myfp_us').html("");         /tds.eq(calories_i).find('div.myfp_us').html(real_cals);/         if (!isNaN(carb_pct)) {            tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%");        }         if (!isNaN(fat_pct)) {            tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%");        }         if (!isNaN(protein_pct)) {            tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%");        }         meal_idx++;    });     var remaining_tr_elements = jQuery('.food_container tr.total.remaining');     remaining_tr_elements.each(function() {         // Show remaining as net carbs        var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber;        var tds = jQuery(this).find('td');        tds.eq(net_carbs_i).text(parseInt(net_carbs));         // Fix color        tds.eq(net_carbs_i).removeClass("positive");        tds.eq(net_carbs_i).removeClass("negative");         if (net_carbs < 0) {            tds.eq(net_carbs_i).addClass("negative");        } else {            tds.eq(net_carbs_i).addClass("positive");        }     });

    if (daily_total_carbs !== 0 ||        daily_total_protein !== 0 ||        daily_total_fat !== 0) {         jQuery('.food_container').append('

');         var data1 = new google.visualization.DataTable();        data1.addColumn('string', 'Type');        data1.addColumn('number', 'Cals');        data1.addRows(            [                ['Net Carbs', daily_total_carbs],                ['Protein', daily_total_protein],                ['Fat', daily_total_fat]            ]        );         var chart = new google.visualization.PieChart(document.getElementById('google_graph_1'));        chart.draw(data1, {            width: 350,            height: 300,            title: 'Daily Totals by Calories (This is what you use for your macro ratios)'        });        document.getElementById('google_graph_1').style.cssFloat = "left";         jQuery('.food_container').append('
');         var carb_grams = daily_total_carbs / 4;        var pro_grams = daily_total_protein / 4;        var fat_grams = daily_total_fat / 9;         var data2 = new google.visualization.DataTable();        data2.addColumn('string', 'Type');        data2.addColumn('number', 'Grams');        data2.addRows(            [                ['Net Carbs (' + carb_grams + 'g)', carb_grams],                ['Protein (' + pro_grams + 'g)', pro_grams],                ['Fat (' + fat_grams + 'g)', fat_grams]            ]        );         var chart2 = new google.visualization.PieChart(document.getElementById('google_graph_2'));        chart2.draw(data2, {            width: 350,            height: 300,            title: 'Daily Totals by Grams'        });        document.getElementById('google_graph_2').style.cssFloat = "right";    }}

ghost commented 5 years ago

Hi Sarah,

The script you pasted is not the modified one I sent you. When you paste the new script into Tampermonkey, you need to save it. You can click File > Save:

[image: image.png]

Or just press Ctrl-S on your keyboard.

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:06, dsarah3 notifications@github.com wrote:

Hi Paul,Thanks for the message. No, actually the net carb count is totally off! It says I had 0 net carbs when I had 14. I'm wondering if I installed the new script properly? I opened the script in Settings, selected all, deleted the old one and cut and pasted your new one in. Perhaps that doesn't save the new script? Newbie here LOL! This is in there now: // ==UserScript==// @name MyFitnessPal Percentages and Net Carbs// @version 1.16// @namespace surye// @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs"// @downloadURL https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js// @include http://www.myfitnesspal.com/food/diary// ==/UserScript== / ------------------------------------------------------------ Much credit to Bompus, author of the original script! Thanks to kt123 and Wickity for the fixes. ------------------------------------------------------------ /

function exec(fn) { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up} function startRun() { // Load Google API for Charts var script = document.createElement("script"); script.setAttribute("src", "// www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script); // Load jQuery script = document.createElement("script"); script.setAttribute("src", "// ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script); // Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script);} startRun(); function jsapiLoaded() { google.load("visualization", "1", { packages: ["corechart"], "callback": main });} function main() { var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0; var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0; var net_carb_total = 0; var net_carb_total_goal = 0; var header_tr_element = jQuery('.food_container tr.meal_header:first'); var elem_i = 0; header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; } elem_i += 1; });

// Add new column for net carbs    var net_carb_tr_elements =

jQuery('tr'); net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(carbs_i)); }); // Recalculate offsets net_carbs_i = carbs_i; calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i; carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i; fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i; protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i; fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i; // Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs    var footer_tr_element = jQuery('tfoot

tr'); footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0];    var alreadyCountedFiberIdx = 0;

var food_tr_elements = jQuery('tr'); food_tr_elements.each(function() { if($(this).hasClass('bottom')) { alreadyCountedFiberIdx++; alreadyCountedFiber[alreadyCountedFiberIdx] = 0; } var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text()); // Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase(); tds.eq(net_carbs_i).text(carbs - fiber); if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) { alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber); tds.eq(net_carbs_i).text(carbs); } } }); var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container

tr.bottom, .food_container tr.total'); var meal_idx = 0; bottom_tr_elements.each(function() { if (jQuery(this).hasClass('remaining')) { return false; / continue / } var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { carbs = parseFloat(tds.eq(carbs_i).text()); } else { carbs = parseFloat(tds.eq(carbs_i).text()) + totalAlreadyCountedFiber; } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text()); var net_carbs = carbs; // HACK to show net carbs if (!jQuery(this).hasClass('alt')) { net_carbs = carbs - fiber + alreadyCountedFiber[meal_idx]; if (!isNaN(net_carbs)) { tds.eq(net_carbs_i).text(net_carbs); } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); } } else { // record goal net_carb_total_goal = net_carbs; }

    /* do nothing if cannot calculate for the row */        if

(isNaN(cals) || isNaN(carbs) || isNaN(protein) || isNaN(fat) || isNaN(fiber) || isNaN(net_carbs) || cals === 0) { meal_idx++; return true; } tds.eq(net_carbs_i).text(net_carbs);

    // if (net_carbs == 0 &&        //     protein == 0 &&        //

fat == 0) { // return true; // } var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat 9); if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('alt') && daily_total_carbs === 0) { daily_total_carbs = carb_cals; daily_total_protein = protein_cals; daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; } var real_cals = carb_cals + protein_cals + fat_cals; var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) 100).toFixed(2); //alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total); carb_pct = Math.round(carb_pct); fat_pct = Math.round(fat_pct); protein_pct = Math.round(protein_pct); tds.each(function() { jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;"> 

'); }); tds.eq(0).find('div.myfp_us').html(""); /tds.eq(calories_i).find('div.myfp_us').html(real_cals);/ if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); } if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); } if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); } meal_idx++; }); var remaining_tr_elements = jQuery('.food_container tr.total.remaining'); remaining_tr_elements.each(function() { // Show remaining as net carbs var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs)); // Fix color tds.eq(net_carbs_i).removeClass("positive"); tds.eq(net_carbs_i).removeClass("negative"); if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); } });

if (daily_total_carbs !== 0 ||        daily_total_protein !== 0 ||
daily_total_fat !== 0) {
    jQuery('.food_container').append('<div

id="google_graph_1">

'); var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows( [ ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat] ] ); var chart = new google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left"; jQuery('.food_container').append('<div id="google_graph_2">'); var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9; var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows( [ ['Net Carbs ('

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463394622, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOfY61Z9-ARhLvlEdvuNKh3RGv0BKks5vNIxSgaJpZM4aNmUF .

dsarah3 commented 5 years ago

Hi Paul,I did that and the net carbs column disappeared! I copied the entire script you sent, are there perhaps extra characters at the top or bottom of it that I need to delete? Thanks again for your patience, kindness and help with this, much appreciated!Sarah On Wednesday, February 13, 2019, 2:23:00 PM PST, pauljhobbs notifications@github.com wrote:

Hi Sarah,

The script you pasted is not the modified one I sent you. When you paste the new script into Tampermonkey, you need to save it. You can click File > Save:

[image: image.png]

Or just press Ctrl-S on your keyboard.

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:06, dsarah3 notifications@github.com wrote:

Hi Paul,Thanks for the message. No, actually the net carb count is totally off! It says I had 0 net carbs when I had 14. I'm wondering if I installed the new script properly? I opened the script in Settings, selected all, deleted the old one and cut and pasted your new one in. Perhaps that doesn't save the new script? Newbie here LOL! This is in there now: // ==UserScript==// @name MyFitnessPal Percentages and Net Carbs// @version 1.16// @namespace   surye// @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs"// @downloadURL https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js// @include http://www.myfitnesspal.com/food/diary// ==/UserScript== / ------------------------------------------------------------ Much credit to Bompus, author of the original script! Thanks to kt123 and Wickity for the fixes. ------------------------------------------------------------ /

function exec(fn) {   var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up} function startRun() { // Load Google API for Charts   var script = document.createElement("script"); script.setAttribute("src", "// www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script); // Load jQuery script = document.createElement("script"); script.setAttribute("src", "// ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script); // Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script);} startRun(); function jsapiLoaded() {   google.load("visualization", "1", { packages: ["corechart"], "callback": main });} function main() { var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0; var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0; var net_carb_total = 0; var net_carb_total_goal = 0; var header_tr_element = jQuery('.food_container tr.meal_header:first'); var elem_i = 0;   header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; }   elem_i += 1; });

// Add new column for net carbs   var net_carb_tr_elements = jQuery('tr');   net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(carbs_i)); }); // Recalculate offsets   net_carbs_i = carbs_i;   calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i;   carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i;   fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i;   protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i;   fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i; // Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs   var footer_tr_element = jQuery('tfoot tr');   footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0]; var alreadyCountedFiberIdx = 0; var food_tr_elements = jQuery('tr');   food_tr_elements.each(function() { if($(this).hasClass('bottom')) {   alreadyCountedFiberIdx++;   alreadyCountedFiber[alreadyCountedFiberIdx] = 0; } var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text()); // Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase(); tds.eq(net_carbs_i).text(carbs - fiber); if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) {   alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber);   tds.eq(net_carbs_i).text(carbs); } } }); var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total'); var meal_idx = 0; bottom_tr_elements.each(function() { if (jQuery(this).hasClass('remaining')) { return false; / continue / } var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { carbs = parseFloat(tds.eq(carbs_i).text()); } else { carbs = parseFloat(tds.eq(carbs_i).text()) + totalAlreadyCountedFiber; } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text()); var net_carbs = carbs; // HACK to show net carbs if (!jQuery(this).hasClass('alt')) {   net_carbs = carbs - fiber + alreadyCountedFiber[meal_idx]; if (!isNaN(net_carbs)) {   tds.eq(net_carbs_i).text(net_carbs); } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); } } else { // record goal   net_carb_total_goal = net_carbs; }

/ do nothing if cannot calculate for the row / if (isNaN(cals) ||   isNaN(carbs) ||   isNaN(protein) ||   isNaN(fat) ||   isNaN(fiber) || isNaN(net_carbs) ||   cals === 0) {   meal_idx++; return true; }   tds.eq(net_carbs_i).text(net_carbs);

// if (net_carbs == 0 && // protein == 0 && // fat == 0) { // return true; // } var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat 9); if (jQuery(this).hasClass('total') &&    !jQuery(this).hasClass('alt') &&   daily_total_carbs === 0) {   daily_total_carbs = carb_cals;   daily_total_protein = protein_cals;   daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; } var real_cals = carb_cals + protein_cals + fat_cals; var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) 100).toFixed(2); //alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total);   carb_pct = Math.round(carb_pct);   fat_pct = Math.round(fat_pct);   protein_pct = Math.round(protein_pct);   tds.each(function() {   jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;"> 

'); });   tds.eq(0).find('div.myfp_us').html(""); /tds.eq(calories_i).find('div.myfp_us').html(real_cals);/ if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); } if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); } if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); }   meal_idx++; }); var remaining_tr_elements = jQuery('.food_container tr.total.remaining');   remaining_tr_elements.each(function() { // Show remaining as net carbs   var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs)); // Fix color   tds.eq(net_carbs_i).removeClass("positive");   tds.eq(net_carbs_i).removeClass("negative"); if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); } });

if (daily_total_carbs !== 0 ||   daily_total_protein !== 0 ||   daily_total_fat !== 0) {   jQuery('.food_container').append('<div id="google_graph_1">

'); var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows(   [  ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat]   ]   );   var chart = new google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left";   jQuery('.food_container').append('<div id="google_graph_2">'); var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9; var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows(   [  ['Net Carbs ('

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463394622, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOfY61Z9-ARhLvlEdvuNKh3RGv0BKks5vNIxSgaJpZM4aNmUF  .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

ghost commented 5 years ago

Hi Sarah,

I have posted a reply to the issue you logged at GitHub (issue 56).

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:34, dsarah3 notifications@github.com wrote:

Hi Paul,I did that and the net carbs column disappeared! I copied the entire script you sent, are there perhaps extra characters at the top or bottom of it that I need to delete? Thanks again for your patience, kindness and help with this, much appreciated!Sarah On Wednesday, February 13, 2019, 2:23:00 PM PST, pauljhobbs < notifications@github.com> wrote:

Hi Sarah,

The script you pasted is not the modified one I sent you. When you paste the new script into Tampermonkey, you need to save it. You can click File > Save:

[image: image.png]

Or just press Ctrl-S on your keyboard.

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:06, dsarah3 notifications@github.com wrote:

Hi Paul,Thanks for the message. No, actually the net carb count is totally off! It says I had 0 net carbs when I had 14. I'm wondering if I installed the new script properly? I opened the script in Settings, selected all, deleted the old one and cut and pasted your new one in. Perhaps that doesn't save the new script? Newbie here LOL! This is in there now: // ==UserScript==// @name MyFitnessPal Percentages and Net Carbs// @version 1.16// @namespace surye// @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs"// @downloadURL

https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js// @include http://www.myfitnesspal.com/food/diary// ==/UserScript== / ------------------------------------------------------------ Much credit to Bompus, author of the original script! Thanks to kt123 and Wickity for the fixes. ------------------------------------------------------------ /

function exec(fn) { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up} function startRun() { // Load Google API for Charts var script = document.createElement("script"); script.setAttribute("src", "// www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script); // Load jQuery script = document.createElement("script"); script.setAttribute("src", "// ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script); // Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script);} startRun(); function jsapiLoaded() { google.load("visualization", "1", { packages: ["corechart"], "callback": main });} function main() { var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0; var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0; var net_carb_total = 0; var net_carb_total_goal = 0; var header_tr_element = jQuery('.food_container tr.meal_header:first'); var elem_i = 0; header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; } elem_i += 1; });

// Add new column for net carbs var net_carb_tr_elements = jQuery('tr'); net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(carbs_i)); }); // Recalculate offsets net_carbs_i = carbs_i; calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i; carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i; fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i; protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i; fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i; // Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs var footer_tr_element = jQuery('tfoot tr'); footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0]; var alreadyCountedFiberIdx = 0; var food_tr_elements = jQuery('tr'); food_tr_elements.each(function() { if($(this).hasClass('bottom')) { alreadyCountedFiberIdx++; alreadyCountedFiber[alreadyCountedFiberIdx] = 0; } var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text()); // Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase(); tds.eq(net_carbs_i).text(carbs - fiber); if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) { alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber); tds.eq(net_carbs_i).text(carbs); } } }); var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total'); var meal_idx = 0; bottom_tr_elements.each(function() { if (jQuery(this).hasClass('remaining')) { return false; / continue / } var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { carbs = parseFloat(tds.eq(carbs_i).text()); } else { carbs = parseFloat(tds.eq(carbs_i).text()) + totalAlreadyCountedFiber; } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text()); var net_carbs = carbs; // HACK to show net carbs if (!jQuery(this).hasClass('alt')) { net_carbs = carbs - fiber + alreadyCountedFiber[meal_idx]; if (!isNaN(net_carbs)) { tds.eq(net_carbs_i).text(net_carbs); } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); } } else { // record goal net_carb_total_goal = net_carbs; }

/ do nothing if cannot calculate for the row / if (isNaN(cals) || isNaN(carbs) || isNaN(protein) || isNaN(fat) || isNaN(fiber) || isNaN(net_carbs) || cals === 0) { meal_idx++; return true; } tds.eq(net_carbs_i).text(net_carbs);

// if (net_carbs == 0 && // protein == 0 && // fat == 0) { // return true; // } var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat 9); if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('alt') && daily_total_carbs === 0) { daily_total_carbs = carb_cals; daily_total_protein = protein_cals; daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; } var real_cals = carb_cals + protein_cals + fat_cals; var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) 100).toFixed(2); //alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total); carb_pct = Math.round(carb_pct); fat_pct = Math.round(fat_pct); protein_pct = Math.round(protein_pct); tds.each(function() { jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;"> 

'); }); tds.eq(0).find('div.myfp_us').html(""); /tds.eq(calories_i).find('div.myfp_us').html(real_cals);/ if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); } if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); } if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); } meal_idx++; }); var remaining_tr_elements = jQuery('.food_container tr.total.remaining'); remaining_tr_elements.each(function() { // Show remaining as net carbs var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs)); // Fix color tds.eq(net_carbs_i).removeClass("positive"); tds.eq(net_carbs_i).removeClass("negative"); if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); } });

if (daily_total_carbs !== 0 || daily_total_protein !== 0 || daily_total_fat !== 0) { jQuery('.food_container').append('<div id="google_graph_1">

'); var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows( [ ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat] ] ); var chart = new google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left"; jQuery('.food_container').append('<div id="google_graph_2">'); var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9; var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows( [ ['Net Carbs ('

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463394622 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AsnJOfY61Z9-ARhLvlEdvuNKh3RGv0BKks5vNIxSgaJpZM4aNmUF

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463403466, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOeQyn9igCUIQybJczsCO8XLnXGJtks5vNJMQgaJpZM4aNmUF .

dsarah3 commented 5 years ago

Hi Paul, Thank you;)Sarah

Sent from Yahoo Mail on Android

On Wed, Feb 13, 2019 at 7:43 PM, pauljhobbsnotifications@github.com wrote: Hi Sarah,

I have posted a reply to the issue you logged at GitHub (issue 56).

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:34, dsarah3 notifications@github.com wrote:

Hi Paul,I did that and the net carbs column disappeared! I copied the entire script you sent, are there perhaps extra characters at the top or bottom of it that I need to delete? Thanks again for your patience, kindness and help with this, much appreciated!Sarah On Wednesday, February 13, 2019, 2:23:00 PM PST, pauljhobbs < notifications@github.com> wrote:

Hi Sarah,

The script you pasted is not the modified one I sent you. When you paste the new script into Tampermonkey, you need to save it. You can click File > Save:

[image: image.png]

Or just press Ctrl-S on your keyboard.

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:06, dsarah3 notifications@github.com wrote:

Hi Paul,Thanks for the message. No, actually the net carb count is totally off! It says I had 0 net carbs when I had 14. I'm wondering if I installed the new script properly? I opened the script in Settings, selected all, deleted the old one and cut and pasted your new one in. Perhaps that doesn't save the new script? Newbie here LOL! This is in there now: // ==UserScript==// @name MyFitnessPal Percentages and Net Carbs// @version 1.16// @namespace surye// @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs"// @downloadURL

https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js// @include http://www.myfitnesspal.com/food/diary// ==/UserScript== / ------------------------------------------------------------ Much credit to Bompus, author of the original script! Thanks to kt123 and Wickity for the fixes. ------------------------------------------------------------ /

function exec(fn) { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up} function startRun() { // Load Google API for Charts var script = document.createElement("script"); script.setAttribute("src", "// www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script); // Load jQuery script = document.createElement("script"); script.setAttribute("src", "// ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script); // Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script);} startRun(); function jsapiLoaded() { google.load("visualization", "1", { packages: ["corechart"], "callback": main });} function main() { var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0; var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0; var net_carb_total = 0; var net_carb_total_goal = 0; var header_tr_element = jQuery('.food_container tr.meal_header:first'); var elem_i = 0; header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; } elem_i += 1; });

// Add new column for net carbs var net_carb_tr_elements = jQuery('tr'); net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(carbs_i)); }); // Recalculate offsets net_carbs_i = carbs_i; calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i; carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i; fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i; protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i; fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i; // Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs var footer_tr_element = jQuery('tfoot tr'); footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0]; var alreadyCountedFiberIdx = 0; var food_tr_elements = jQuery('tr'); food_tr_elements.each(function() { if($(this).hasClass('bottom')) { alreadyCountedFiberIdx++; alreadyCountedFiber[alreadyCountedFiberIdx] = 0; } var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text()); // Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase(); tds.eq(net_carbs_i).text(carbs - fiber); if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) { alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber); tds.eq(net_carbs_i).text(carbs); } } }); var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total'); var meal_idx = 0; bottom_tr_elements.each(function() { if (jQuery(this).hasClass('remaining')) { return false; / continue / } var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { carbs = parseFloat(tds.eq(carbs_i).text()); } else { carbs = parseFloat(tds.eq(carbs_i).text()) + totalAlreadyCountedFiber; } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text()); var net_carbs = carbs; // HACK to show net carbs if (!jQuery(this).hasClass('alt')) { net_carbs = carbs - fiber + alreadyCountedFiber[meal_idx]; if (!isNaN(net_carbs)) { tds.eq(net_carbs_i).text(net_carbs); } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); } } else { // record goal net_carb_total_goal = net_carbs; }

/ do nothing if cannot calculate for the row / if (isNaN(cals) || isNaN(carbs) || isNaN(protein) || isNaN(fat) || isNaN(fiber) || isNaN(net_carbs) || cals === 0) { meal_idx++; return true; } tds.eq(net_carbs_i).text(net_carbs);

// if (net_carbs == 0 && // protein == 0 && // fat == 0) { // return true; // } var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat 9); if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('alt') && daily_total_carbs === 0) { daily_total_carbs = carb_cals; daily_total_protein = protein_cals; daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; } var real_cals = carb_cals + protein_cals + fat_cals; var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) 100).toFixed(2); //alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total); carb_pct = Math.round(carb_pct); fat_pct = Math.round(fat_pct); protein_pct = Math.round(protein_pct); tds.each(function() { jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;"> 

'); }); tds.eq(0).find('div.myfp_us').html(""); /tds.eq(calories_i).find('div.myfp_us').html(real_cals);/ if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); } if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); } if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); } meal_idx++; }); var remaining_tr_elements = jQuery('.food_container tr.total.remaining'); remaining_tr_elements.each(function() { // Show remaining as net carbs var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs)); // Fix color tds.eq(net_carbs_i).removeClass("positive"); tds.eq(net_carbs_i).removeClass("negative"); if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); } });

if (daily_total_carbs !== 0 || daily_total_protein !== 0 || daily_total_fat !== 0) { jQuery('.food_container').append('<div id="google_graph_1">

'); var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows( [ ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat] ] ); var chart = new google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left"; jQuery('.food_container').append('<div id="google_graph_2">'); var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9; var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows( [ ['Net Carbs ('

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463394622 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AsnJOfY61Z9-ARhLvlEdvuNKh3RGv0BKks5vNIxSgaJpZM4aNmUF

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463403466, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOeQyn9igCUIQybJczsCO8XLnXGJtks5vNJMQgaJpZM4aNmUF .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

dsarah3 commented 5 years ago

Hi Paul,I was so excited to see net carbs reappear, but when I do the math, it still doesn't add up.Please check this (with note on bottom right) and let me know if you think I added the script wrong, thank you!Best regards,Sarah 

On Wednesday, February 13, 2019, 7:43:43 PM PST, pauljhobbs notifications@github.com wrote:

Hi Sarah,

I have posted a reply to the issue you logged at GitHub (issue 56).

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:34, dsarah3 notifications@github.com wrote:

Hi Paul,I did that and the net carbs column disappeared! I copied the entire script you sent, are there perhaps extra characters at the top or bottom of it that I need to delete? Thanks again for your patience, kindness and help with this, much appreciated!Sarah On Wednesday, February 13, 2019, 2:23:00 PM PST, pauljhobbs < notifications@github.com> wrote:

Hi Sarah,

The script you pasted is not the modified one I sent you. When you paste the new script into Tampermonkey, you need to save it. You can click File > Save:

[image: image.png]

Or just press Ctrl-S on your keyboard.

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:06, dsarah3 notifications@github.com wrote:

Hi Paul,Thanks for the message. No, actually the net carb count is totally off! It says I had 0 net carbs when I had 14. I'm wondering if I installed the new script properly? I opened the script in Settings, selected all, deleted the old one and cut and pasted your new one in. Perhaps that doesn't save the new script? Newbie here LOL! This is in there now: // ==UserScript==// @name MyFitnessPal Percentages and Net Carbs// @version 1.16// @namespace   surye// @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs"// @downloadURL

https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js// @include http://www.myfitnesspal.com/food/diary// ==/UserScript== / ------------------------------------------------------------ Much credit to Bompus, author of the original script! Thanks to kt123 and Wickity for the fixes. ------------------------------------------------------------ /

function exec(fn) {   var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up} function startRun() { // Load Google API for Charts   var script = document.createElement("script"); script.setAttribute("src", "// www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script); // Load jQuery script = document.createElement("script"); script.setAttribute("src", "// ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script); // Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script);} startRun(); function jsapiLoaded() {   google.load("visualization", "1", { packages: ["corechart"], "callback": main });} function main() { var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0; var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0; var net_carb_total = 0; var net_carb_total_goal = 0; var header_tr_element = jQuery('.food_container tr.meal_header:first'); var elem_i = 0;   header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; }   elem_i += 1; });

// Add new column for net carbs   var net_carb_tr_elements = jQuery('tr');   net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(carbs_i)); }); // Recalculate offsets   net_carbs_i = carbs_i;   calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i;   carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i;   fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i;   protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i;   fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i; // Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs   var footer_tr_element = jQuery('tfoot tr');   footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt"); header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0]; var alreadyCountedFiberIdx = 0; var food_tr_elements = jQuery('tr');   food_tr_elements.each(function() { if($(this).hasClass('bottom')) {   alreadyCountedFiberIdx++;   alreadyCountedFiber[alreadyCountedFiberIdx] = 0; } var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text()); // Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase(); tds.eq(net_carbs_i).text(carbs - fiber); if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) {   alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber);   tds.eq(net_carbs_i).text(carbs); } } }); var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total'); var meal_idx = 0; bottom_tr_elements.each(function() { if (jQuery(this).hasClass('remaining')) { return false; / continue / } var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { carbs = parseFloat(tds.eq(carbs_i).text()); } else { carbs = parseFloat(tds.eq(carbs_i).text()) + totalAlreadyCountedFiber; } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text()); var net_carbs = carbs; // HACK to show net carbs if (!jQuery(this).hasClass('alt')) {   net_carbs = carbs - fiber + alreadyCountedFiber[meal_idx]; if (!isNaN(net_carbs)) {   tds.eq(net_carbs_i).text(net_carbs); } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); } } else { // record goal   net_carb_total_goal = net_carbs; }

/ do nothing if cannot calculate for the row / if (isNaN(cals) ||   isNaN(carbs) ||   isNaN(protein) ||   isNaN(fat) ||   isNaN(fiber) || isNaN(net_carbs) ||   cals === 0) {   meal_idx++; return true; }   tds.eq(net_carbs_i).text(net_carbs);

// if (net_carbs == 0 && // protein == 0 && // fat == 0) { // return true; // } var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat 9); if (jQuery(this).hasClass('total') &&    !jQuery(this).hasClass('alt') &&   daily_total_carbs === 0) {   daily_total_carbs = carb_cals;   daily_total_protein = protein_cals;   daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; } var real_cals = carb_cals + protein_cals + fat_cals; var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) 100).toFixed(2); //alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total);   carb_pct = Math.round(carb_pct);   fat_pct = Math.round(fat_pct);   protein_pct = Math.round(protein_pct);   tds.each(function() {   jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;"> 

'); });   tds.eq(0).find('div.myfp_us').html(""); /tds.eq(calories_i).find('div.myfp_us').html(real_cals);/ if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); } if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); } if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); }   meal_idx++; }); var remaining_tr_elements = jQuery('.food_container tr.total.remaining');   remaining_tr_elements.each(function() { // Show remaining as net carbs   var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs)); // Fix color   tds.eq(net_carbs_i).removeClass("positive");   tds.eq(net_carbs_i).removeClass("negative"); if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); } });

if (daily_total_carbs !== 0 ||   daily_total_protein !== 0 ||   daily_total_fat !== 0) {   jQuery('.food_container').append('<div id="google_graph_1">

'); var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows(   [  ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat]   ]   );   var chart = new google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left";   jQuery('.food_container').append('<div id="google_graph_2">'); var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9; var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows(   [  ['Net Carbs ('

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463394622 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AsnJOfY61Z9-ARhLvlEdvuNKh3RGv0BKks5vNIxSgaJpZM4aNmUF

 .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463403466, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOeQyn9igCUIQybJczsCO8XLnXGJtks5vNJMQgaJpZM4aNmUF  .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

ghost commented 5 years ago

Hi Sarah,

Could you please take a screenshot of the full page of food items, as well as the total section at the bottom. I'd like to see what your macros are and see if I can figure out what is going on with the Net Carbs total. If you like, feel free to email me directly at paul@goopod.com.

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 17:13, dsarah3 notifications@github.com wrote:

Hi Paul,I was so excited to see net carbs reappear, but when I do the math, it still doesn't add up.Please check this (with note on bottom right) and let me know if you think I added the script wrong, thank you!Best regards,Sarah

On Wednesday, February 13, 2019, 7:43:43 PM PST, pauljhobbs < notifications@github.com> wrote:

Hi Sarah,

I have posted a reply to the issue you logged at GitHub (issue 56).

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:34, dsarah3 notifications@github.com wrote:

Hi Paul,I did that and the net carbs column disappeared! I copied the entire script you sent, are there perhaps extra characters at the top or bottom of it that I need to delete? Thanks again for your patience, kindness and help with this, much appreciated!Sarah On Wednesday, February 13, 2019, 2:23:00 PM PST, pauljhobbs < notifications@github.com> wrote:

Hi Sarah,

The script you pasted is not the modified one I sent you. When you paste the new script into Tampermonkey, you need to save it. You can click File > Save:

[image: image.png]

Or just press Ctrl-S on your keyboard.

Cheers,

Paul +61 431 986 756

On Thu, 14 Feb 2019 at 08:06, dsarah3 notifications@github.com wrote:

Hi Paul,Thanks for the message. No, actually the net carb count is totally off! It says I had 0 net carbs when I had 14. I'm wondering if I installed the new script properly? I opened the script in Settings, selected all, deleted the old one and cut and pasted your new one in. Perhaps that doesn't save the new script? Newbie here LOL! This is in there now: // ==UserScript==// @name MyFitnessPal Percentages and Net Carbs// @version 1.16// @namespace surye// @description Adds display of Carb/Protein/Fat percentages to any daily food diary page. Also adds "Real Calories" calcalation based off 4/4/9 algorithm. Based on "MyFitnessPal Percentages and Net Carbs"// @downloadURL

https://github.com/Surye/mfp-keto-userscript/raw/master/mfpketo.user.js//

@include http://www.myfitnesspal.com/food/diary// ==/UserScript== / ------------------------------------------------------------ Much credit to Bompus, author of the original script! Thanks to kt123 and Wickity for the fixes. ------------------------------------------------------------ /

function exec(fn) { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = '(' + fn + ')();'; document.body.appendChild(script); // run the script document.body.removeChild(script); // clean up} function startRun() { // Load Google API for Charts var script = document.createElement("script"); script.setAttribute("src", "// www.google.com/jsapi"); script.addEventListener('load', function() { exec(jsapiLoaded); }, false); document.body.appendChild(script); // Load jQuery script = document.createElement("script"); script.setAttribute("src", "// ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"); script.addEventListener('load', function() { exec("jQuery.noConflict()"); }, false); document.body.appendChild(script); // Inject this script into page. script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = main; document.body.appendChild(script);} startRun(); function jsapiLoaded() { google.load("visualization", "1", { packages: ["corechart"], "callback": main });} function main() { var calories_i = 0; var net_carbs_i = 0; var carbs_i = 0; var fiber_i = 0; var protein_i = 0; var fat_i = 0; var daily_total_carbs = 0; var daily_total_protein = 0; var daily_total_fat = 0; var net_carb_total = 0; var net_carb_total_goal = 0; var header_tr_element = jQuery('.food_container tr.meal_header:first'); var elem_i = 0; header_tr_element.find('td').each(function() { var myval = jQuery(this).text().toLowerCase().trim(); if (myval.indexOf('calories') !== -1) { calories_i = elem_i; } if (myval.indexOf('carbs') !== -1) { carbs_i = elem_i; } if (myval.indexOf('fiber') !== -1) { fiber_i = elem_i; } if (myval.indexOf('fat') !== -1) { fat_i = elem_i; } if (myval.indexOf('protein') !== -1) { protein_i = elem_i; } elem_i += 1; });

// Add new column for net carbs var net_carb_tr_elements = jQuery('tr'); net_carb_tr_elements.each(function() { var tds = jQuery(this).find('td'); jQuery('').insertBefore(tds.eq(carbs_i)); }); // Recalculate offsets net_carbs_i = carbs_i; calories_i = calories_i >= net_carbs_i ? calories_i + 1 : calories_i; carbs_i = carbs_i >= net_carbs_i ? carbs_i + 1 : carbs_i; fiber_i = fiber_i >= net_carbs_i ? fiber_i + 1 : fiber_i; protein_i = protein_i >= net_carbs_i ? protein_i + 1 : protein_i; fat_i = fat_i >= net_carbs_i ? fat_i + 1 : fat_i; // Set header header_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); header_tr_element.find('td').eq(net_carbs_i).addClass("alt");

header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

// Change to say net carbs var footer_tr_element = jQuery('tfoot tr'); footer_tr_element.find('td').eq(net_carbs_i).text("Net Carbs"); footer_tr_element.find('td').eq(net_carbs_i).addClass("alt");

header_tr_element.find('td').eq(net_carbs_i).addClass("nutrient-column");

var alreadyCountedFiber = [0]; var alreadyCountedFiberIdx = 0; var food_tr_elements = jQuery('tr'); food_tr_elements.each(function() { if($(this).hasClass('bottom')) { alreadyCountedFiberIdx++; alreadyCountedFiber[alreadyCountedFiberIdx] = 0; } var tds = jQuery(this).find('td'); var carbs = parseFloat(tds.eq(carbs_i).text()); var fiber = parseFloat(tds.eq(fiber_i).text()); // Find only food rows! var delete_td = tds.eq(tds.length - 1); if (delete_td.hasClass('delete')) { var name = jQuery(this).find('.js-show-edit-food').text().toLowerCase(); tds.eq(net_carbs_i).text(carbs - fiber); if (name.indexOf("net carbs") !== -1 || (carbs - fiber) < 0) { alreadyCountedFiber[alreadyCountedFiberIdx] += Number(fiber); tds.eq(net_carbs_i).text(carbs); } } }); var totalAlreadyCountedFiber = 0; for (var i=0; i < alreadyCountedFiber.length; i++){ totalAlreadyCountedFiber += alreadyCountedFiber[i];}

var bottom_tr_elements = jQuery('.food_container tr.bottom, .food_container tr.total'); var meal_idx = 0; bottom_tr_elements.each(function() { if (jQuery(this).hasClass('remaining')) { return false; / continue / } var tds = jQuery(this).find('td'); var cals = parseFloat(tds.eq(calories_i).text()); var carbs = 0; if($(this).hasClass('bottom')) { carbs = parseFloat(tds.eq(carbs_i).text()); } else { carbs = parseFloat(tds.eq(carbs_i).text()) + totalAlreadyCountedFiber; } var fiber = parseFloat(tds.eq(fiber_i).text()); var protein = parseFloat(tds.eq(protein_i).text()); var fat = parseFloat(tds.eq(fat_i).text()); var net_carbs = carbs; // HACK to show net carbs if (!jQuery(this).hasClass('alt')) { net_carbs = carbs - fiber + alreadyCountedFiber[meal_idx]; if (!isNaN(net_carbs)) { tds.eq(net_carbs_i).text(net_carbs); } else if (jQuery(this).hasClass("total")) { tds.eq(net_carbs_i).text("0"); } } else { // record goal net_carb_total_goal = net_carbs; }

/ do nothing if cannot calculate for the row / if (isNaN(cals) || isNaN(carbs) || isNaN(protein) || isNaN(fat) || isNaN(fiber) || isNaN(net_carbs) || cals === 0) { meal_idx++; return true; } tds.eq(net_carbs_i).text(net_carbs);

// if (net_carbs == 0 && // protein == 0 && // fat == 0) { // return true; // } var carb_cals = (net_carbs 4); var protein_cals = (protein 4); var fat_cals = (fat 9); if (jQuery(this).hasClass('total') && !jQuery(this).hasClass('alt') && daily_total_carbs === 0) { daily_total_carbs = carb_cals; daily_total_protein = protein_cals; daily_total_fat = fat_cals; net_carb_total = net_carbs - totalAlreadyCountedFiber; } var real_cals = carb_cals + protein_cals + fat_cals; var carb_pct = ((carb_cals / real_cals) 100).toFixed(2); var fat_pct = ((fat_cals / real_cals) 100).toFixed(2); var protein_pct = ((protein_cals / real_cals) 100).toFixed(2); //alert(daily_total_carbs + ", " + daily_total_protein + ", " + daily_total_fat + ", " + net_carb_total); carb_pct = Math.round(carb_pct); fat_pct = Math.round(fat_pct); protein_pct = Math.round(protein_pct); tds.each(function() { jQuery(this).append('<div class="myfp_us" style="color:#0a0;font-size:9px;text-align:center;"> 

'); }); tds.eq(0).find('div.myfp_us').html(""); /tds.eq(calories_i).find('div.myfp_us').html(real_cals);/ if (!isNaN(carb_pct)) { tds.eq(net_carbs_i).find('div.myfp_us').html(carb_pct + "%"); } if (!isNaN(fat_pct)) { tds.eq(fat_i).find('div.myfp_us').html(fat_pct + "%"); } if (!isNaN(protein_pct)) { tds.eq(protein_i).find('div.myfp_us').html(protein_pct + "%"); } meal_idx++; }); var remaining_tr_elements = jQuery('.food_container tr.total.remaining'); remaining_tr_elements.each(function() { // Show remaining as net carbs var net_carbs = net_carb_total_goal - net_carb_total - totalAlreadyCountedFiber; var tds = jQuery(this).find('td'); tds.eq(net_carbs_i).text(parseInt(net_carbs)); // Fix color tds.eq(net_carbs_i).removeClass("positive"); tds.eq(net_carbs_i).removeClass("negative"); if (net_carbs < 0) { tds.eq(net_carbs_i).addClass("negative"); } else { tds.eq(net_carbs_i).addClass("positive"); } });

if (daily_total_carbs !== 0 || daily_total_protein !== 0 || daily_total_fat !== 0) { jQuery('.food_container').append('<div id="google_graph_1">

'); var data1 = new google.visualization.DataTable(); data1.addColumn('string', 'Type'); data1.addColumn('number', 'Cals'); data1.addRows( [ ['Net Carbs', daily_total_carbs], ['Protein', daily_total_protein], ['Fat', daily_total_fat] ] ); var chart = new

google.visualization.PieChart(document.getElementById('google_graph_1')); chart.draw(data1, { width: 350, height: 300, title: 'Daily Totals by Calories (This is what you use for your macro ratios)' }); document.getElementById('google_graph_1').style.cssFloat = "left"; jQuery('.food_container').append('<div id="google_graph_2">'); var carb_grams = daily_total_carbs / 4; var pro_grams = daily_total_protein / 4; var fat_grams = daily_total_fat / 9; var data2 = new google.visualization.DataTable(); data2.addColumn('string', 'Type'); data2.addColumn('number', 'Grams'); data2.addRows( [ ['Net Carbs ('

google.visualization.PieChart(document.getElementById('google_graph_2')); chart2.draw(data2, { width: 350, height: 300, title: 'Daily Totals by Grams' }); document.getElementById('google_graph_2').style.cssFloat = "right"; }}

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <

https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463394622

, or mute the thread <

https://github.com/notifications/unsubscribe-auth/AsnJOfY61Z9-ARhLvlEdvuNKh3RGv0BKks5vNIxSgaJpZM4aNmUF

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463403466 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AsnJOeQyn9igCUIQybJczsCO8XLnXGJtks5vNJMQgaJpZM4aNmUF

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Surye/mfp-keto-userscript/issues/51#issuecomment-463518192, or mute the thread https://github.com/notifications/unsubscribe-auth/AsnJOcDrS0Fezo7fmU_3M5KyZTNNLvS4ks5vNQyzgaJpZM4aNmUF .