-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
57 lines (50 loc) · 1.85 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
chrome.storage.sync.get([
"encodePlaceholder",
"encodeDisplay",
"decodePlaceholder",
"decodeDisplay"
], (defaultValues) => {
encodeInput.placeholder = defaultValues.encodePlaceholder;
encodeResult.innerText = defaultValues.encodeDisplay;
decodeInput.placeholder = defaultValues.decodePlaceholder;
decodeResult.innerText = defaultValues.decodeDisplay;
});
var hashids =
new Hashids("jS5qAlR0kHbAeqeuVowPBnyotKuTvvlGzIzkEtz79XQGB7BJHb", 8, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
function debounce( callback, delay ) {
let timeout;
return function() {
clearTimeout( timeout );
timeout = setTimeout( callback, delay );
}
}
function encode () {
let encodePlaceholder = encodeInput.value;
let encodeDisplay = hashids.encode(parseInt(encodePlaceholder));
encodeResult.innerText = encodeDisplay;
chrome.storage.sync.set({ encodeDisplay, encodePlaceholder });
}
function decode () {
let decodePlaceholder = decodeInput.value.trim();
let decodeDisplay = hashids.decode(decodePlaceholder);
decodeResult.innerText = decodeDisplay;
chrome.storage.sync.set({ decodeDisplay, decodePlaceholder });
}
function copy (element) {
switch(element) {
case "encode":
navigator.clipboard.writeText(encodeResult.textContent);
copiedEncode.classList.add("active")
setTimeout(() => copiedEncode.classList.remove("active"), 2500)
break;
case "decode":
navigator.clipboard.writeText(decodeResult.textContent);
copiedDecode.classList.add("active")
setTimeout(() => copiedDecode.classList.remove("active"), 2500)
break;
}
}
clipboardEncode.addEventListener("click", () => copy("encode"))
clipboardDecode.addEventListener("click", () => copy("decode"))
encodeInput.addEventListener("keyup", debounce(encode, 1500))
decodeInput.addEventListener("keyup", debounce(decode, 1500))