-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscripts.js
124 lines (91 loc) · 3.33 KB
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function onConvertToJson()
{
// runConverterForLocalBookmarksDotHtmlFile(function() {
// alert("Conversion complete.\nFor large HTML files, give the browser a few seconds to display the results in the text box.");
// });
runConverterSelectedFile(function() {
//alert("Conversion complete.\nFor large HTML files, give the browser a few seconds to display the results in the text box.");
});
}
/**
* Read a bookmarks.html file in the same folder.
*/
function runConverterForLocalBookmarksDotHtmlFile(callback)
{
var chromBookmarkConverter = new ChromBookmarkConverter();
// Load the contents of the Chrome bookmarks file
var name = "bookmarks.html";
$.get(name, function(fileContents)
{
// Convert the Chrome bookmarks
chromBookmarkConverter.processChromeBookmarksContent(fileContents);
console.log("bookmarks=", chromBookmarkConverter.bookmarks);
// Convert to JSON
var bookmarksJson = JSON.stringify(chromBookmarkConverter.bookmarks);
$("#output").val(bookmarksJson);
callback();
});
}
function runConverterSelectedFile(callback)
{
var chromBookmarkConverter = new ChromBookmarkConverter();
// Read content from the selected file.
var fileContents = $("#input").val();
// Convert the Chrome bookmarks
chromBookmarkConverter.processChromeBookmarksContent(fileContents);
console.log("bookmarks=", chromBookmarkConverter.bookmarks);
// Convert to JSON
var bookmarksJson = JSON.stringify(chromBookmarkConverter.bookmarks);
$("#output").val(bookmarksJson);
callback();
}
/**
* Read the contents of a selected file.
*/
function onReadSelectedFile()
{
// Referneces:
// https://ourcodeworld.com/articles/read/191/how-to-read-a-computer-file-using-javascript-in-the-browser
// https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications
var selectedFile = document.getElementById('bookmarksFile').files[0];
//console.log("selectedFile", selectedFile);
//console.log("file", selectedFile);
var timer;
var startValue = $("#input").val();
if (selectedFile)
{
// Validate file is HTML
if (selectedFile.type != "text/html")
{
alert("Select file must be a HTML file");
return;
}
timer = setInterval(function() {
var currentValue = $("#input").val();
if (currentValue != startValue)
{
// textarea has been updated.
onInputChanged();
clearInterval(timer);
}
}, 500);
var reader = new FileReader();
reader.onload = function (evt)
{
//console.log(evt);
//console.log("evt.target.result=", evt.target.result);
document.getElementById("input").value = evt.target.result;
//alert("Read complete.\nFor large HTML files, give the browser a few seconds to display the results in the text box.");
};
reader.onerror = function (evt)
{
console.error("An error ocurred reading the file",evt);
};
reader.readAsText(selectedFile, "UTF-8");
}
}
function onInputChanged()
{
$("#convertToJson").prop("disabled", false);
//alert("Ready to convert");
}