-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
127 lines (108 loc) · 3.99 KB
/
popup.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
function isValidJsonFormat(json) {
var obj = JSON.parse(json);
if (typeof obj !== "object" || obj === null) return false;
if (!obj.folderData || typeof obj.folderData !== "object") return false;
if (
!obj.folderData.folderActiveClasses ||
typeof obj.folderData.folderActiveClasses !== "object"
)
return false;
if (!obj.folderData.folders || typeof obj.folderData.folders !== "object")
return false;
if (!obj.classData || typeof obj.classData !== "object") return false;
for (const key in obj.folderData.folderActiveClasses) {
if (!Array.isArray(obj.folderData.folderActiveClasses[key])) return false;
}
for (const key in obj.folderData.folders) {
if (typeof obj.folderData.folders[key] !== "string") return false;
}
for (const key in obj.classData) {
if (typeof obj.classData[key] !== "string") return false;
}
return true;
}
function exportFolders() {
chrome.runtime.sendMessage({ action: "getFolderData" }, function (response) {
var exportedFolderData = response.folderData;
chrome.runtime.sendMessage({ action: "getClassData" }, function (response) {
userDefinedClassName = response.classData;
var json = JSON.stringify({
folderData: exportedFolderData,
classData: userDefinedClassName,
});
var blob = new Blob([json], { type: "application/json" });
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = url;
a.download = "BetterClassroom_ExportedFolders.json";
a.click();
URL.revokeObjectURL(url);
var settingMessage = document.getElementById("setting-message");
settingMessage.textContent =
"Folders exported successfully! It has been downloaded as BetterClassroom_ExportedFolders.json";
});
});
}
function importFolders() {
try {
var input = document.createElement("input");
input.type = "file";
input.accept = "application/json";
input.onchange = function (event) {
var file = event.target.files[0];
var reader = new FileReader();
reader.onload = function () {
if (isValidJsonFormat(reader.result)) {
var importedFolderData = JSON.parse(reader.result);
chrome.runtime.sendMessage(
{
action: "saveFolderData",
folderData: importedFolderData.folderData,
},
function (response) {
chrome.runtime.sendMessage(
{
action: "saveClassData",
classData: importedFolderData.classData,
},
function (response) {
var settingMessage =
document.getElementById("setting-message");
settingMessage.textContent =
"Folders imported successfully! Please refresh the page to apply the changes.";
}
);
}
);
} else {
var settingMessage = document.getElementById("setting-message");
settingMessage.textContent =
"Invalid JSON format. Please make sure the file is exported from BetterClassroom.";
}
};
reader.readAsText(file);
};
input.click();
} catch (err) {
var settingMessage = document.getElementById("setting-message");
settingMessage.textContent = "Error importing folders. Please try again.";
}
}
document.addEventListener("DOMContentLoaded", function () {
// Fetch the extension version from the manifest file
version = chrome.runtime.getManifest().version;
console.log("popup.js loaded");
document
.getElementById("export-folders-button")
.addEventListener("click", function () {
exportFolders();
});
document
.getElementById("import-folders-button")
.addEventListener("click", function () {
importFolders();
});
// Update the version element with the fetched version
var versionElement = document.getElementById("version");
versionElement.textContent = "Extension Version: " + version;
});