0
0
mirror of https://github.com/Maks1mS/userscripts.git synced 2024-12-23 16:22:59 +03:00

[SteamPriceConverter] update to 0.6

replace prices when body updates
This commit is contained in:
Maxim Slipenko 2024-06-23 10:19:20 +03:00
parent fbe2e6e830
commit 82ea415171

View File

@ -1,7 +1,7 @@
// ==UserScript==
// @name Steam Price Converter
// @namespace https://github.com/Maks1mS/userscripts
// @version 0.5
// @version 0.6
// @description Converts prices to rubles
// @author Maxim Slipenko
// @match https://store.steampowered.com/*
@ -57,6 +57,29 @@
return SYMBOL_TO_CODE_MAPPING[state.source_symbol];
}
const observers = new WeakMap();
function addObserver(element, callback, config = { childList: true }) {
if (element && !observers.has(element)) {
const observer = new MutationObserver(callback);
observer.observe(element, config);
observers.set(element, observer);
}
}
function qs(...args) {
return document.querySelector(...args);
}
function debounce(callback, delay) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => {
callback(...args);
}, delay);
};
}
async function main() {
const rates = await getRates();
const source_valute = getCurrentValute();
@ -65,16 +88,42 @@
return;
}
await delay(75);
// await delay(75);
const convert = (n) => +(n * rates[source_valute].value).toFixed(2);
replace(convert);
const execute = () => {
replace(convert);
}
const config = { childList: true, subtree: true };
function handle(mutationsList, observer) {
observer.disconnect();
execute();
observer.observe(document.body, config);
}
const debouncedCallback = debounce(handle, 300);
addObserver(document.body, debouncedCallback, config);
window.addEventListener('popstate', execute);
window.addEventListener('load', execute);
document.addEventListener('DOMContentLoaded', execute);
if (document.readyState == "complete" ||
document.readyState == "loaded" ||
document.readyState == "interactive"
) {
execute();
}
// GM_registerMenuCommand("update", () => replace(convert), "u");
}
function replace(convert) {
let r = document.evaluate(`//text()[contains(., \"${state.source_symbol}\")]`, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
let xpath = `//text()[contains(., \"${state.source_symbol}\") and not(ancestor::*[@data-converted])]`;
let r = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < r.snapshotLength; i++) {
let n = r.snapshotItem(i);
@ -92,6 +141,7 @@
});
let newNode = document.createTextNode(newContent);
n.parentNode.setAttribute('data-converted', 'true');
n.parentNode.replaceChild(newNode, n);
console.log(newNode);
}