Skip to content

Commit

Permalink
Refactored downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
robbizbal committed Nov 26, 2024
1 parent 3f427e5 commit 6c88f50
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 deletions.
37 changes: 23 additions & 14 deletions src/static/scripts/demask_downloader.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
document.getElementById('downloadBtnDemask').addEventListener('click', function() {
// Get the content of the textarea
const Text = document.getElementById('responseFieldDeanonText').value;

// Create a Blob from the textarea content
const blobText = new Blob([Text], { type: 'text/plain' });
const contents = [
{ content: document.getElementById('responseFieldDeanonText').value, name: 'yoyo-text.txt', type: 'text/plain' },
];

// Download files
async function downloadFiles(files) {
for (const file of files) {
const blob = new Blob([file.content], { type: file.type });
const url = window.URL.createObjectURL(blob);

const a = document.createElement('a');
a.href = url;
a.download = file.name;
document.body.appendChild(a);
a.click();
a.remove();

window.URL.revokeObjectURL(url);

// Create a link element
const link = document.createElement('a');
link.href = URL.createObjectURL(blobText);
link.download = 'yoyo-text.txt'; // Specify the file name

// Programmatically click the link to trigger the download
link.click();

// Clean up and revoke the object URL
URL.revokeObjectURL(link.href);
await new Promise(resolve => setTimeout(resolve, 100));
};
}

downloadFiles(contents);
});
46 changes: 23 additions & 23 deletions src/static/scripts/mask_downloader.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
document.getElementById('downloadBtn').addEventListener('click', function() {
// Get the content of the textarea
const foundEntities = document.getElementById('responseFieldEntities').value;
const newText = document.getElementById('responseFieldAnonText').value;

// Create a Blob from the textarea content
const blobEntities = new Blob([foundEntities], { type: 'application/json' });
const blobNewText = new Blob([newText], { type: 'text/plain' });

// Create a link element
const linkE = document.createElement('a');
linkE.href = URL.createObjectURL(blobEntities);
linkE.download = 'yoyo-entities.json'; // Specify the file name

const linkNT = document.createElement('a');
linkNT.href = URL.createObjectURL(blobNewText);
linkNT.download = 'yoyo-anonymizedText.txt'; // Specify the file name
const contents = [
{ content: document.getElementById('responseFieldEntities').value, name: 'yoyo-entities.json', type: 'application/json' },
{ content: document.getElementById('responseFieldAnonText').value, name: 'yoyo-anonymizedText.txt', type: 'text/plain' },
];

setTimeout(function() {
linkE.click();
URL.revokeObjectURL(linkE.href);
}, 50);
// Download files
async function downloadFiles(files) {
for (const file of files) {
const blob = new Blob([file.content], { type: file.type });
const url = window.URL.createObjectURL(blob);

const a = document.createElement('a');
a.href = url;
a.download = file.name;
document.body.appendChild(a);
a.click();
a.remove();

window.URL.revokeObjectURL(url);

await new Promise(resolve => setTimeout(resolve, 100));
};
}

setTimeout(function() {
linkNT.click();
URL.revokeObjectURL(linkNT.href)
}, 50);
downloadFiles(contents);
});

0 comments on commit 6c88f50

Please sign in to comment.