Skip to content

Commit

Permalink
Update rt-html-abs.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ledangtrung committed Dec 6, 2024
1 parent cad8cb3 commit 0a1320a
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions js/rt-html-abs.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,53 @@ function callSIM() {
}

function sendEmail() {
const recipient = document.querySelector('[aria-labelledby="to-label"]').textContent;
const subject = document.querySelector('[aria-labelledby="subject-label"]').textContent;
const body = document.getElementById('message').value;
const mailtoUrl = `mailto:${recipient}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;

if (navigator && navigator.app && navigator.app.loadUrl) {
// Android
navigator.app.loadUrl(mailtoUrl, { openExternal: true });
try {
// Get elements with error handling
const recipientEl = document.querySelector('[aria-labelledby="to-label"]');
const subjectEl = document.querySelector('[aria-labelledby="subject-label"]');
const bodyEl = document.getElementById('message');

if (!recipientEl || !subjectEl || !bodyEl) {
throw new Error('Required email elements not found');
}

// Validate email format
const recipient = recipientEl.textContent.trim();
if (!recipient.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)) {
throw new Error('Invalid email address');
}

// Build mailto URL with sanitized inputs
const mailtoUrl = new URL('mailto:' + recipient);
mailtoUrl.searchParams.append('subject', subjectEl.textContent.trim());
mailtoUrl.searchParams.append('body', bodyEl.value.trim());

// Handle different platforms
if ('canShare' in navigator && navigator.canShare()) {
// Modern sharing API if available
navigator.share({
url: mailtoUrl.toString()
}).catch(() => {
// Fallback to traditional mailto
openMailto(mailtoUrl.toString());
});
} else {
openMailto(mailtoUrl.toString());
}
} catch (error) {
console.error('Email error:', error);
showRTDialog(error.message);
}
}

function openMailto(url) {
if (navigator?.app?.loadUrl) {
navigator.app.loadUrl(url, { openExternal: true });
} else {
// iOS and others
window.open(mailtoUrl, '_system');
window.open(url, '_system');
}
}

// Contact Operations
async function copyContactInfo(type) {
const content = type === 'phone' ? '{phone}' : '{email}';
Expand Down

0 comments on commit 0a1320a

Please sign in to comment.