Instacart Automated Sort & Load

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Instacart Automated Sort & Load
// @description  Automatically loads all items on Instacart sales pages and sorts them by price (low to high). No user interface.
// @version      2.2
// @license      MIT
// @match        https://*.instacart.ca/*
// @match        https://*.instacart.com/*
// @grant        none
// @namespace    https://greasyfork.org/en/users/1428059-dexteon
// ==/UserScript==

(function () {
  "use strict";

  let maxLoadMoreClicks = Infinity; // Default: Unlimited "Load More" clicks
  let loadMoreClickCount = 0;

  // Helper function to scroll to an element
  function scrollToElement(element) {
    element.scrollIntoView({ behavior: "smooth", block: "center" });
  }

  // Function to click the "Load More" button
  function autoLoadMore(callback) {
    const loadMoreButton = document.querySelector('.e-1sdcacc .e-1opyh2e'); // Selector for "Load More" button

    if (loadMoreButton && loadMoreClickCount < maxLoadMoreClicks) {
      console.log("Clicking 'Load More' button...");
      scrollToElement(loadMoreButton);
      loadMoreButton.click();
      loadMoreClickCount++;

      // Wait 3 seconds before checking again
      setTimeout(() => autoLoadMore(callback), 3000);
    } else if (loadMoreClickCount >= maxLoadMoreClicks) {
      console.log(`Reached max 'Load More' clicks (${maxLoadMoreClicks}).`);
      callback(); // Call the sorting function when done
    } else {
      console.log("No more 'Load More' button found. Proceeding to sorting...");
      if (callback) callback();
    }
  }

  // Function to sort products by price
  function sortProducts() {
    const productContainer = document.querySelector("#store-wrapper ul"); // Update this selector if necessary
    if (!productContainer) {
      console.error("Product container not found.");
      return;
    }

    const productItems = Array.from(productContainer.querySelectorAll("li")); // Adjust selector if needed
    if (productItems.length === 0) {
      console.error("No products found to sort.");
      return;
    }

    const pricePattern = /[\d,.]+/;

    // Extract prices and sort items
    const sortedItems = productItems
      .map((item) => {
        const priceElement = item.querySelector("[class*='price']"); // Adjust selector for price
        const priceText = priceElement?.textContent.match(pricePattern);
        const price = priceText ? parseFloat(priceText[0].replace(/,/g, "")) : Infinity;
        return { item, price };
      })
      .sort((a, b) => a.price - b.price); // Sort low to high

    // Clear container and reappend sorted items
    productContainer.innerHTML = "";
    sortedItems.forEach(({ item }) => productContainer.appendChild(item));

    console.log("Products sorted by price (low to high).");
  }

  // Scroll to top of the page
  function scrollToTop() {
    console.log("Scrolling to the top of the page...");
    window.scrollTo({ top: 0, behavior: "smooth" });
  }

  // Main function to execute the process
  function main() {
    console.log("Starting automated loading and sorting...");
    autoLoadMore(() => {
      sortProducts(); // Sort products after all items are loaded
      scrollToTop(); // Scroll to the top of the page
    });
  }

  // Run the script
  main();
})();