2023-09-07 20:21:44 +03:00
|
|
|
// ==UserScript==
|
|
|
|
// @name Steam Price Converter
|
|
|
|
// @namespace https://github.com/Maks1mS/userscripts
|
2024-06-24 08:48:02 +03:00
|
|
|
// @version 0.7.1
|
2023-09-07 20:22:42 +03:00
|
|
|
// @description Converts prices to rubles
|
2023-09-07 20:21:44 +03:00
|
|
|
// @author Maxim Slipenko
|
|
|
|
// @match https://store.steampowered.com/*
|
2024-06-24 08:48:02 +03:00
|
|
|
// @match https://steamcommunity.com/*
|
2023-09-07 20:21:44 +03:00
|
|
|
// @icon https://www.google.com/s2/favicons?sz=64&domain=steampowered.com
|
|
|
|
// @grant GM_xmlhttpRequest
|
|
|
|
// @grant GM_registerMenuCommand
|
|
|
|
// ==/UserScript==
|
|
|
|
|
2024-06-22 23:23:29 +03:00
|
|
|
(function () {
|
2023-09-07 20:21:44 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const SYMBOL_TO_CODE_MAPPING = {
|
|
|
|
'₸': 'KZT',
|
|
|
|
'$': 'USD',
|
|
|
|
'₴': 'UAH',
|
|
|
|
}
|
|
|
|
|
|
|
|
let state = {
|
|
|
|
source_symbol: undefined
|
|
|
|
}
|
|
|
|
|
2024-06-23 10:19:20 +03:00
|
|
|
const delay = (ms) =>
|
2024-06-22 23:29:56 +03:00
|
|
|
new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
|
2023-09-07 20:21:44 +03:00
|
|
|
async function getRates() {
|
|
|
|
const arr = await new Promise((resolve, reject) => {
|
|
|
|
GM_xmlhttpRequest({
|
|
|
|
method: 'GET',
|
|
|
|
url: 'https://www.cbr-xml-daily.ru/daily_utf8.xml',
|
2024-06-22 23:23:29 +03:00
|
|
|
onload: function (res) {
|
2023-09-07 20:21:44 +03:00
|
|
|
const valutes = res.responseXML.getElementsByTagName('Valute');
|
|
|
|
resolve([...valutes].map((valute) => {
|
|
|
|
const charCode = valute.getElementsByTagName('CharCode')[0].textContent;
|
|
|
|
const value = parseFloat(valute.getElementsByTagName('Value')[0].textContent.replace(',', '.'));
|
|
|
|
const nominal = parseFloat(valute.getElementsByTagName('Nominal')[0].textContent.replace(',', '.'));
|
|
|
|
|
|
|
|
return {
|
|
|
|
charCode,
|
|
|
|
value: +(value / nominal).toFixed(4)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return Object.fromEntries(
|
|
|
|
arr.map(obj => [obj.charCode, obj])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentValute() {
|
|
|
|
const walletText = document.getElementById('header_wallet_balance').innerText;
|
2024-05-28 13:33:22 +03:00
|
|
|
state.source_symbol = Object.keys(SYMBOL_TO_CODE_MAPPING).find(symbol => walletText.includes(symbol))
|
|
|
|
return SYMBOL_TO_CODE_MAPPING[state.source_symbol];
|
2023-09-07 20:21:44 +03:00
|
|
|
}
|
|
|
|
|
2024-06-23 10:19:20 +03:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-07 20:21:44 +03:00
|
|
|
async function main() {
|
|
|
|
const rates = await getRates();
|
|
|
|
const source_valute = getCurrentValute();
|
|
|
|
|
|
|
|
if (!source_valute) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-23 10:19:20 +03:00
|
|
|
// await delay(75);
|
2024-06-22 23:23:29 +03:00
|
|
|
|
2023-09-07 20:21:44 +03:00
|
|
|
const convert = (n) => +(n * rates[source_valute].value).toFixed(2);
|
2024-06-23 10:19:20 +03:00
|
|
|
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();
|
|
|
|
}
|
2023-09-07 20:21:44 +03:00
|
|
|
|
2024-06-22 23:29:56 +03:00
|
|
|
// GM_registerMenuCommand("update", () => replace(convert), "u");
|
2023-09-07 20:21:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function replace(convert) {
|
2024-06-23 14:46:43 +03:00
|
|
|
let xpath = `//text()[contains(., \"${state.source_symbol}\") and not(ancestor::*[@data-converted]) and not(ancestor::script)]`;
|
2024-06-23 10:19:20 +03:00
|
|
|
let r = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
|
2023-09-07 20:21:44 +03:00
|
|
|
|
|
|
|
for (let i = 0; i < r.snapshotLength; i++) {
|
|
|
|
let n = r.snapshotItem(i);
|
2024-06-22 23:55:35 +03:00
|
|
|
let textContent = n.textContent;
|
2024-06-23 14:46:43 +03:00
|
|
|
let regex = new RegExp(`(\\${state.source_symbol}\\s*[0-9\\s]+[.,]?[0-9]*(?:USD)?|[0-9\\s]*[.,]?[0-9]+\\s*\\${state.source_symbol})`, 'g');
|
2024-06-23 10:19:20 +03:00
|
|
|
|
2024-06-22 23:55:35 +03:00
|
|
|
let newContent = textContent.replace(regex, (match) => {
|
|
|
|
let value;
|
2024-06-23 14:46:43 +03:00
|
|
|
let originalValue;
|
|
|
|
originalValue = match.replace(state.source_symbol, '').replace(' ', '').replace(',', '.').trim();
|
|
|
|
value = parseFloat(originalValue);
|
|
|
|
|
|
|
|
let convertedValue = convert(value);
|
|
|
|
let formattedConvertedValue;
|
|
|
|
let formattedOriginalValue;
|
|
|
|
|
|
|
|
if (match.trim().startsWith(state.source_symbol)) {
|
|
|
|
formattedOriginalValue = `${state.source_symbol}${originalValue}`;
|
2024-06-22 23:55:35 +03:00
|
|
|
} else {
|
2024-06-23 14:46:43 +03:00
|
|
|
formattedOriginalValue = `${originalValue}${state.source_symbol}`;
|
2024-06-22 23:55:35 +03:00
|
|
|
}
|
2024-06-23 14:46:43 +03:00
|
|
|
|
|
|
|
formattedConvertedValue = `${convertedValue}₽`;
|
|
|
|
|
|
|
|
return `${formattedConvertedValue} / ${formattedOriginalValue}`;
|
2024-06-22 23:55:35 +03:00
|
|
|
});
|
2024-06-23 10:19:20 +03:00
|
|
|
|
2024-06-22 23:55:35 +03:00
|
|
|
let newNode = document.createTextNode(newContent);
|
2024-06-23 10:19:20 +03:00
|
|
|
n.parentNode.setAttribute('data-converted', 'true');
|
2024-06-22 23:55:35 +03:00
|
|
|
n.parentNode.replaceChild(newNode, n);
|
2024-06-23 14:46:43 +03:00
|
|
|
// console.log(newNode);
|
2023-09-07 20:21:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 14:46:43 +03:00
|
|
|
|
2023-09-07 20:21:44 +03:00
|
|
|
main();
|
|
|
|
})();
|