Instacart Automated Sort & Load

Automatically loads all items on Instacart sales pages and sorts them by price (low to high). No user interface.

  1. // ==UserScript==
  2. // @name Instacart Automated Sort & Load
  3. // @description Automatically loads all items on Instacart sales pages and sorts them by price (low to high). No user interface.
  4. // @version 2.2
  5. // @license MIT
  6. // @match https://*.instacart.ca/*
  7. // @match https://*.instacart.com/*
  8. // @grant none
  9. // @namespace https://greasyfork.org/en/users/1428059-dexteon
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. let maxLoadMoreClicks = Infinity; // Default: Unlimited "Load More" clicks
  16. let loadMoreClickCount = 0;
  17.  
  18. // Helper function to scroll to an element
  19. function scrollToElement(element) {
  20. element.scrollIntoView({ behavior: "smooth", block: "center" });
  21. }
  22.  
  23. // Function to click the "Load More" button
  24. function autoLoadMore(callback) {
  25. const loadMoreButton = document.querySelector('.e-1sdcacc .e-1opyh2e'); // Selector for "Load More" button
  26.  
  27. if (loadMoreButton && loadMoreClickCount < maxLoadMoreClicks) {
  28. console.log("Clicking 'Load More' button...");
  29. scrollToElement(loadMoreButton);
  30. loadMoreButton.click();
  31. loadMoreClickCount++;
  32.  
  33. // Wait 3 seconds before checking again
  34. setTimeout(() => autoLoadMore(callback), 3000);
  35. } else if (loadMoreClickCount >= maxLoadMoreClicks) {
  36. console.log(`Reached max 'Load More' clicks (${maxLoadMoreClicks}).`);
  37. callback(); // Call the sorting function when done
  38. } else {
  39. console.log("No more 'Load More' button found. Proceeding to sorting...");
  40. if (callback) callback();
  41. }
  42. }
  43.  
  44. // Function to sort products by price
  45. function sortProducts() {
  46. const productContainer = document.querySelector("#store-wrapper ul"); // Update this selector if necessary
  47. if (!productContainer) {
  48. console.error("Product container not found.");
  49. return;
  50. }
  51.  
  52. const productItems = Array.from(productContainer.querySelectorAll("li")); // Adjust selector if needed
  53. if (productItems.length === 0) {
  54. console.error("No products found to sort.");
  55. return;
  56. }
  57.  
  58. const pricePattern = /[\d,.]+/;
  59.  
  60. // Extract prices and sort items
  61. const sortedItems = productItems
  62. .map((item) => {
  63. const priceElement = item.querySelector("[class*='price']"); // Adjust selector for price
  64. const priceText = priceElement?.textContent.match(pricePattern);
  65. const price = priceText ? parseFloat(priceText[0].replace(/,/g, "")) : Infinity;
  66. return { item, price };
  67. })
  68. .sort((a, b) => a.price - b.price); // Sort low to high
  69.  
  70. // Clear container and reappend sorted items
  71. productContainer.innerHTML = "";
  72. sortedItems.forEach(({ item }) => productContainer.appendChild(item));
  73.  
  74. console.log("Products sorted by price (low to high).");
  75. }
  76.  
  77. // Scroll to top of the page
  78. function scrollToTop() {
  79. console.log("Scrolling to the top of the page...");
  80. window.scrollTo({ top: 0, behavior: "smooth" });
  81. }
  82.  
  83. // Main function to execute the process
  84. function main() {
  85. console.log("Starting automated loading and sorting...");
  86. autoLoadMore(() => {
  87. sortProducts(); // Sort products after all items are loaded
  88. scrollToTop(); // Scroll to the top of the page
  89. });
  90. }
  91.  
  92. // Run the script
  93. main();
  94. })();