Skip to content

Instantly share code, notes, and snippets.

@xPomaHx
Created December 2, 2019 17:45
Show Gist options
  • Save xPomaHx/05997ac29edf991812ad4595844705e0 to your computer and use it in GitHub Desktop.
Save xPomaHx/05997ac29edf991812ad4595844705e0 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name lakomkaspb
// @description qwe
// @namespace lakomkaspb
// @version 1.1
// @include http://www.lakomkaspb.ru/catalog/*
// @require https://code.jquery.com/jquery-3.4.1.min.js
// ==/UserScript==
"use strict";
const getDataFromElemet = (el) => {
let $el = $(el);
const title = ($el.find(".prod_name").text() + $el.find(".manufacturer").text())
.replace(" ", " ")
.trim();
// if (title.indexOf("Пирог Фортуна") !== -1) debugger
const priceHtml = $el
.find(".price_product")
.eq(0)
.text()
.trim()
const priceRegexpResult = /([0-9\.,]+)\s?/gim.exec(priceHtml)
const price = priceRegexpResult ? priceRegexpResult[1] : 0
let [kg, l, g, ml] = [
/([0-9]+[\.,]?[0-9]?)\s?кг/gm,
/([0-9]+[\.,]?[0-9]?)\s?л/gm,
/([0-9]+[\.,]?[0-9]?)\s?г/gm,
/([0-9]+[\.,]?[0-9]?)\s?мл/gm
].map(regexp => regexp.exec(title));
let weightFromTitle = [
...[kg, l].filter(_ => _).map(el => +el[1]),
...[g, ml].filter(_ => _).map(el => +el[1] / 1000)
];
if (priceHtml.indexOf('р./ кг.') !== -1 || priceHtml.indexOf('р./ кг/кор.') !== -1) {
weightFromTitle = [1];
}
const countRegexpResult = /([0-9\.,]+)\s?шт /gim.exec(title);
const count = countRegexpResult ? countRegexpResult[1] : 0
return { count, weightFromTitle, price, priceHtml }
}
const perkg = function (el) {
const { count, weightFromTitle, price, priceHtml } = getDataFromElemet(el)
if ((priceHtml.indexOf('кор.') !== -1 || priceHtml.indexOf('уп.') !== -1) && count > 0) {
return weightFromTitle.length ? price / (weightFromTitle[0] * count) : 0;
}
return weightFromTitle.length ? price / weightFromTitle[0] : 0;
};
const init = (async () => {
const goods = [];
const urlsToDownload = { [window.location.pathname]: false }
do {
let nextlink = Object.entries(urlsToDownload).find(([url, isDownloaded]) => isDownloaded === false)[0]
do {
urlsToDownload[nextlink] = true;
const rawHtml = await $.ajax(nextlink);
let virtualDom = document.implementation.createHTMLDocument('virtualDom');
let jsdoc = $(rawHtml, virtualDom)
nextlink = jsdoc.find(".right_arrow_to a").attr("href");
jsdoc.find("#leftcol .box a").each((i, el) => {
const link = $(el).attr("href")
if (!urlsToDownload[link]) urlsToDownload[link] = false;
});
jsdoc.find(".list > div").each((i, el) => {
const { count, weightFromTitle, price } = getDataFromElemet(el)
const perkgValue = perkg(el);
$(el).find(".price_product").after($("<span>").html(Object.entries({ count, weightFromTitle, price, perkgValue }).map(([key, val]) => key + " " + val).join("<br/>")).css({ color: "green" }))
goods.push({
html: el,
price: perkgValue,
});
});
} while (nextlink !== undefined);
} while (Object.values(urlsToDownload).some(url => url === false))
$(".list").html("");
goods.sort((a, b) => a.price - b.price);
goods.forEach((good) => {
$(".list").append(good.html);
});
})()
! function (win) {
if (window != window.top) return
win.addEventListener("load", setTimeout.bind(null, init, 999), false);
}(typeof unsafeWindow == 'undefined' ? window : unsafeWindow);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment