Last active
September 23, 2020 11:23
-
-
Save app06/e1808405e2cff228b085175c5e09b854 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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