-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
134 lines (107 loc) · 3.75 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
// VARIABLES:
const resources = document.querySelector(".resources section");
const quickstart = document.querySelector(".quickstart section");
let _savedResources;
let _savedQuickstart;
// FUNCTIONS:
// Google: "js download text file" => Grepper: "javascript download file from text"
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function applyQuickstartItems(items) {
if (items && items.savedQuickstart) {
const savedQuickstart = JSON.parse(items.savedQuickstart);
_savedQuickstart = savedQuickstart;
let output = "";
for (let i = 0; i < savedQuickstart.length; i += 2) {
output += `
<h3>
<a href="${savedQuickstart[i + 1][1]}">
${savedQuickstart[i][1]}
</a>
</h3>
`
}
quickstart.innerHTML = output;
}
}
function applyResourcesItems(items) {
if (items && items.savedResources) {
const savedResources = JSON.parse(items.savedResources);
_savedResources = savedResources;
let output = "";
for (let i = 0; i < savedResources.length; i += 2) {
output += `
<h3>
<a href="${savedResources[i + 1][1]}">
${savedResources[i][1]}
</a>
</h3>
`
}
resources.innerHTML = output;
}
}
document.addEventListener('DOMContentLoaded', function (e) {
document.querySelector("#options").addEventListener("click", e => {
chrome.runtime.openOptionsPage();
})
document.querySelector("#backup").addEventListener("click", e => {
console.log("Backing up...", _savedResources);
const backup = {
savedResources: _savedResources,
savedQuickstart: _savedQuickstart
}
download("newTab.backup.json", JSON.stringify(backup, null, "\t"));
})
document.querySelector("#import").addEventListener("change", e => {
const f = new FileReader();
f.onload = function () {
const resourcesJSON = JSON.parse(f.result);
const { savedResources, savedQuickstart } = resourcesJSON;
// Resources Import
let output = "";
for (let i = 0; i < savedResources.length; i += 2) {
output += `
<h3>
<a target="_blank" href="${savedResources[i + 1][1]}">
${savedResources[i][1]}
</a>
</h3>
`
}
resources.innerHTML = output;
// Save data to storage locally, in just this browser...
chrome.storage.local.set({ "savedResources": JSON.stringify(savedResources, null, "\t") }, function () {
// Data's been saved boys and girls, go on home
console.log("Saved!");
});
// Quickstart Import
output = "";
for (let i = 0; i < savedQuickstart.length; i += 2) {
output += `
<h3>
<a target="_blank" href="${savedQuickstart[i + 1][1]}">
${savedQuickstart[i][1]}
</a>
</h3>
`
}
quickstart.innerHTML = output;
// Save data to storage locally, in just this browser...
chrome.storage.local.set({ "savedQuickstart": JSON.stringify(savedQuickstart, null, "\t") }, function () {
// Data's been saved boys and girls, go on home
console.log("Saved!");
});
}
f.readAsText(e.target.files[0]);
})
chrome.storage.local.get(["savedResources"], applyResourcesItems);
chrome.storage.local.get(["savedQuickstart"], applyQuickstartItems);
});