-
Notifications
You must be signed in to change notification settings - Fork 0
/
userscript.js
106 lines (100 loc) · 3.13 KB
/
userscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// ==UserScript==
// @name olx true m2 price
// @namespace http://tampermonkey.net/
// @version 0.1
// @description jak chce mieszkanie za 3000 zl to raczej nie chce mieszkania za 3000 zl + milion zl czynszu jak cos (taka ciekawostka)
// @author makin
// @match https://www.olx.pl/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=olx.pl
// @grant none
// ==/UserScript==
const rentCategoryId = "15";
function patchPrice(rentlessPrice, rent) {
return rent == null
? "poszukaj w opisie Xddd"
: `✅ ${Math.ceil(rentlessPrice + parseFloat(rent))} zł`;
}
function patchTitle(title, rentlessPrice) {
return `${title} - ${rentlessPrice} zł`;
}
function interceptPwaLazyOffers() {
let orgFetch = window.fetch;
window.fetch = async (url, ...rest) => {
const resp = await orgFetch(url, ...rest);
const parsedUrl = new URL(url);
if (
resp.ok &&
parsedUrl.host === "www.olx.pl" &&
parsedUrl.pathname === "/api/v1/offers/" &&
parsedUrl.searchParams.get("category_id") === rentCategoryId
) {
const body = await resp.json();
const modifiedBody = {
...body,
data: body?.data?.map((offer) => ({
...offer,
title: patchTitle(
offer.title,
offer.params.find((param) => param.key === "price").value.value
),
params: offer.params.map((param) => {
if (param.key === "price") {
const rentlessPrice = param.value.value;
const rent = offer.params.find((param) => param.key === "rent")
?.value?.key;
return {
...param,
value: {
...param.value,
label: patchPrice(rentlessPrice, rent),
},
};
} else {
return param;
}
}),
})),
};
return new Response(JSON.stringify(modifiedBody), {
status: resp.status,
statusText: resp.statusText,
headers: resp.headers,
});
}
return resp;
};
}
function patchPrerenderedState() {
if (window.__PRERENDERED_STATE__ == null) {
return;
}
const prerenderedState = JSON.parse(window.__PRERENDERED_STATE__);
if (
!prerenderedState.listing.breadcrumbs.some(
(breadcrumb) => breadcrumb.label === "Wynajem"
)
) {
return;
}
prerenderedState.listing.listing.ads =
prerenderedState.listing.listing.ads.map((offer) => {
const rentlessPrice = offer.price.regularPrice?.value;
if (rentlessPrice == null) {
return offer;
}
return {
...offer,
title: patchTitle(offer.title, rentlessPrice),
price: {
...offer.price,
displayValue: patchPrice(
rentlessPrice,
offer.params.find((param) => param.key === "rent")?.normalizedValue
),
},
};
});
window.__PRERENDERED_STATE__ = JSON.stringify(prerenderedState);
}
patchPrerenderedState();
interceptPwaLazyOffers();