Ocado Price Sorter and fast checkout

Automatically set the Ocado price dropdown with name="sortBy" to "Price per: Low to High". Skip through all "continue checkout" buttons

// ==UserScript==
// @name         Ocado Price Sorter and fast checkout
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Automatically set the Ocado price dropdown with name="sortBy" to "Price per: Low to High".  Skip through all "continue checkout" buttons
// @author       Pepe
// @match        https://www.ocado.com/*
// @icon         https://www.google.com/s2/favicons?domain=ocado.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to set the dropdown to "Price per: Low to High"
    function setDropdown() {
        // Wait for the dropdown to exist in the DOM
        const dropdownInterval = setInterval(() => {
            const dropdown = document.querySelector('select[name="sortBy"]');
            if (dropdown) {
                clearInterval(dropdownInterval);

                // Set the value to "Price per: Low to High"
                for (const option of dropdown.options) {
                    if (option.textContent.includes("Price per: Low to High")) {
                        dropdown.value = option.value;
                        dropdown.dispatchEvent(new Event('change', { bubbles: true }));
                        break;
                    }
                }
            }
        }, 500);
    }

    function clickCheckoutButton() {
        let buttons = document.querySelectorAll('button, input[type="button"], input[type="submit"]');
        for (let button of buttons) {
            if (button.textContent.trim() === "Continue checkout") {
                console.log("Clicking 'Continue checkout' button.");
                button.click();
                return;
            }
        }
    }

    // Run on page load
    clickCheckoutButton();


    // Run the function when the page loads
    window.addEventListener('load', setDropdown);


    // Detect changes in the search results using MutationObserver
    const observer = new MutationObserver(setDropdown);
    observer.observe(document.body, { childList: true, subtree: true });

    // Detect changes in the URL (e.g., when a new search is performed)
    let lastUrl = location.href;
    setInterval(() => {
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            setDropdown();
        }
    }, 1000);

})();