Skip to content

Commit

Permalink
Added backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziga committed May 21, 2021
1 parent 98aeb0e commit ff96b30
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
13 changes: 12 additions & 1 deletion html/export.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,19 @@ <h3 class="mt-6 text-gray-900 text-lg font-medium">Passky</h3>
<span class="ml-3">Import</span>
</a>
</div>
<div class="w-0 flex-1 flex">
<a href="javascript:backup_passky()" class="relative -mr-px w-0 flex-1 inline-flex items-center justify-center py-4 text-sm text-gray-700 font-medium border border-transparent rounded-bl-lg hover:text-gray-500">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" stroke-width="1.5" stroke="#2c3e50" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M6 4h10l4 4v10a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2" />
<circle cx="12" cy="14" r="2" />
<polyline points="14 4 14 8 8 8 8 4" />
</svg>
<span class="ml-3">Backup</span>
</a>
</div>
<div class="-ml-px w-0 flex-1 flex">
<a href="#" class="relative w-0 flex-1 inline-flex items-center justify-center py-4 text-sm text-gray-700 font-medium border border-transparent rounded-br-lg hover:text-gray-500">
<a href="javascript:export_passky()" class="relative w-0 flex-1 inline-flex items-center justify-center py-4 text-sm text-gray-700 font-medium border border-transparent rounded-br-lg hover:text-gray-500">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" stroke-width="1.5" stroke="#2c3e50" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M14 3v4a1 1 0 0 0 1 1h4" />
Expand Down
26 changes: 26 additions & 0 deletions js/default-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,30 @@ const errors = {
"429": "You are sending too many requests! Please wait some time before executing this action.",
"505": "Something went wrong while connecting to database!",
"999": "You don't have permission to use this endpoint."
}

function downloadJson(exportJson, exportName){
let dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(exportJson);
let downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", exportName + ".json");
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}

function downloadObjectAsJson(exportObj, exportName){
let dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
let downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", exportName + ".json");
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}

function getDate(date) {
let local = new Date(date);
local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
return local.toJSON().slice(0, 10);
}
28 changes: 28 additions & 0 deletions js/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function backup_passky(){

if(sessionStorage.passwords == null || typeof(sessionStorage.passwords) == 'undefined'){
window.location.href = 'login.html';
return;
}

let passwords = JSON.parse(sessionStorage.passwords);
for(let i = 0; i < passwords.length; i++) delete passwords[i]['id'];

downloadObjectAsJson(passwords, "passky_" + getDate(new Date()));
}

function export_passky(){

if(sessionStorage.passwords == null || typeof(sessionStorage.passwords) == 'undefined'){
window.location.href = 'login.html';
return;
}

let passwords = JSON.parse(sessionStorage.passwords);
for(let i = 0; i < passwords.length; i++){
delete passwords[i]['id'];
passwords[i]['password'] = CryptoJS.AES.decrypt(passwords[i]['password'], sessionStorage.password).toString(CryptoJS.enc.Utf8);
}

downloadObjectAsJson(passwords, "passky_" + getDate(new Date()));
}

0 comments on commit ff96b30

Please sign in to comment.