JavaScript Bookmarklet for Enhanced Macro Goals in MyFitnessPal

 Aug, 13 - 2013   299 comments   ProjectsThings that are broken
Please read this post from Steven at MyFitnessPal regarding the change to allow setting macros by gram, along with other advanced nutrition options as part of their new premium service.

This JavaScript bookmarklet will enhance the macro target section of the custom goal editor  in MyFitnessPal with some advanced features that allow you to set up a more complex macro strategy.

Features:

  • Set percentages in 1% increments
  • Set targets in grams rather than percentage
  • Set one of the three macros to  automatically fill any remaining calories in your daily goal that are not allocated to the other two macros
  • Remember your advanced settings as long as you do not overwrite your changes with the default MFP 5% scheme

Screen Shot 2013-08-13 at 9.22.29 AM
Example – Specify protein in grams, fat as a percentage, fill remaining calories with carbs

 

How To Install The Script

 

The script is available as a userscript for use in browser extensions like Greasemonkey (for FireFox) and Tampermonkey (for Chrome). Running like this, the fix is always there, even after you update, and you never have to manually run anything. You can install the script here:
http://karoshiethos.com/js/MFP_Macro_Fix.user.js

Internet Explorer and Safari users need to add the bookmarklet below to their bookmarks. When you are on the MyFitnessPal custom goal screen, click on the bookmark, and the form will be enhanced with these features. After you save your changes, the form will revert back to the default 5% increments, but your gram counts will be correct. The mobile apps will display your chosen percentages and grams properly. You will need to click the bookmark each time you want to change your macros.

Internet explorer users, right click the link in the blue box below and click “Add to Favorites”. Safari users, drag the link up to your bookmarks bar.

 

MyFitnessPal Macro Enhancements

 

 

How does it work?

The script is a small JavaScript class that adds components and logic, and integrates with the existing logic in the custom goals screen. When you are finished with your changes and submit the form, the grams for each macro are translated into high-resolution percentages (Example: Carb=44.56666666666666%, Protein=30.533333333333335%, Fat=24.9%), which are saved exactly the same way the default 5% increment percentages would be. MyFitnessPal will round the percentages to the nearest 1% when displaying percentages on the website and in the mobile apps, but they appear to use the high resolution percentages for calculating grams. In my testing, the gram counts always come out as specified.

I’m not sure why this functionality is not already available in MyFitnessPal, I think most users who are serious about their diet probably set up their macros by the gram, or in some combination of grams and percentages.

This is a quick fix I put together in my spare time. It may have bugs. If so, I may or may not fix them. I have tested this in Chrome, Firefox, Safari, and IE10 and 11 – it may not work in IE6, IE4 for Mac, NCSA Mosaic, Lynx, etc. in other words, if you are not running an up to date browser, YMMV. If you need help and your questions are not already answered in the comments, you can find me on Twitter at @robruchte or email me at rob.ruchte@gmail.com


Related articles

 Comments 299 comments

  • Stephan says:

    This is an amazing script! Any chance of combining it with the LeanGains auto MFP updater python script? 
    Or perhaps a whole new script that understands carb/calorie cycling?

    http://www.reddit.com/r/leangains/comments/ny4km/i_made_myfitnesspal_work_with_lg_testers_please/

    ReplyReply
  • susan says:

    this is awesome but I am having a serious issue with it. I installed on both safarie as well as firefox. after i enter my numbers under grams i get a pop up script from MFP saying macros are out of range to meet my number of calories 🙁 any suggestions as to how i can prevent this? or do i need to just adjust the calories to what my macros are? ( if thats the case i am screwed hahaha)
     
    i would love your thoughts thanks!

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    @susan, yes, if you set all three macros to grams, the values you set must add up to your calorie goal. Fat=9cal Carb & Protein=4cal. I suggest setting one of your macros to fill (double arrow), then set your grams for the other two. That way the script figures out the math for you.

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    @Ophelia Sounds like you are using the bookmarklet and saving your goals without running the bookmarklet after saving the with the bookmarklet. Try using greasemonkey.

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    @Stephan I’ll look into that, there are a lot of functionality I would like to add to MFP. Thanks for turning me on to it.

    ReplyReply
  • Ophellia says:

    Hiya thanks for your reply. Again sorry if it’s a stupid question, but how do I use greasemonkey? I installed it just before I downloaded your program and thought it just did it thing automatically. Sorry again x

    ReplyReply
  • Jason says:

    Just found this today – awesome for job!

    ReplyReply
  • Morit says:

    Thank you!!!

    ReplyReply
  • Mary says:

    Worked great! Followed your instructions for Chrome, no troubles installing.  New numbers have carried over to my Android app on Samsung Galaxy S5, for the most part. Under “Nutrition” and under “Diary” tabs all info shows my updated percentages and numbers. But under “Goals”, it still lists the old 5% percentages (my goals are 50%, 23%, 27%). I don’t think this is a problem, just putting it out there as an FYI to other people. 

    ReplyReply
  • This is excellent.  Would you mind if I shared this and linked back to your page on my blog?  I know my readers would benefit from this post.  Thanks – Erin

    ReplyReply
  • Max says:

    I am using Firefox , Greasemonkey is installed and the script is activated.
    1 hour ago, the app reduced my calories from 3200 to 3000 and deactivated the script, so that is have to use the 5%-steps.
    Anyone having this problem too?

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    It’s working fine for me.

    ReplyReply
  • monica says:

    Wow!  Thank you so much!!!  You are a genius!!!
     

    ReplyReply
  • x says:

    I installed this app via tampermonkey in chrome but it’s not working for me :/ mfp stay’s the same

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    It’s working for me.

    ReplyReply
  • m says:

    Thank you! just did it today on google chrome and it worked just fine. 
    I sincerely appreciate it. 

    ReplyReply
  • m says:

    it doesnt work in chrome. I did everything like you say, still doesnt work

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    It’s working for me in Chrome. Follow the instructions in the video carefully.

    ReplyReply
  • Lin says:

    Hi Rob, 
    I ran the userscript using Tampermonkey with Chrome. Worked a charm. However, after this,  I am noticing my MFP has problems calculating the total calories for the day.  The actual total calories  doesn’t match up to the macros.  Help! 
    Lin

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    That’s a problem in MFP with or without the script, the script doesn’t have any impact on how MFP calculates your calorie total. MFP does not calculate calories based on the macro nutrients in the food data,it uses to total calories in the food data. So you could have a food record with 1 gram carb, 1 gram fat, and one gram protein (should add up to 17kcal) but the total calories could be listed at 15 kcal, so MFP will log 15kcal. These discrepancies could be due to rounding, or just mistakes in the data.

    ReplyReply
  • Janet Ruffin says:

    Thank you.  Once I was able to reinstall chrome and download Tampermonkey – this worked absolutely wonderful.  Thank you!

    ReplyReply
  • Lin says:

    Hi Rob,
    Thanks for much your time and patience in addressing each and every one of these enquiries, even so long after you put out the original script. The length of this comments thread is testament to that. It certainly is appreciated! You rock! 🙂

    ReplyReply
  • Lin says:

    Also another question.  I am using MFP on android 4.4. When it syncs, it still doesn’t give me the exact macros I entered on MFP using the userscript? Its very close,  eg, 48g fat instead of 45g. Total calorie goal is correct. I think it rounded to the nearest 5%. I tried uninstalling and reinstalling MFP, to no avail.  Is there anything I can do?

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    That’s really odd, I’ve never seen that happen. I use iOS, but it’s always exactly what I enter.

    ReplyReply
  • Georgie says:

    Hi Rob,

    Thank you so much for sharing this!

    I’ve read your response to @susan, but I’m still confused – I’m sorry. I’m getting the same error as she was and can’t work out how to fix it. The macros I’m trying to set are 130g for carbs, 130g for protein & 35g for fats but it won’t let me. I then tried changing the kjs to 1355 (9×35=315 + 4×130=520 + 4×130 = 1355) but it still won’t allow me to save it.

    Could you please help me?

    Thank you so much!

    With kind regards,
    Georgie

    @susan, yes, if you set all three macros to grams, the values you set must add up to your calorie goal. Fat=9cal Carb & Protein=4cal. I suggest setting one of your macros to fill (double arrow), then set your grams for the other two. That way the script figures out the math for you.

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    @Georgie That’s odd, I can’t recreate the problem with the macro values that you listed. Do you get the “Macros out of range” message?

    ReplyReply
  • Greg T says:

    Worked like a charm ! Love it

    ReplyReply
  • Chris says:

    Good stuff man thanks a lot!

    ReplyReply
  • Georgie says:

    Hi Rob,

    Thank you for your reply.

    Yes, I get the “Macros are out of range, please adjust your choices to meet your total of 1355 calories” when trying to put in 130g for both carbohydrates and protein and 35g for fat.

    I cannot for the life of me get it to save!

    With kind regards,
    Georgie

    ReplyReply
  • Siew says:

    Thank you. You are awesome!!

    ReplyReply
  • Nadia says:

    Thank you!

    ReplyReply
  • Phil says:

    NOPE! I added the tampermonkey for chrome extension. It has then gone to the top right corner next to the address bar but it wont work. went back in and out of chrome and myfitnesspal and it does’nt do anything.
    My tampermonkey logo is grey and looks like it is inactive

    ReplyReply
  • Krystal says:

    Hi! great great script! very useful, but i keep getting this trouble ,i don’t know if t happens to anyone else too. i did every step in the video for google chrome and after i log my macros and and hit save it i get this: “There was a problem with your request
    Sorry, but a server error occurred processing your request. Our team has been notified of the issue.” and it puts crazy big values on my macros. please help. Thank you very much

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    I haven’t seen that happen, but if you are getting huge numbers in your macros, it sounds like your calorie goal is set to something really high.

    ReplyReply
  • Janet says:

    Followed your instruction video using GreaseMonkey with FireFox. Worked like a charm. Thank you SO much! 🙂

    ReplyReply
  • Matt says:

    Worked great, your Awesome man !! thanks for doing this

    ReplyReply
  • As says:

    Any chance you could create something for iOS?

    ReplyReply
  • amy says:

    thank you!! what an awesome thing to discover today! 🙂

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    Yes, there is a way to install the bookmarklet in iOS, but it’s a bit tricky.

    1. Go this page: http://karoshiethos.com/mfp_cwfix_ios.html
    2. Bookmark the page, call it something you will recognize later, like MFP Macro Fix
    3. Copy all of the code on the page
    4. Open your bookemrks, tap Edit, and edit the bookmark you just created. Paste the contents of the page into the URL field and save the bookmark.
    5. Now you can go to the custom goals screen, tap the bookmark, and it will run the script.

      ReplyReply
  • jenn says:

    installed it on chrome on my macbook and malware came along with it! now i get pop ups that lock up my chrome and safari… great

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    That’s from something else. All my script does is change the way a form on MyFitnessPal works.

    ReplyReply
  • Tonya Striefel says:

    Any chance this will update for a Kindle fire application?

    ReplyReply
  • Rob Ruchte Rob Ruchte says:

    Yes, changes to your goals will be reflected in the Kindle app, just like iOS and Android.

    ReplyReply
  • Gav says:

    This is awesome, great work thanks for this.

    ReplyReply
  • Sarah G says:

    Thank you for this – so helpful!

    ReplyReply
  • Strongman says:

    Hello.
    It did not work for me in Chrome. So I had to add in the “user include” in TM edit this line “http://www.myfitnesspal.se/en/account/change_goals_custom”
    And it works fine now.

    ReplyReply
  • 42Libs says:

    Exhale: Ahhhhh. Thank you =D

    ReplyReply
  • Fareeha says:

    Sincerely, thank you! 🙂

    ReplyReply
  • J. B. Rainsberger says:

    Thank you for building this. I find it very helpful. I think I could hack on this a little to add support for fat profile: 50% saturated, 25% PUFA, 25% MUFA, 0% trans fat.

    ReplyReply
  • Dexx says:

    I had no idea something like this existed. For the longest time I had just stuck with the 5% incremental options and did my best to remember what the actual target was I was trying to hit. Thank you, thank you, thank you for creating this!

    ReplyReply

  • Leave a Reply

    Your email address will not be published. Fields with * are mandatory.