Last active
April 4, 2025 01:16
-
-
Save shtrih/ff9c87707ed34bab081d436de9256e03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name auchan-prices.user.js | |
// @version 0.8 | |
// @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('p.styles_productCardContentPanel_name__072Y7 { 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('.styles_productCard__Qy_9h'), | |
productTitleSelector = '.styles_productCardContentPanel_name__072Y7', | |
productPriceSelector = '.styles_productCardContentPanel_price__MqlWB', | |
extractPriceValue = text => text.match(/^((\d+\s?)+[,]\d+)/)[0].replaceAll(' ', '').replaceAll(',', '.'), | |
perWeightSel = '.styles_productCardContentPanel_type__6VRIK' | |
; | |
const productList = []; // 0 - price, 1 - node | |
let productContainer = null; | |
productCards.forEach(card => { | |
// Удаляем скрытые товары | |
if (card.classList.contains("hidden")) { | |
card.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), | |
isPer100g = card.querySelector(perWeightSel).textContent === 'Цена за 100 г' | |
; | |
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]); | |
} else if (byWeight != null) { | |
let pricePerKG = parseFloat(extractPriceValue(price.textContent)); | |
if (isPer100g) { | |
pricePerKG = pricePerKG * 10; | |
} | |
price.insertAdjacentHTML('beforeend', `<div>${pricePerKG.toFixed(2)} ₽ / кг</div>`); | |
productList.push([pricePerKG, card]); | |
} else if (weightMatches != null || weightMatchesKilo != null) { | |
const weight = weightMatches != null ? weightMatches[1] : weightMatchesKilo[1].replace(',', '.') * 1000, | |
priceValue = extractPriceValue(price.textContent), | |
pricePerKG = (isPer100g ? 10 : 1) * (priceValue / weight * 1000), | |
newPriceHTML = `<div>${pricePerKG.toFixed(2)} ₽ / ${weightMatches != null ? weightMatches[2].replace('мл', 'л').replace('г', 'кг').replace('гр', 'кг') : weightMatchesKilo[2]}</div>` | |
; | |
//console.log(weight, priceValue); | |
price.insertAdjacentHTML('beforeend', newPriceHTML); | |
productList.push([pricePerKG, card]); | |
} | |
if (!productContainer) { | |
productContainer = card.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