-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm_bouns_date.user.js
92 lines (81 loc) · 2.87 KB
/
mm_bouns_date.user.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
// ==UserScript==
// @name Megamarket Bonus Date
// @namespace https://github.com/xob0t/MM-tools
// @version 2024-02-12
// @description Показывает дату начислений бонусов
// @author xob0t
// @match https://megamarket.ru/*
// @icon https://raw.githubusercontent.com/xob0t/MM-tools/main/media/spasibo-bonus-icon.png
// @grant none
// @run-at body
// ==/UserScript==
(function () {
"use strict";
const urlPattern = "api/mobile/v1/loyaltyService/bonus/history";
let bonusData = null;
const originalFetch = window.fetch;
window.fetch = async (...args) => {
if (args?.[0].endsWith(urlPattern)) {
try {
const response = await originalFetch(...args);
const clonedResponse = response.clone();
const data = await clonedResponse.text();
parseData(data);
return response;
} catch (error) {
console.error("Fetch error:", error);
throw error;
}
}
return originalFetch(...args);
};
function createNewElement(text) {
const p = document.createElement("p");
p.className =
"custom-date bonus-transaction-item__transaction-spasibo-debit-date bonus-transaction-item__transaction-value_finished";
p.textContent = text;
return p;
}
function parseData(data) {
const parsedData = JSON.parse(data);
bonusData = parsedData.details;
}
function appendDate() {
if (!bonusData) {
console.error("no bonus data!");
return;
}
try {
const bonusHistoryElements = Array.from(document.querySelectorAll(".bonus-transaction-item"));
for (const [index, bonusHistoryElement] of bonusHistoryElements.entries()) {
const processed = bonusHistoryElement.querySelector(".bonus-transaction-item__right-side .custom-date");
const preExistingDate = bonusHistoryElement.querySelector(
".bonus-transaction-item__right-side .bonus-transaction-item__transaction-spasibo-debit-date"
);
if (processed || preExistingDate) continue;
const date = bonusData[index].date;
const newElement = createNewElement(date);
bonusHistoryElement.querySelector(".bonus-transaction-item__right-side").append(newElement);
}
} catch (error) {
console.error("Error appending date:", error);
}
}
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === "childList") {
for (const node of mutation.addedNodes) {
if (
node?.classList?.contains("profile-loyalty-list") ||
node?.classList?.contains("bonus-transaction-item")
) {
appendDate();
}
}
}
}
});
const target = document.body;
const config = { attributes: false, childList: true, subtree: true };
observer.observe(target, config);
})();