Compare commits

..

1 Commits

Author SHA1 Message Date
eae40dd8c9 Add script 2019-07-22 23:58:30 +03:00

View File

@ -13,36 +13,44 @@
// @require https://unpkg.com/ohm-js@0.14.0/dist/ohm.min.js // @require https://unpkg.com/ohm-js@0.14.0/dist/ohm.min.js
// ==/UserScript== // ==/UserScript==
(function () { ; (function ($) {
'use strict'; 'use strict';
const price = (() => { const price = (() => {
const float = n => parseFloat(n.sourceString) const priceGrammar = ohm.grammar(`Price {
const priceG = ohm.grammar(`Price { Price = digit+ "р." digit+ "к." Price? }`) Price = integer "р." integer "к." Price?
const priceS = priceG.createSemantics().addOperation('val', { integer = digit+
Price: (rub, _2, kop, _4, _5) => float(rub) * 100 + float(kop),
})
const weightG = ohm.grammar(`Weight {
Exp = BS* AnyWeight BS* AnyWeight? BS*
AnyWeight = MulWeight | Weight
Weight = float (OneUnit | ThUnit)
OneUnit = Dot<"г"> | Dot<"мл"> | Dot<"шт"> | Dot<"см">
ThUnit = Dot<"кг"> | Dot<"л"> | Dot<"м">
Dot<R> = R "."*
MulWeight = float ("x" | "х" | "*") Weight
BS = ~AnyWeight any
float = (digit digit* "." digit) -- fl
| digit+
}`) }`)
const weightS = weightG.createSemantics().addOperation('val', { const priceVal = priceGrammar.createSemantics().addOperation('val', {
Exp: (_, weight, __, ___, _____) => weight.val(), Price: (rub, _1, kop, _2, _3) => rub.val() + kop.val() * 0.01,
Weight: (n, u) => float(n) * u.val(), integer: _ => parseInt(_.sourceString)
OneUnit: _ => 1, ThUnit: _ => 1000,
MulWeight: (d, _, w) => float(d) * w.val(),
}) })
const interpret = (g, s) => (text) => s(g.match(text)) const weightGrammar = ohm.grammar(`Weight {
return (price, weight) => (interpret(priceG, priceS)(price).val() / interpret(weightG, weightS)(weight).val() / 100 * 1000) Exp = BS AnyWeight BS AnyWeight? BS
AnyWeight = MulWeight | Weight
Weight = float (Unit1 | Unit10 | Unit1000) "."*
MulWeight = float ("x" | "х" | "*") Weight
float = digit+ ("." digit+)? -- fl
Unit1 = "г" | "мл"
Unit10 = "см"
Unit1000 = "кг" | "л" | "м" | "шт"
BS = (~AnyWeight any)*
}`)
const weightVal = weightGrammar.createSemantics().addOperation('val', {
Exp: (_1, weight, _2, _3, _4) => weight.val(),
Weight: (n, u, _1) => n.val() * u.val(),
MulWeight: (d, _, w) => d.val() * w.val(),
Unit1: _ => 0.001,
Unit10: _ => 0.01,
Unit1000: _ => 1,
float: _ => parseFloat(_.sourceString)
})
const interpret = (grammar, semantics, text) => {
const match = grammar.match(text)
if (match.failed()) console.log(`Failed to parse [${text}] with ${match.message}`)
return semantics(match).val()
}
return (price, weight) => interpret(priceGrammar, priceVal, price) / interpret(weightGrammar, weightVal, weight)
})() })()
const $ = unsafeWindow.jQuery; // somehow, when @including jquery, infinite scroll stops working.
const template = $("<a/>").css({ const template = $("<a/>").css({
'background-color': '#fbba00', 'background-color': '#fbba00',
'color': 'black', // default is #cb4f2b 'color': 'black', // default is #cb4f2b
@ -54,26 +62,29 @@
'right': '5px' 'right': '5px'
}).attr('target', '_blank') }).attr('target', '_blank')
$(document).arrive(".products_card", { existing: true }, div => { $(document).arrive(".products_card", { existing: true }, div => {
const normalizedPrice = price( try {
$(div).find('div > form > div.prices__wrapper > div > div.prices_block > div > div.price').text(), const normalizedPrice = price(
$(div).find('a.fancy_ajax').text() $(div).find('div > form > div.prices__wrapper > div > div.prices_block > div > div.price').text(),
) $(div).find('a.fancy_ajax').text()
const button = template.clone() )
const url = (R.test(/e-dostavka/, location.host) ? R.identity : R.flip)(R.replace)('e-dostavka', 'gipermall', $(div).find('a.fancy_ajax').attr('href')) const link = template.clone()
$(div).find('a.fa').replaceWith(button) $(div).find('a.fa').replaceWith(link)
button.text(normalizedPrice.toFixed(2)) link.text(normalizedPrice.toFixed(2))
GM.xmlHttpRequest({ const url = (R.test(/e-dostavka/, location.host) ? R.identity : R.flip)(R.replace)('e-dostavka', 'gipermall', $(div).find('a.fancy_ajax').attr('href'))
method: 'GET', url, onload: page => { GM.xmlHttpRequest({
try { method: 'GET', url, onload: page => {
const html = $($.parseHTML(page.responseText)) try {
const competingPrice = price( const html = $($.parseHTML(page.responseText))
html.find('div.services_wrap:nth-child(3) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)').text(), const competingPrice = price(
html.find('.template_1_columns > h1:nth-child(2)').text() html.find('div.services_wrap:nth-child(3) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)').text(),
) html.find('.template_1_columns > h1:nth-child(2)').text()
const difference = Math.abs(1 - competingPrice / normalizedPrice) * 100 )
button.text((i, _) => `${_} (${competingPrice >= normalizedPrice ? '>' : '<'}${difference.toFixed(0)}%)`).prop('href', url) const diff = (1 - competingPrice / normalizedPrice) * 100
} catch (e) { } link.text((_, text) => `${text} (${diff > 0 ? '>' : '<'}${Math.abs(diff).toFixed(0)}%)`)
} .prop('href', url)
}); } catch (e) { console.log(`Failed to process page ${url}`, e) }
}
});
} catch (e) { console.log(`Failed to process product card with ${$(div).find('a.fancy_ajax').text()}`, e) }
}); });
})(); })(unsafeWindow.jQuery); // somehow, when @including jquery, infinite scroll stops working.