-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
62 lines (55 loc) · 2.23 KB
/
options.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
function saveOption() {
const formCount = document.querySelectorAll("input").length / 2;
let projects = [];
let sids = [];
for (let i = 0; i < formCount; i++) {
// save if project is not empty
let project = document.querySelector(`#project_${i + 1}`).value;
if (project !== "") {
projects.push(project);
sids.push(document.querySelector(`#sid_${i + 1}`).value);
}
}
chrome.storage.local.set({ projects: projects }, function () {});
chrome.storage.local.set({ sids: sids }, function () {});
alert("saved", projects, sids);
}
function loadOption() {
chrome.storage.local.get(["projects", "sids"], function (items) {
for (let i = 0; i < items.projects.length - 1; i++) {
addForm();
}
for (let i = 0; i < items.projects.length; i++) {
document.querySelector(`#project_${i + 1}`).value = items.projects[i];
document.querySelector(`#sid_${i + 1}`).value = items.sids[i] || "";
}
});
}
function addForm() {
const formCount = document.querySelectorAll("input").length / 2;
const newForm = document.createElement("div");
newForm.className = "form-row";
const newLabel = document.createElement("label");
newLabel.textContent = `Project ${formCount + 1}:`;
newForm.appendChild(newLabel);
const newFormProject = document.createElement("div");
newFormProject.setAttribute("class", "col");
const newFormProjectInput = document.createElement("input");
newFormProjectInput.type = "text";
newFormProjectInput.className = "form-control";
newFormProjectInput.id = `project_${formCount + 1}`;
newFormProject.appendChild(newFormProjectInput);
newForm.appendChild(newFormProject);
const newFormSid = document.createElement("div");
newFormSid.setAttribute("class", "col");
const newFormSidInput = document.createElement("input");
newFormSidInput.type = "text";
newFormSidInput.id = `sid_${formCount + 1}`;
newFormSidInput.className = "form-control";
newFormSid.appendChild(newFormSidInput);
newForm.appendChild(newFormSid);
document.querySelector("form").appendChild(newForm);
}
document.querySelector("#add").addEventListener("click", addForm);
document.querySelector("#save").addEventListener("click", saveOption);
document.addEventListener("DOMContentLoaded", loadOption);