Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Created September 12, 2022 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjaap/1b473e4ebb66be6138574e9bf338743b to your computer and use it in GitHub Desktop.
Save peterjaap/1b473e4ebb66be6138574e9bf338743b to your computer and use it in GitHub Desktop.
Fitchef to MyFitnessPal Greasemonkey script
// ==UserScript==
// @name Fitchef to MyFitnessPal
// @version 0.1
// @description Voeg link toe aaan Fitchef.nl om een recept aan MyFitnessPal toe te voegen
// @author Peter Jaap Blaakmeer
// @match https://fitchef.nl/weekschema/*/*
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
let alreadySet = false;
setInterval(function () {
if (jQuery('.meal-plan-day').length) {
if (!alreadySet) {
jQuery('.meal-plan-day__item').each(function (index, card) {
card = jQuery(card);
let ingredientsArray = [];
card.find('ul.meal-plan-day-ingredients li').each(function (index, li) { ingredientsArray.push(jQuery(li).text()); });
let ingredients = encodeURIComponent(ingredientsArray.join('\r\n'));
let name = encodeURIComponent(card.find('h3').eq(0).text());
let backgroundImage = card.find('figure').css('background-image');
let urlRegex = /(https?:\/\/[^ ]*)/;
const urlMatches = backgroundImage.match(urlRegex);
let pictureUrl = encodeURIComponent(urlMatches[0]);
let url = encodeURIComponent(document.location.href);
let servings = 1;
let myFitnessPalUrl = 'https://www.myfitnesspal.com/recipe/edit_v2?action=parse&controller=recipe&hash=484e16d977832970746be4bac0748d15&ingredient_section=' + ingredients + '&name=' + name + '&picture_url=' + pictureUrl + '&servings=' + servings + '&url=' + url;
let myFitnessPalButton = jQuery('<a>').attr('href', myFitnessPalUrl).text('Voeg toe aan MyFitnessPal');
card.find('ul.meal-plan-day-ingredients').parent().append(myFitnessPalButton);
});
alreadySet = true;
}
}
}, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment