Compare commits

..

1 Commits

Author SHA1 Message Date
4ad67bc8d2 Add script 2019-07-20 10:29:16 +03:00

View File

@ -13,44 +13,36 @@
// @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 priceGrammar = ohm.grammar(`Price { const float = n => parseFloat(n.sourceString)
Price = integer "р." integer "к." Price? const priceG = ohm.grammar(`Price { Price = digit+ "р." digit+ "к." Price? }`)
integer = digit+ const priceS = priceG.createSemantics().addOperation('val', {
}`) Price: (rub, _2, kop, _4, _5) => float(rub) * 100 + float(kop),
const priceVal = priceGrammar.createSemantics().addOperation('val', {
Price: (rub, _1, kop, _2, _3) => rub.val() + kop.val() * 0.01,
integer: _ => parseInt(_.sourceString)
}) })
const weightGrammar = ohm.grammar(`Weight { const weightG = ohm.grammar(`Weight {
Exp = BS AnyWeight BS AnyWeight? BS Exp = BS* AnyWeight BS* AnyWeight? BS*
AnyWeight = MulWeight | Weight AnyWeight = MulWeight | Weight
Weight = float (Unit1 | Unit10 | Unit1000) "."* Weight = float (OneUnit | ThUnit)
OneUnit = Dot<"г"> | Dot<"мл"> | Dot<"шт"> | Dot<"см">
ThUnit = Dot<"кг"> | Dot<"л"> | Dot<"м">
Dot<R> = R "."*
MulWeight = float ("x" | "х" | "*") Weight MulWeight = float ("x" | "х" | "*") Weight
float = digit+ ("." digit+)? -- fl BS = ~AnyWeight any
Unit1 = "г" | "мл" float = (digit digit* "." digit) -- fl
Unit10 = "см" | digit+
Unit1000 = "кг" | "л" | "м" | "шт"
BS = (~AnyWeight any)*
}`) }`)
const weightVal = weightGrammar.createSemantics().addOperation('val', { const weightS = weightG.createSemantics().addOperation('val', {
Exp: (_1, weight, _2, _3, _4) => weight.val(), Exp: (_, weight, __, ___, _____) => weight.val(),
Weight: (n, u, _1) => n.val() * u.val(), Weight: (n, u) => float(n) * u.val(),
MulWeight: (d, _, w) => d.val() * w.val(), OneUnit: _ => 1, ThUnit: _ => 1000,
Unit1: _ => 0.001, MulWeight: (d, _, w) => float(d) * w.val(),
Unit10: _ => 0.01,
Unit1000: _ => 1,
float: _ => parseFloat(_.sourceString)
}) })
const interpret = (grammar, semantics, text) => { const interpret = (g, s) => (text) => s(g.match(text))
const match = grammar.match(text) return (price, weight) => (interpret(priceG, priceS)(price).val() / interpret(weightG, weightS)(weight).val() / 100 * 1000)
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
@ -62,15 +54,14 @@
'right': '5px' 'right': '5px'
}).attr('target', '_blank') }).attr('target', '_blank')
$(document).arrive(".products_card", { existing: true }, div => { $(document).arrive(".products_card", { existing: true }, div => {
try {
const normalizedPrice = price( const normalizedPrice = price(
$(div).find('div > form > div.prices__wrapper > div > div.prices_block > div > div.price').text(), $(div).find('div > form > div.prices__wrapper > div > div.prices_block > div > div.price').text(),
$(div).find('a.fancy_ajax').text() $(div).find('a.fancy_ajax').text()
) )
const link = template.clone() const button = template.clone()
$(div).find('a.fa').replaceWith(link)
link.text(normalizedPrice.toFixed(2))
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 url = (R.test(/e-dostavka/, location.host) ? R.identity : R.flip)(R.replace)('e-dostavka', 'gipermall', $(div).find('a.fancy_ajax').attr('href'))
$(div).find('a.fa').replaceWith(button)
button.text(normalizedPrice.toFixed(2))
GM.xmlHttpRequest({ GM.xmlHttpRequest({
method: 'GET', url, onload: page => { method: 'GET', url, onload: page => {
try { try {
@ -79,12 +70,10 @@
html.find('div.services_wrap:nth-child(3) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)').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() html.find('.template_1_columns > h1:nth-child(2)').text()
) )
const diff = (1 - competingPrice / normalizedPrice) * 100 const difference = Math.abs(1 - competingPrice / normalizedPrice) * 100
link.text((_, text) => `${text} (${diff > 0 ? '>' : '<'}${Math.abs(diff).toFixed(0)}%)`) button.text((i, _) => `${_} (${competingPrice >= normalizedPrice ? '>' : '<'}${difference.toFixed(0)}%)`).prop('href', url)
.prop('href', url) } catch (e) { }
} 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. })();