Skip to content

Instantly share code, notes, and snippets.

@app06
Last active September 23, 2020 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save app06/e1808405e2cff228b085175c5e09b854 to your computer and use it in GitHub Desktop.
Save app06/e1808405e2cff228b085175c5e09b854 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="javascript:(async () => {
async function getData(url, selector) {
const data = await fetch(url);
const text = await data.text();
const xml = (new window.DOMParser()).parseFromString(text, 'text/xml');
let cost = xml.querySelector(selector).innerHTML;
return cost.replace(',', '.') * 1
};
const date = new Date();
let day = date.getDate();
if (day < 10) {
day = `0${day}`;
}
let month = date.getMonth() + 1;
if (month < 10) {
month = `0${month}`;
}
const query = `${day}/${month}/${date.getFullYear()}`;
const goldUrl = `https://www.cbr.ru/scripts/xml_metall.asp?date_req1=${query}&date_req2=${query}`;
let goldCost = await getData(goldUrl, `[Code='1'] Sell`);
goldCost = goldCost * 1000;
let usdCost = await getData('https://www.cbr.ru/scripts/XML_daily.asp', `[ID='R01235'] Value`);
const cost = goldCost / usdCost;
alert(`Стоимость золота: ${cost.toFixed(2)} $/кг`);
})();">Узнать стоимость</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment