-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
194 lines (164 loc) Β· 4.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { getCurrentTab } from "./tabs.js";
import { generateId } from "./utils.js";
const VARS = {
radioName: "project-path",
};
(async function main() {
loadItems();
const tab = await getCurrentTab();
const currentUrl = new URL(tab.url).origin;
const addButton = document.querySelector(".add");
const clearButton = document.querySelector(".clear");
addButton.addEventListener("click", addItem(currentUrl));
clearButton.addEventListener("click", clearItems);
const saveButton = document.querySelector(".save");
saveButton.addEventListener("click", save);
})();
function loadItems() {
chrome.storage.local.get("data", (result) => {
const items = result.data || [];
items.forEach((item) => {
const list = document.querySelector(".list");
list.appendChild(createItem(item));
});
});
}
function addItem(url) {
return () => {
const list = document.querySelector(".list");
list.appendChild(createItem({ url }));
};
}
function clearItems() {
const list = document.querySelector(".list");
list.innerHTML = "";
}
function createItem({ id, checked, url, path, alias } = {}) {
const newId = id || generateId();
const item = document.createElement("li");
item.className = "item";
item.innerHTML = /* html */ `
<label for=${newId} class="label">
<input
type="radio"
id=${newId}
name=${VARS.radioName}
${Boolean(checked) ? "checked" : ""}
/>
<input type="text" placeholder="URL" ${url ? `value="${url}"` : ""} />
<input
type="text"
placeholder="project path"
${path ? `value="${path}"` : ""}
/>
<input
type="text"
placeholder="alias (optional)"
${alias ? `value="${alias}"` : ""}
/>
</label>
<button class="copy-path" title="copy project path">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
width="16"
height="16"
>
<path fill="none" d="M0 0h24v24H0z" />
<path
d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"
/>
</svg>
</button>
<button class="remove" title="remove">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16">
<path fill="none" d="M0 0h24v24H0z"/>
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
</svg>
</button>
`;
const removeButton = item.querySelector(".remove");
removeButton.addEventListener("click", (event) => {
event.target.closest(".item").remove();
});
const radioButton = item.querySelector(`input[type=radio]`);
radioButton.addEventListener("change", () => {
const items = document.querySelectorAll(".item");
items.forEach((item) => {
const radio = item.querySelector(`input[type=radio]`);
radio.checked = radio === radioButton;
});
save();
});
const copyButton = item.querySelector(".copy-path");
copyButton.addEventListener("click", () => {
const pathInput = item.querySelector('input[placeholder="project path"]');
navigator.clipboard.writeText(pathInput.value);
pathInput.animate([{ outline: "0px" }, { outline: "#008DDA auto 1px" }], {
duration: 500,
fill: "forwards",
}).onfinish = () => {
setTimeout(() => {
pathInput.animate(
[{ outline: "#008DDA auto 1px" }, { outline: "0px" }],
{
duration: 500,
fill: "forwards",
}
);
}, 1000);
};
toast({
container: document.querySelector(".button-wrapper"),
message: "β
Copied!",
});
});
return item;
}
function save() {
const items = document.querySelectorAll(".item");
const data = Array.from(items).map((item) => {
const { id, checked } = item.querySelector("input[type=radio]");
const [url, path, alias] = item.querySelectorAll("input[type=text]");
return {
id,
checked,
url: url.value,
path: path.value,
alias: alias.value,
};
});
chrome.storage.local.set({ data });
toast({
container: document.querySelector(".button-wrapper"),
message: "β
Saved!",
});
}
function toast({ container, message, position = "before" }) {
if (document.querySelector(".toast")) {
document.querySelector(".toast").remove();
}
const toast = document.createElement("div");
toast.className = "toast";
toast.textContent = message;
if (position === "before") {
container.prepend(toast);
} else {
container.appendChild(toast);
}
const showAnimation = toast.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: 500,
fill: "forwards",
});
showAnimation.onfinish = () => {
setTimeout(() => {
const hideAnimation = toast.animate([{ opacity: 1 }, { opacity: 0 }], {
duration: 500,
fill: "forwards",
});
hideAnimation.onfinish = () => {
toast.remove();
};
}, 1000);
};
}