Skip to content

Commit

Permalink
update dlvcard
Browse files Browse the repository at this point in the history
  • Loading branch information
ledangtrung committed Dec 9, 2024
1 parent f77e5ee commit 1198b24
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
23 changes: 23 additions & 0 deletions js/app.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
})();
59 changes: 35 additions & 24 deletions js/rt-html-abs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 1198b24

Please sign in to comment.