<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Копирование email</title>
<style>
.notification {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
background-color: #4CAF50;
color: white;
padding: 12px 24px;
border-radius: 4px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
opacity: 0;
transition: opacity 0.3s ease;
z-index: 1000;
}
.notification.show {
opacity: 1;
}
/* Стиль для email-ссылок (необязательно) */
a[href^="mailto:"] {
color: #0066cc;
text-decoration: underline;
cursor: pointer;
}
</style>
</head>
<body>
<!-- Пример email-адресов на странице -->
<p>Контакты: <a href="mailto:example@domain.com">example@domain.com</a></p>
<p>Поддержка: <a href="mailto:support@company.org">support@company.org</a></p>
<!-- Элемент уведомления -->
<div id="notification" class="notification">Email скопирован!</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const notification = document.getElementById('notification');
// Функция для копирования текста в буфер обмена
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(function() {
showNotification();
}).catch(function(err) {
console.error('Не удалось скопировать текст: ', err);
});
}
// Функция для показа уведомления
function showNotification() {
notification.classList.add('show');
setTimeout(function() {
notification.classList.remove('show');
}, 3000);
}
// Обработчик кликов по email-ссылкам
document.addEventListener('click', function(e) {
// Проверяем, был ли клик по ссылке с mailto:
if (e.target.tagName === 'A' && e.target.getAttribute('href')?.startsWith('mailto:')) {
e.preventDefault();
const email = e.target.getAttribute('href').replace('mailto:', '');
copyToClipboard(email);
}
// Дополнительно: обработка кликов по тексту, который выглядит как email
// (даже если он не в ссылке)
if (e.target.textContent.includes('@') &&
e.target.textContent.includes('.') &&
!e.target.hasAttribute('href')) {
const potentialEmail = e.target.textContent.trim();
if (isValidEmail(potentialEmail)) {
copyToClipboard(potentialEmail);
}
}
});
// Функция для проверки, похож ли текст на email
function isValidEmail(text) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(text);
}
});
</script>
</body>
</html>