// ==UserScript== // @name Ocado Price Sorter // @namespace http://tampermonkey.net/ // @version 1.3 // @description Automatically set the Ocado price dropdown with name="sortBy" to "Price per: Low to High" after search or nav on Ocado.com // @author. pepepepepe // @match https://www.ocado.com/* // @icon https://www.google.com/s2/favicons?domain=ocado.com // @grant none // @license MIT // @downloadURL none // ==/UserScript== (function () { 'use strict'; let dropdownSet = false; function waitForDropdownAndSet() { let attempts = 0; const maxAttempts = 20; const interval = setInterval(() => { const dropdown = document.querySelector('select[name="sortBy"]'); if (dropdown) { for (const option of dropdown.options) { if (option.textContent.includes("Price per: Low to High")) { if (dropdown.value !== option.value) { dropdown.value = option.value; dropdown.dispatchEvent(new Event('change', { bubbles: true })); console.log("✅ Dropdown set to: Price per: Low to High"); } dropdownSet = true; break; } } clearInterval(interval); } if (++attempts > maxAttempts) { console.warn("⚠️ Could not find dropdown after waiting."); clearInterval(interval); } }, 500); } function handleUrlChange() { dropdownSet = false; waitForDropdownAndSet(); } // Click checkout button if on relevant page function clickCheckoutButton() { const 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(); break; } } } if (location.href.includes("/checkout")) { window.addEventListener("load", clickCheckoutButton); } // Detect SPA-style URL changes let lastUrl = location.href; setInterval(() => { const currentUrl = location.href; if (currentUrl !== lastUrl) { lastUrl = currentUrl; console.log("🔄 Detected URL change to:", currentUrl); handleUrlChange(); } }, 500); // Initial run window.addEventListener('load', waitForDropdownAndSet); })();