Cheaper Colruyt mealboxes

Background & Concept

Back with another Greasemonkey script. A friend of mine discovered a bit of a loophole in Colruyt’s mealbox service: when you order the same ingredients from the mealbox from Collect & Go, you pay less. Collect & Go is a service by Colruyt where you can order your groceries online and collect them later at the physical store. The mealboxes are very much alike, except for two things. One is that they alter the portions to perfectly match the recipe (so sometimes 0.75 pots of yoghurt are included in the mealbox). The other is that it’s a preconfigured package, so you just have to order the one thing and there’ll be some 15 ingredients waiting for you at the store.

So my friend figured he’d just copy all ingredients from the mealbox page, and insert them into Collect & Go. This turned out to save him money, while he was getting more (the 0.75 pots of yoghurt are of course not for sale in the collect & go). Since copying all these ingredients is somewhat time consuming, I wrote a script to automate the process.

Main consists of two functions, extract_ingredients and buy_ingredient. The first one fetches all ingredients and opens new tabs that search for these ingredients. The second one buys the first search result once and then closes the tab again.

This is more of a proof of concept, it’s not robust at all, but it works.

Code

// ==UserScript==
// @name         Collect & Go
// @version      0.1
// @description  Script to fetch ingredients from Colruyt mealboxes and add them to cart on Collect and Go
// @author       You
// @match        https://colruyt.collectandgo.be/*
// @grant        https://colruyt.collectandgo.be/*
// ==/UserScript==

'use strict';

let URL_MODIFIER = "tntntn"; // this can be anything that colruyt doesn't recognize as a parameter

let extract_ingredients = function() {
    console.log("Extracting ingredients");

    let recipe__ingredients = document.getElementsByClassName("recipe__ingredients");
    let ingredients = recipe__ingredients[0].children[1].children;
    console.log(ingredients.length);
    console.log("Opening new tab per ingredient");

    let urlString;
    for (let i=0; i<ingredients.length; i++){
        urlString = 'https://colruyt.collectandgo.be/cogo/nl/zoeken?z=';
        urlString += ingredients[i].innerText;
        urlString += '&category=generic';
        urlString += "&" + URL_MODIFIER + "=true"; // modify URL so that other script can take over

        window.open(urlString, '_blank'); // _blank means open in new tab
    }

    console.log("Ingredient extraction done");

}

let buy_ingredient = function() {
    console.log("Buying ingredient");

  	let articles = document.querySelector("#articles").firstChild.firstChild; // buy first item once
  	console.log("Got articles:");
  	console.log(articles);
    articles.lastChild.lastChild.firstChild.firstChild.submit()
    window.close();

    console.log("Done buying ingredient");
}

let main = function() {
  console.log("Running greasemonkey script!");

  let url = window.location.href;
  if (url.indexOf("recipedetail") > -1) { // URL contains "recipedetail"
    extract_ingredients();
  }
  if (url.indexOf(URL_MODIFIER) > -1) { // URL was modified by this script
  	buy_ingredient();
  }

  console.log("Done running greasemonkey script!");
}

main();