Created
June 25, 2023 17:51
-
-
Save joeperpetua/9ab98c2e6af383194205d030e4e87e22 to your computer and use it in GitHub Desktop.
Add button to calculate AVG values for MFP reports for Calories, Net Calories, Protein, Carbs and Fat.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name MyFitnessPal - Graph Average values | |
// @version 0.1 | |
// @description Add button to calculate AVG values for MFP reports for Calories, Net Calories, Protein, Carbs and Fat. | |
// @author https://github.com/joeperpetua | |
// @match https://www.myfitnesspal.com/reports | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=myfitnesspal.com | |
// @grant none | |
// ==/UserScript== | |
const sleep = async (time) => { | |
return new Promise(resolve => setTimeout(resolve, time)); | |
} | |
const heightValuesRatio = { | |
'Calories': 12.5221238938, | |
'Net Calories': 12.5221238938, | |
'Carbs': 2.08602151, | |
'Fat': 0.52073732718, | |
'Protein': 0.83419689119 | |
}; | |
function initializeDOM(){ | |
let reportingPeriodDiv = document.querySelector(".report-periods"); | |
let calculateButton = document.createElement("button"); | |
calculateButton.textContent = "Calculate graph AVG value"; | |
calculateButton.onclick = handleAction; | |
reportingPeriodDiv.after(calculateButton); | |
} | |
function handleAction(){ | |
let graphType = document.querySelector('.chzn-single').textContent; | |
let avgHeight = calculateHeightAVG(); | |
let done = false; | |
Object.keys(heightValuesRatio).forEach((label) => { | |
if (graphType === label){ | |
let valueAVG = Math.floor(avgHeight.avg * heightValuesRatio[label]); | |
alert(`Data type: ${label}\nDays calculated: ${avgHeight.days}\nAverage value: ${valueAVG}`); | |
console.log('Ratio:', label, heightValuesRatio[label]); | |
done = true; | |
} | |
}); | |
done ? null : alert(`Data type not compatible.\nCompatible data type: ${Object.keys(heightValuesRatio)}`); | |
} | |
function calculateHeightAVG(){ | |
let graph = document.querySelector(".highcharts-series.highcharts-tracker"); | |
let sum = 0, count = 0; | |
graph?.childNodes.forEach(record => { | |
let value = parseInt(record.height.baseVal.value); | |
if (value < 1){ | |
return; | |
} | |
sum += value; | |
count++; | |
}); | |
return {days: count, avg: sum / count}; | |
} | |
(async function() { | |
'use strict'; | |
await sleep(2000); | |
initializeDOM(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment