Skip to content

Instantly share code, notes, and snippets.

@M1nified
Last active October 20, 2020 13:14
Show Gist options
  • Save M1nified/c5880ed65568d255f5e49745b910aba4 to your computer and use it in GitHub Desktop.
Save M1nified/c5880ed65568d255f5e49745b910aba4 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Allegro - cena za sztukę
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://allegro.pl/listing*
// @match https://allegro.pl/oferta*
// @grant none
// @updateURL https://gist.githubusercontent.com/M1nified/c5880ed65568d255f5e49745b910aba4/raw/allegro-price-per-item.js
// ==/UserScript==
(function() {
'use strict';
const PRICE_BOX_SELECTOR = '._1svub._lf05o',
TITLE_BOX_SELECTOR = 'h2',
PRICE_SIBLING_SELECTOR = '._9c44d_1xKGX',
COUNT_REGEX = /\d+\s*[sS][zZ][tT]/,
MULTIPLY_REGEX = /\d\s*x\s*\d+\s*[sS][zZ][tT]/;
const articles = document.querySelectorAll('article');
articles.forEach(article => {
try{
const priceBox = article.querySelector(PRICE_BOX_SELECTOR);
const titleBox = article.querySelector(TITLE_BOX_SELECTOR);
if(!priceBox || !titleBox) return;
const titleText = titleBox.textContent;
const countTextMatches = titleText.match(COUNT_REGEX);
if(!countTextMatches) return;
const [countTextMatch] = countTextMatches;
const multiplyTextMatches = titleText.match(MULTIPLY_REGEX);
const multiplyCount = multiplyTextMatches ? Number.parseFloat(multiplyTextMatches[0]) : 1;
const count = Number.parseInt(countTextMatch) * multiplyCount;
const priceText = priceBox.textContent.replace(',','.');
const price = Number.parseFloat(priceText);
const priceWithShippingBox = article.querySelector(PRICE_SIBLING_SELECTOR);
const pricePerItemBox = document.createElement('div');
priceWithShippingBox.after(pricePerItemBox);
const pricePerItem = Math.round((price/count) * 1000) / 1000;
pricePerItemBox.innerHTML = `${pricePerItem} zł za sztukę (${count} sztuk)`;
}catch(e){
console.error(e);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment