// код дополнение
const getIndex = () =>{
return parseInt(window.localStorage.getItem('tgindex') ?? 1);
}
const incrementIndex = () => {
window.localStorage.setItem('tgindex', `${getIndex() + 1}`);
}
// ваш код
const TOKEN = "пример";
const CHAT_ID = "-1001806005464";
const URI_API = `https://api.telegram.org/bot ${ TOKEN }/sendMessage`;
document.getElementById('tg').addEventListener('submit', function(e) {
e.preventDefault();
// add
const index = getIndex(); // get index
incrementIndex(); // update index
//
let message = `<b>Заявка с сайта!</b>\n`;
message += `<b>Отправитель:</b> ${ e.target[0].value }\n`;
message += `<b>Почта: </b> ${ e.target[1].value }\n`;
message += `<b>Номер сообщения: </b>${index}`
axios.post(URI_API, {
chat_id: CHAT_ID,
parse_mode: 'html',
text: message
})
})
const toFloat = str => {
let val = str.replace(/[^0-9,.]/g, '');
return val.split('').reduce((str, num) => {
if ([',', '.'].includes(num)) {
if (str.indexOf(',') !== -1 || str.indexOf('.') !== -1) return str;
if (!str) str += '0';
}
return (str += num);
}, '');
};
console.log(
toFloat('0.00s1'), //0.001
toFloat('.0!20'), //0.020
toFloat('1,.20') //1,20
);
const childWindow = window.open('/training');
const onChangeTxtContent = (txt) => {
if (childWindow.closed) {
//train window closed
return;
}
const event = new CustomEvent('txt-content', {detail: {txt}});
childWindow.dispatchEvent(event);
}
window.addEventListener('txt-content', ({detail})=>{
console.log(detail?.txt) // here is txt from parent (start) page
})
function getPosts(callback) {
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://jsonplaceholder.typicode.com/posts");
xhr.addEventListener("load", () => {
const response = JSON.parse(xhr.responseText);
console.log(response);
callback(response);
})
xhr.addEventListener("error", () => {
console.log("error!");
})
xhr.send();
}