-
Notifications
You must be signed in to change notification settings - Fork 10
/
popup.js
106 lines (89 loc) · 3.26 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
"use strict";
const tabTemplate = "tr_template";
const tabAppendElement = "tbody";
const SUCCESS_MESSAGE = "Your changes were saved successfully";
const storageKey = "sfmWhySF";
loadTabs();
function loadTabs() {
const template = document.getElementById(tabTemplate);
const elements = new Set();
chrome.storage.sync.get([storageKey], function (items) {
const rowObj = items[storageKey] || [];
for (const rowId in rowObj) {
let tab = rowObj[rowId];
const element = template.content.firstElementChild.cloneNode(true);
element.querySelector(".tabTitle").value = tab.tabTitle;
element.querySelector(".url").value = tab.url;
element.querySelector(".openInNewTab").checked =
tab.openInNewTab || false;
element
.querySelector(".delete")
.addEventListener("click", deleteTab);
elements.add(element);
}
document.querySelector(tabAppendElement).append(...elements);
updateSaveButtonState();
});
}
function addTab() {
const template = document.getElementById(tabTemplate);
const element = template.content.firstElementChild.cloneNode(true);
element.querySelector(".delete").addEventListener("click", deleteTab);
document.querySelector(tabAppendElement).append(element);
updateSaveButtonState();
clearMessage();
}
function saveTab() {
let validTabs = processTabs();
setBrowserStorage(validTabs);
}
function processTabs() {
let tabs = [];
const tabElements = document.getElementsByClassName("tab");
Array.from(tabElements).forEach(function (tab) {
let tabTitle = tab.querySelector(".tabTitle").value;
let url = tab.querySelector(".url").value;
let openInNewTab = tab.querySelector(".openInNewTab").checked;
if (tabTitle && url) {
tabs.push({ tabTitle, url, openInNewTab });
}
});
return tabs;
}
function deleteTab() {
this.closest(".tab").remove();
saveTab();
updateSaveButtonState();
}
function setBrowserStorage(tabs) {
// Save it using the Chrome extension storage API.
chrome.storage.sync.set({ sfmWhySF: tabs }, function () {
setMessage("success", SUCCESS_MESSAGE);
});
}
function setMessage(type, message) {
const messageDiv = document.querySelector("#message");
messageDiv.classList.remove("hidden");
const messageType = document.querySelector("#message-type");
messageType.classList.add(`slds-theme_${type}`);
const messageBody = document.querySelector("#message-body");
messageBody.innerText = message;
setTimeout(function () {
clearMessage();
}, 3000);
}
function clearMessage() {
const messageDiv = document.querySelector("#message");
messageDiv.classList.add("hidden");
}
function updateSaveButtonState() {
const saveButton = document.querySelector(".save");
const tabElements = document.getElementsByClassName("tab");
saveButton.disabled = tabElements.length === 0;
}
const saveButton = document.querySelector(".save");
saveButton.addEventListener("click", saveTab);
const addButton = document.querySelector(".add");
addButton.addEventListener("click", addTab);
// Initial check to set the state of the save button
updateSaveButtonState();