Skip to content

Instantly share code, notes, and snippets.

@alexpts
Last active September 20, 2024 06:57
Show Gist options
  • Save alexpts/347505e33250bfad673d00e67ab54336 to your computer and use it in GitHub Desktop.
Save alexpts/347505e33250bfad673d00e67ab54336 to your computer and use it in GitHub Desktop.
UserScript для отображения цен на megamarket
// ==UserScript==
// @name Отображение цены за вычетом кешбека
// @namespace http://tampermonkey.net/
// @version 2024-09-20
// @description Упращает поиск реальной нижней цены, сортирует по реальной цене и отображает ее.
// @author You
// @match https://megamarket.ru/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=megamarket.ru
// @grant none
// ==/UserScript==
(function () {
'use strict';
const calculate = () => {
const selectors = [
{
list: ".product-list-item-price, .product-offer-price, .offers-info, .cart-item-price__main-info, .catalog-item-regular-desktop__price-block, .goods-item-card__price, .order-bought-goods__list-item",
discont: ".bonus-amount",
price: ".goods-item-card__amount, .product-offer-price__amount, .catalog-item-regular-desktop__price, .amount, .sales-block-offer-price__price-final, .price, .catalog-item-regular-desktop__price, .personal-goods-item__price"
},
//{list: '.catalog-item-desktop', discont: ".bonus-amount", price: ".catalog-item-regular-desktop__price"},
//{list: '.product-list-item-price', discont: ".bonus-amount", price: ".amount"},
//{list: '.offers-info', discont: ".bonus-amount", price: ".sales-block-offer-price__price-final"},
]
for (const s of selectors) {
let nodes = document.querySelectorAll(s.list)
for (const item of nodes) {
if ('special_price' in item.dataset) {
continue;
}
let discontNode = item.querySelector(s.discont)
if (discontNode == null) {
continue; // нет в наличии
}
let discont = parseInt(discontNode.innerText.replaceAll(" ", ""))
//discont = discont + discont*0.1
let priceNode = item.querySelector(s.price)
if (priceNode == null) {
continue;
}
let price = parseInt(priceNode.innerText.trim().replaceAll(/[^0-9]/g, ""))
priceNode.innerHTML = `<small style="color: #999">${priceNode.innerHTML}</small> - <span class="real-price">${price - discont}</span>`
item.dataset.special_price = true
}
}
}
const sortNodesByRealPrice = (nodeOffers) => {
let changeOrder = false
nodeOffers.sort((a, b) => {
const priceA = parseInt(a.querySelector(".real-price").innerText)
const priceB = parseInt(b.querySelector(".real-price").innerText)
let diff = priceA - priceB
if (diff === 0) {
return 0
}
changeOrder = true
return diff
})
return changeOrder
}
const sortByRealPrice = (selectorList = ".product-offers .product-offer") => {
let offers = [...document.querySelectorAll(selectorList)]
if (sortNodesByRealPrice(offers)) {
offers.forEach(node => {
node.parentNode.appendChild(node)
})
}
}
//window.addEventListener('load', calculate)
window.setInterval(calculate, 3000)
window.setInterval(sortByRealPrice, 4000)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment