From 1198b24a006a0ac4ccb0296807b3d746b475c2da Mon Sep 17 00:00:00 2001 From: Trung Le Date: Mon, 9 Dec 2024 02:33:22 -0800 Subject: [PATCH] update dlvcard --- js/app.js | 23 ++++++++++++++++++ js/rt-html-abs.js | 59 ++++++++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 js/app.js diff --git a/js/app.js b/js/app.js new file mode 100644 index 0000000..d166543 --- /dev/null +++ b/js/app.js @@ -0,0 +1,23 @@ +// app.js +(function() { + 'use strict'; + + // Initialize the application when DOM is ready + document.addEventListener('DOMContentLoaded', initApp); + + function initApp() { + const downloadBtn = document.getElementById('downloadBtn'); + if (downloadBtn) { + downloadBtn.addEventListener('click', handleDownload); + } + } + + function handleDownload() { + // Check if external function exists + if (typeof window.rtlib === 'object' && typeof window.rtlib.downloadVCard === 'function') { + window.rtlib.downloadVCard(); + } else { + console.error('VCard download functionality not available'); + } + } +})(); \ No newline at end of file diff --git a/js/rt-html-abs.js b/js/rt-html-abs.js index 9280d8c..9822d22 100644 --- a/js/rt-html-abs.js +++ b/js/rt-html-abs.js @@ -276,32 +276,43 @@ function openChat(element) { App.callActionButton(json); } -// Create namespace if it doesn't exist -window.rtlib = window.rtlib || {}; +// rt-html-abs.js +(function(global) { + 'use strict'; -// Add function to namespace -rtlib.downloadVCard = function() { - const vCardContent = document.getElementById('vcardTemplate').textContent; - - const fnMatch = vCardContent.match(/FN;CHARSET=utf-8:(.*)/); - const filename = fnMatch ? fnMatch[1].trim() : 'contact'; - - const blob = new Blob([vCardContent], { - type: 'text/vcard;charset=utf-8' - }); - - const downloadUrl = URL.createObjectURL(blob); - const downloadLink = document.createElement('a'); - downloadLink.href = downloadUrl; - downloadLink.download = `${filename}.vcf`; - - document.body.appendChild(downloadLink); - downloadLink.click(); - document.body.removeChild(downloadLink); - - URL.revokeObjectURL(downloadUrl); -}; + // Create namespace + global.rtlib = global.rtlib || {}; + // Add function to namespace + global.rtlib.downloadVCard = function() { + try { + const vCardContent = document.getElementById('vcardTemplate')?.textContent; + if (!vCardContent) { + throw new Error('VCard template not found'); + } + + const fnMatch = vCardContent.match(/FN;CHARSET=utf-8:(.*)/); + const filename = fnMatch ? fnMatch[1].trim() : 'contact'; + + const blob = new Blob([vCardContent], { + type: 'text/vcard;charset=utf-8' + }); + + const downloadUrl = URL.createObjectURL(blob); + const downloadLink = document.createElement('a'); + downloadLink.href = downloadUrl; + downloadLink.download = `${filename}.vcf`; + + document.body.appendChild(downloadLink); + downloadLink.click(); + document.body.removeChild(downloadLink); + + URL.revokeObjectURL(downloadUrl); + } catch (error) { + console.error('Error downloading VCard:', error); + } + }; +})(typeof window !== 'undefined' ? window : global); document.addEventListener('DOMContentLoaded', () => { const buttons = document.querySelectorAll('.btn');