Last active
July 21, 2021 13:14
-
-
Save stevenirby/cbb26cb1571915952f1ccad99f058cd9 to your computer and use it in GitHub Desktop.
Cronometer webhooker - push calories and carbs to webhook.
This file contains hidden or 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 Cronometer carb and calorie webhooker | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Send net carbs and calories to a webhook. | |
// @author Steven | |
// @match https://cronometer.com/ | |
// @grant none | |
// @run-at document-end | |
// ==/UserScript== | |
'use strict'; | |
const webhookUrl = "https://patchbay.pub/k8u4-c4sm"; | |
// only work on diary page | |
if ( !/#diary.*/.test(location.hash)) { | |
return; | |
} | |
const main = function() { | |
let carbs = document.querySelector(".diary_side_box .summary-carbs").textContent; | |
carbs = carbs.replace(/\s\(\d+%\)/gi, "").replace(/\sg/gi, "").replace(/\s+/gi, "").split("/"); | |
const carbsAllowed = carbs[1]; | |
const calories = document.querySelectorAll(".diary_side_box img")[1].nextElementSibling.textContent; | |
const totalCarbs = Math.round(carbsAllowed - parseFloat(carbs[0])); | |
const totalCalories = parseInt(calories, 10); | |
if (totalCarbs !== "" && totalCalories !== "") { | |
const message = JSON.stringify({ | |
carbs: totalCarbs, | |
calories: totalCalories, | |
}); | |
fetch(webhookUrl, { | |
method: 'POST', | |
body: `data: ${message}\n\n`, | |
}); | |
} | |
// Just refesh the page after a while. | |
setInterval(function() { | |
window.location.reload(); | |
}, 1800000); | |
} | |
// Push data to the webhook after 10 seconds giving time to log food. | |
function ready() { | |
setInterval(main, 10000); | |
} | |
window.addEventListener("load", ready); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment