-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
184 lines (159 loc) · 5.49 KB
/
script.js
File metadata and controls
184 lines (159 loc) · 5.49 KB
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
const stack = [];
const token = document.getElementById("token");
const container = document.getElementById("container");
const header = document.getElementById("header");
const payload = document.getElementById("payload");
const copyButton = document.getElementById("copy");
const copyPayloadButton = document.getElementById("copy_payload");
const copyHeaderButton = document.getElementById("copy_header");
const clearButton = document.getElementById("clear");
const decodeButton = document.getElementById("decode");
const toggleThemeButton = document.getElementById("toggle-theme");
const dark = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="svg-icon bi bi-lightbulb" viewBox="0 0 16 16">
<path d="M2 6a6 6 0 1 1 10.174 4.31c-.203.196-.359.4-.453.619l-.762 1.769A.5.5 0 0 1 10.5 13a.5.5 0 0 1 0 1 .5.5 0 0 1 0 1l-.224.447a1 1 0 0 1-.894.553H6.618a1 1 0 0 1-.894-.553L5.5 15a.5.5 0 0 1 0-1 .5.5 0 0 1 0-1 .5.5 0 0 1-.46-.302l-.761-1.77a2 2 0 0 0-.453-.618A5.98 5.98 0 0 1 2 6m6-5a5 5 0 0 0-3.479 8.592c.263.254.514.564.676.941L5.83 12h4.342l.632-1.467c.162-.377.413-.687.676-.941A5 5 0 0 0 8 1/>"
</svg>`
const light = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="svg-icon bi bi-lightbulb" viewBox="0 0 16 16">
<path d="M2 6a6 6 0 1 1 10.174 4.31c-.203.196-.359.4-.453.619l-.762 1.769A.5.5 0 0 1 10.5 13h-5a.5.5 0 0 1-.46-.302l-.761-1.77a2 2 0 0 0-.453-.618A5.98 5.98 0 0 1 2 6m3 8.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1l-.224.447a1 1 0 0 1-.894.553H6.618a1 1 0 0 1-.894-.553L5.5 15a.5.5 0 0 1-.5-.5"
fill="yellow" stroke="#dee2e6" stroke-width="0.4"/>
</svg>`
function get_data() {
// Use default value color = 'red' and likesColor = true.
console.info("loading from storage");
chrome.storage.sync.get(
{
stack: "data",
},
function (items) {
if (Array.isArray(items.stack)) {
token.value = items.stack[0].token;
header.textContent = items.stack[0].result.header;
payload.textContent = items.stack[0].result.payload;
toggleButtons();
console.log("got data");
}
}
);
chrome.storage.sync.get('theme', (data) => {
if (data.theme) {
document.body.classList.add(data.theme);
updateSVG(data.theme);
}
});
// Toggle theme
toggleThemeButton.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
const theme = document.body.classList.contains("dark-mode") ? "dark-mode" : "light-mode";
// Save theme to storage
chrome.storage.sync.set({ theme });
updateSVG(theme);
});
}
function storeToken(stack) {
chrome.storage.sync.set(
{
stack: stack,
},
function () {
console.info("storing");
}
);
}
function decodeJWT() {
stack.push({
token: token.value,
result: {
header: (header.textContent = JSON.stringify(
parseJwt(token.value, 0),
null,
2
)),
payload: (payload.textContent = JSON.stringify(
parseJwt(token.value, 1),
null,
2
)),
},
});
storeToken(stack);
toggleButtons();
}
function toggleButtons() {
let payloadDisplay = payload.textContent.length > 0 ? "block" : "none";
let headerDisplay = header.textContent.length > 0 ? "block" : "none";
copyButton.style.display =
payloadDisplay === "block" || headerDisplay === "block" ? "block" : "none";
clearButton.style.display =
payloadDisplay === "block" || headerDisplay === "block" ? "block" : "none";
container.style.display =
payloadDisplay === "block" || headerDisplay === "block" ? "block" : "none";
}
function parseJwt(token, part) {
var body = token.split(".")[part].replace(/-/g, "+").replace(/_/g, "/");
var payload = decode(body);
return JSON.parse(payload);
}
function decode(data) {
return decodeURIComponent(
window
.atob(data)
.split("")
.map(function (c) {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
})
.join("")
);
}
function copyAll() {
navigator.clipboard.writeText(
header.textContent + "\n" + payload.textContent
);
copyButton.textContent = "Copied!";
setTimeout(function () {
copyButton.textContent = "Copy All";
}, 750);
}
function copyHeader() {
navigator.clipboard.writeText(header.textContent);
copyHeaderButton.textContent = "Copied!";
setTimeout(function () {
copyHeaderButton.textContent = "Copy";
}, 750);
}
function copyPayload() {
navigator.clipboard.writeText(payload.textContent);
copyPayloadButton.textContent = "Copied!";
setTimeout(function () {
copyPayloadButton.textContent = "Copy";
}, 750);
}
function options() {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL("options.html"));
}
}
function updateSVG(theme) {
if (theme === "dark-mode") {
toggleThemeButton.innerHTML = dark;
} else {
toggleThemeButton.innerHTML = light;
}
}
function clear() {
header.textContent = "";
payload.textContent = "";
token.value = "";
chrome.storage.sync.clear();
toggleButtons();
}
//hide buttons until we have data
copyButton.style.display = "none";
clearButton.style.display = "none";
copyButton.addEventListener("click", copyAll);
copyPayloadButton.addEventListener("click", copyPayload);
copyHeaderButton.addEventListener("click", copyHeader);
decodeButton.addEventListener("click", decodeJWT);
clearButton.addEventListener("click", clear);
document.addEventListener("DOMContentLoaded", get_data);
toggleButtons();