Skip to content

Commit

Permalink
Merge pull request #131 from Onemind-Services-LLC/feat/copy_btn
Browse files Browse the repository at this point in the history
Add button to export keys to a file for download and a button to copy the private key
  • Loading branch information
abhi1693 authored Mar 12, 2024
2 parents 03e23c1 + 29caa12 commit e1223a4
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 408 deletions.
27 changes: 26 additions & 1 deletion netbox_secrets/project-static/src/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import type { APISecret, APIKeyPair } from './types';
function initGenerateKeyPair() {
const element = document.getElementById('new_keypair_modal') as HTMLDivElement;
const accept = document.getElementById('use_new_pubkey') as HTMLButtonElement;
const copyBtn = document.getElementById('copy_prikey') as HTMLButtonElement;
const exportBtn = document.getElementById('export_key') as HTMLButtonElement;
// If the elements are not loaded, stop.
if (element === null || accept === null) {
if (element === null || accept === null || copyBtn === null || exportBtn === null) {
return;
}
const publicElem = element.querySelector<HTMLTextAreaElement>('textarea#new_pubkey');
Expand Down Expand Up @@ -54,8 +56,31 @@ function initGenerateKeyPair() {
publicKeyField.innerText = publicElem.value;
}
}

/**
* Handles file download functionality.
*/
function handleExport() {
const content = `Public Key\n\n${publicElem?.value}\n\nPrivate Key\n\n${privateElem?.value}`;

const blob = new Blob([content], { type: 'text/plain' });

const a = document.createElement('a');
a.style.display = 'none';
a.href = window.URL.createObjectURL(blob);
a.download = 'key.txt';
document.body.appendChild(a);

a.click();

window.URL.revokeObjectURL(a.href);
document.body.removeChild(a);
}

element.addEventListener('shown.bs.modal', () => handleOpen());
accept.addEventListener('click', () => handleAccept());
copyBtn.addEventListener('click', () => navigator.clipboard.writeText(privateElem?.value || ''));
exportBtn.addEventListener('click', () => handleExport());
}

/**
Expand Down
Loading

0 comments on commit e1223a4

Please sign in to comment.