2017-12-13 00:41:59 +00:00
|
|
|
$(document).ready(function () {
|
2017-12-27 23:27:08 +00:00
|
|
|
$("#importExportModal").on("shown.bs.modal", showBookmarkData);
|
2017-12-29 21:29:00 +00:00
|
|
|
$("#importExportModal").on("show.bs.modal", showImportModal);
|
2017-12-14 22:13:26 +00:00
|
|
|
$("#btnImportDialog").click(importBookmarks);
|
2017-12-29 21:29:00 +00:00
|
|
|
$("#copyExport").click(copyExport);
|
2017-12-13 00:41:59 +00:00
|
|
|
});
|
|
|
|
|
2018-01-02 00:55:44 +00:00
|
|
|
function showImportModal() {
|
2017-12-29 21:29:00 +00:00
|
|
|
$("#copyExportTxt").removeClass("text-success text-danger").addClass("text-muted").text("Copy");
|
2017-12-30 04:22:57 +00:00
|
|
|
$("#exportText").val("");
|
|
|
|
$("#importText").val("");
|
2017-12-29 21:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 23:27:08 +00:00
|
|
|
function showBookmarkData() {
|
|
|
|
var openDBRequest = window.indexedDB.open("bookmarks");
|
|
|
|
|
2018-01-02 00:55:44 +00:00
|
|
|
openDBRequest.onsuccess = function (openEvt) {
|
|
|
|
var db = openEvt.target.result;
|
2017-12-27 23:27:08 +00:00
|
|
|
|
2018-01-03 04:27:02 +00:00
|
|
|
var bookmarkList = [];
|
|
|
|
db.transaction("Groups").objectStore("Groups").openCursor().onsuccess = function (cursorEvt) {
|
|
|
|
var cursor = cursorEvt.target.result;
|
|
|
|
if (cursor) {
|
|
|
|
bookmarkList.push(cursor.value);
|
|
|
|
cursor.continue();
|
|
|
|
} else {
|
|
|
|
var data = JSON.stringify(bookmarkList, null, 4);
|
|
|
|
$("#exportText").val(data);
|
|
|
|
db.close();
|
|
|
|
}
|
2017-12-27 23:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 00:41:59 +00:00
|
|
|
function importBookmarks() {
|
|
|
|
try {
|
|
|
|
var newData = $.parseJSON($("#importText").val());
|
|
|
|
} catch (err) {
|
2018-01-02 00:55:44 +00:00
|
|
|
console.error(err);
|
2017-12-13 00:41:59 +00:00
|
|
|
window.alert("Invalid Format");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-27 23:27:08 +00:00
|
|
|
if (validateBookmarks(newData)) {
|
|
|
|
$("#btnImportDialog").prop("disabled", true);
|
2017-12-13 00:41:59 +00:00
|
|
|
setList(newData);
|
|
|
|
} else {
|
|
|
|
window.alert("Invalid Format");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setList(data) {
|
|
|
|
// empty the DB and fill it with the new data
|
2017-12-27 23:27:08 +00:00
|
|
|
var openDBRequest = window.indexedDB.open("bookmarks");
|
2017-12-13 00:41:59 +00:00
|
|
|
|
2018-01-03 04:27:02 +00:00
|
|
|
openDBRequest.onsuccess = function (openEvt) {
|
|
|
|
db = openEvt.target.result;
|
2017-12-13 00:41:59 +00:00
|
|
|
|
2017-12-27 23:27:08 +00:00
|
|
|
var groupStore = db.transaction("Groups", "readwrite").objectStore("Groups");
|
|
|
|
groupStore.clear();
|
2017-12-13 00:41:59 +00:00
|
|
|
|
2018-01-03 04:27:02 +00:00
|
|
|
for (var i = 0; i < data.length; i++) {
|
|
|
|
groupStore.add(data[i]);
|
2017-12-13 00:41:59 +00:00
|
|
|
}
|
2017-12-27 23:27:08 +00:00
|
|
|
|
|
|
|
$("#importExportModal").modal("hide");
|
|
|
|
$("#btnImportDialog").prop("disabled", false);
|
|
|
|
|
|
|
|
db.close();
|
|
|
|
loadBookmarks();
|
2017-12-13 00:41:59 +00:00
|
|
|
}
|
2017-12-27 23:27:08 +00:00
|
|
|
|
2018-01-03 04:27:02 +00:00
|
|
|
openDBRequest.onerror = function (err) {
|
|
|
|
console.error(err);
|
|
|
|
window.alert("Error importing bookmarks");
|
|
|
|
}
|
2017-12-13 00:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 23:27:08 +00:00
|
|
|
function validateBookmarks(bookmarks) {
|
|
|
|
var indexes = [];
|
|
|
|
|
2017-12-13 00:41:59 +00:00
|
|
|
if (!Array.isArray(bookmarks))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (var i = 0; i < bookmarks.length; i++) {
|
|
|
|
var item = bookmarks[i];
|
|
|
|
|
|
|
|
if (item == null || typeof item != "object")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (item.title == null || typeof item.title != "string")
|
|
|
|
return false;
|
|
|
|
|
2017-12-27 23:27:08 +00:00
|
|
|
if (item.groupIndex == null || typeof item.groupIndex != "number")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (arrayContains(indexes, item.groupIndex))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
indexes.push(item.groupIndex);
|
|
|
|
|
2017-12-13 00:41:59 +00:00
|
|
|
for (var j = 0; j < item.bookmarks.length; j++) {
|
|
|
|
var bkmk = item.bookmarks[j];
|
|
|
|
|
|
|
|
if (bkmk == null || typeof bkmk != "object")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (bkmk.name == null || typeof bkmk.name != "string")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (bkmk.address == null || typeof bkmk.address != "string")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-27 23:27:08 +00:00
|
|
|
|
|
|
|
function arrayContains(array, searchFor) {
|
2018-01-03 04:27:02 +00:00
|
|
|
for (var i = 0; i < array.length; i++) {
|
|
|
|
if (array[i] == searchFor)
|
2017-12-27 23:27:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-29 21:29:00 +00:00
|
|
|
|
|
|
|
function copyExport(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var exportBox = $("#exportText");
|
|
|
|
var copyLink = $("#copyExportTxt");
|
|
|
|
exportBox.select();
|
|
|
|
|
|
|
|
try {
|
|
|
|
var successful = document.execCommand("copy");
|
|
|
|
if (successful) {
|
|
|
|
copyLink.removeClass("text-muted").addClass("text-success").text("Copied successfully!");
|
|
|
|
} else {
|
|
|
|
copyLink.removeClass("text-muted").addClass("text-danger").text("Copy failed");
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
copyLink.removeClass("text-muted").addClass("text-danger").text("Copy failed");
|
|
|
|
}
|
|
|
|
}
|