passman/js/importers/import-main.js

98 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-09-25 06:01:23 +08:00
// Importers should always start with this
if(!window['PassmanImporter']){
var PassmanImporter = {}
}
2016-10-13 02:12:28 +08:00
PassmanImporter.parseRow_ = function(row) {
2016-09-25 06:01:23 +08:00
// Strip leading quote.
row = row.trim();
2016-09-25 16:29:48 +08:00
var isQuoted = false;
2016-09-25 06:01:23 +08:00
if (row.charAt(0) == '"') {
row = row.substring(1);
2016-09-25 16:29:48 +08:00
isQuoted = true;
2016-09-25 06:01:23 +08:00
}
if (row.charAt(row.length - 2) == '"') {
row = row.substring(0, row.length - 2);
2016-09-25 16:29:48 +08:00
isQuoted = true;
2016-09-25 06:01:23 +08:00
}
// Strip trailing quote. There seems to be a character between the last quote
// and the line ending, hence 2 instead of 1.
2016-09-25 16:29:48 +08:00
if(isQuoted == true) {
row = row.split('","');
} else {
row = row.split(',');
}
2016-09-25 06:01:23 +08:00
return row;
};
2016-09-25 22:18:18 +08:00
PassmanImporter.htmlDecode = function(input){
var e = document.createElement('div');
e.innerHTML = input;
return e.childNodes[0].nodeValue;
};
2016-09-25 06:01:23 +08:00
PassmanImporter.toObject_ = function(headings, row) {
var result = {};
for (var i = 0, ii = row.length; i < ii; i++) {
headings[i] = headings[i].replace(',','_')
.toLowerCase().replace(' ','_')
.replace('(','').replace(')','')
.replace('"','');
result[headings[i]] = row[i];
}
return result;
};
PassmanImporter.join_ = function(arr, sep) {
var parts = [];
for (var i = 0, ii = arr.length; i < ii; i++) {
arr[i] && parts.push(arr[i]);
}
return parts.join(sep);
};
PassmanImporter.newCredential = function () {
var credential = {
'credential_id': null,
'guid': null,
'vault_id': null,
'label': null,
'description': null,
'created': null,
'changed': null,
'tags': [],
'email': null,
'username': null,
'password': null,
'url': null,
'favicon': null,
'renew_interval': null,
'expire_time': 0,
'delete_time': 0,
'files': [],
'custom_fields': [],
'otp': {},
'hidden': false
};
return credential;
};
2016-09-25 17:21:04 +08:00
PassmanImporter.readCsv = function( csv, hasHeadings ){
hasHeadings = (hasHeadings === undefined) ? true : hasHeadings;
2016-09-25 06:01:23 +08:00
var lines = [];
var rows = csv.split('\n');
2016-09-25 17:21:04 +08:00
if(hasHeadings) {
var headings = this.parseRow_(rows[0]);
for (var i = 1, row; row = rows[i]; i++) {
row = this.toObject_(headings, this.parseRow_(row));
lines.push(row);
}
} else {
for (var i = 1, row; row = rows[i]; i++) {
lines.push(this.parseRow_(row));
}
2016-09-25 06:01:23 +08:00
}
return lines;
2016-09-25 22:18:18 +08:00
};
PassmanImporter.readJson = function (string){
return JSON.parse(string);
2016-09-25 06:01:23 +08:00
};