Skip to content

Instantly share code, notes, and snippets.

@Pourghannad
Last active November 11, 2023 09:24
Show Gist options
  • Save Pourghannad/45aba406a68549ca23820f509864d647 to your computer and use it in GitHub Desktop.
Save Pourghannad/45aba406a68549ca23820f509864d647 to your computer and use it in GitHub Desktop.
snapp-food sort resturant menu by price
let allRestaurantProduct = [];
const UUID = document.cookie.split("UUID=")[1].split(";")[0];
const vendorID =
window.location.pathname.split("/")[3].split("-").length === 1
? window.location.pathname.split("/")[3]
: window.location.pathname.split("/")[3].split("-")[2];
fetch(
`https://snappfood.ir/mobile/v2/restaurant/details/dynamic?optionalClient=WEBSITE&client=WEBSITE&deviceType=WEBSITE&appVersion=8.1.1&UDID=${UUID}&vendorCode=${vendorID}&show_party=1&fetch-static-data=1&locale=fa`,
{
headers: {
accept: "application/json, text/plain, */*",
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
"content-type": "application/x-www-form-urlencoded",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
},
referrer: `https://snappfood.ir/confectionery/menu/${vendorID}`,
referrerPolicy: "strict-origin-when-cross-origin",
body: null,
method: "GET",
mode: "cors",
credentials: "include",
}
)
.then((res) => res.json())
.then((res) => {
res.data.menus.forEach((menu) => {
if (menu.categoryId !== 1 && menu.categoryId !== 4) {
menu.products.forEach((product) => {
allRestaurantProduct.push(product);
});
}
});
})
.then(() => {
sortedProducts(true);
});
const sortedProducts = (sortType) => {
const sorted = allRestaurantProduct.sort(
sortType
? (x, y) => (x.price < y.price ? -1 : x.price > y.price ? 1 : 0)
: (x, y) => (x.price > y.price ? -1 : x.price < y.price ? 1 : 0)
);
const sortedHTML = sorted.map((product) => {
return `
<div class="sc-citwmv jQhxGx">
<section class="sc-dlfnbm ProductCard__Box-sc-1wfx2e0-0 dUormP cDIPGb">
<div cursor="pointer" class="sc-dlfnbm ProductCard__Body-sc-1wfx2e0-2 fBrhOL jnbSAm">
<div>
<h2 class="sc-hKgILt esHHju">${
product.title
}</h2><strong class="sc-hKgILt fYlAbO">${
product.description
}</strong>
</div>
<div class="ProductCard__ImgWrapper-sc-1wfx2e0-3 gWgJa-D"><img alt="${
product.title
}"
src="${
product.images &&
product.images[0] &&
product.images[0].imageThumbnailSrc
? product.images[0].imageThumbnailSrc
: ""
}"
loading="lazy" width="112" height="112"></div>
</div>
<div class="sc-dlfnbm dUormP">
<footer class="sc-dlfnbm ProductCard__Footer-sc-1wfx2e0-1 iTOHqT iOANJo">
<div class="sc-dlfnbm indexstyles__Container-sc-12ty73j-0 iTOHqT inAVCp">
<div class="sc-dlfnbm dSQnRH">
<div class="sc-dlfnbm jZmgpb">
<div class="sc-dlfnbm cwaYxy">
<div class="sc-dlfnbm hmnfCP"><span class="sc-hKgILt hxREoh">${new Intl.NumberFormat(
"fa",
{ useGrouping: true }
).format(product.price)} <span
class="sc-hKgILt cnaQBH">تومان</span></span></div>
</div>
</div>
</div>
<div class="sc-dlfnbm indexstyles__AddRemoveWrapper-sc-1f5wecv-0 LBOlG kbYkee"><button
class="sc-fFubgz kNRNFK Buttonsstyles__AddButton-sc-1ysvht6-2 ewbiVE"
dir="rtl">افزودن</button></div>
</div>
</footer>
</div>
</section>
</div>
`;
});
const sortMainGrid = document.createElement("div");
sortMainGrid.className = "menu-category-wrapper sorted-base-container";
sortMainGrid.innerHTML = `
<h3 class="menu-category-title sorted-title">
مرتب شده براساس قیمت
<b style="color: #000;font-size: 18px;cursor: pointer;">${
sortType ? "کم به زیاد" : "زیاد به کم"
}</b>
</h3>
<div class="kk-grid-menu-category-container" id="menu-category--sorted">
<div class="row" style="position: relative;"></div>
</div>`;
const mainSnappFoodListGrid = document.querySelector(
"#__next > div > main > div > section > section"
);
mainSnappFoodListGrid.prepend(sortMainGrid);
const sortedContainer = document.querySelector("#menu-category--sorted .row");
sortedContainer.innerHTML = sortedHTML.join(" ").trim();
document.querySelector(".menu-category-title.sorted-title b").onclick =
() => {
document.querySelector(".sorted-base-container").remove();
sortedProducts(!sortType);
};
};
@Pourghannad
Copy link
Author

@masihtehrani
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment