Skip to content

Instantly share code, notes, and snippets.

@shtrih
Last active January 17, 2024 17:01
Show Gist options
  • Save shtrih/ff9c87707ed34bab081d436de9256e03 to your computer and use it in GitHub Desktop.
Save shtrih/ff9c87707ed34bab081d436de9256e03 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name auchan-prices.user.js
// @version 0.6
// @description Сортирует по выгоде и показывает цену за кг/л/шт! Нужно нажать ссылку слева снизу.
// @author You
// @match https://www.auchan.ru/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=auchan.ru
// @grant GM_addStyle
// @homepage https://gist.github.com/shtrih/ff9c87707ed34bab081d436de9256e03
// @supportURL https://gist.github.com/shtrih/ff9c87707ed34bab081d436de9256e03
// ==/UserScript==
// чтобы не обрезались названия товаров, т.к вес в конце
GM_addStyle('a.linkToPDP > p { display: block; }');
(function () {
'use strict';
const linkHTML = '<a id="fix-prices" href="#" style="display: block; position: fixed; bottom: 16px; left: 4px; z-index: 1000;">🔄️Обновить цены</a>';
document.body.insertAdjacentHTML('beforeend', linkHTML);
const link = document.querySelector('#fix-prices');
link.addEventListener('click', (e) => {
e.preventDefault()
const productCards = document.querySelectorAll('.productCard'),
productTitleSelector = '.linkToPDP',
productPriceSelector = '.productCardPriceData > div',
extractPriceValue = text => text.match(/^((\d+\s?)+[,]\d+)/)[0].replaceAll(' ', '').replaceAll(',', '.')
;
const productList = []; // 0 - price, 1 - node
let productContainer = null;
productCards.forEach(card => {
// Удаляем скрытые товары
if (card.classList.contains("hidden")) {
card.parentNode.remove();
return;
}
const title = card.querySelector(productTitleSelector).textContent,
weightMatches = title.match(/(\d+)\s*(г|мл)\s?$/),
weightMatchesKilo = title.match(/(\d*[,.]?\d+)\s*(кг|л)$/),
byWeight = title.match(/\sвес$/),
byQuantity = title.match(/(\d+)\s*шт$/),
price = card.querySelector(productPriceSelector)
;
if (price) {
if (byQuantity != null) {
const quantity = byQuantity[1],
pricePerItem = parseFloat(extractPriceValue(price.textContent) / quantity),
newPriceHTML = `<div>${pricePerItem.toFixed(2)} ₽ / шт</div>`
;
//console.log(quantity, pricePerItem);
price.insertAdjacentHTML('beforeend', newPriceHTML);
productList.push([pricePerItem, card.parentNode]);
} else if (byWeight != null) {
const pricePerKG = parseFloat(extractPriceValue(price.textContent));
productList.push([pricePerKG, card.parentNode]);
} else if (weightMatches != null || weightMatchesKilo != null) {
const weight = weightMatches != null ? weightMatches[1] : weightMatchesKilo[1].replace(',', '.') * 1000,
priceValue = extractPriceValue(price.textContent),
pricePerKG = (priceValue / weight * 1000),
newPriceHTML = `<div>${pricePerKG.toFixed(2)} ₽ / ${weightMatches != null ? weightMatches[2].replace('мл', 'л').replace('г', 'кг') : weightMatchesKilo[2]}</div>`
;
//console.log(weight, priceValue);
price.insertAdjacentHTML('beforeend', newPriceHTML);
productList.push([pricePerKG, card.parentNode]);
}
if (!productContainer) {
productContainer = card.parentNode.parentNode
}
}
});
productList.sort(function (a, b) {
return a[0] == b[0]
? 0
: (a[0] > b[0] ? 1 : -1);
});
for (let i = 0; i < productList.length; ++i) {
productContainer.appendChild(productList[i][1]);
}
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment