-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts.ts
184 lines (158 loc) · 5.1 KB
/
scripts.ts
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
class CookieCache {
private cookies: Record<string, Array<chrome.cookies.Cookie>> = {};
public AddCookie(cookie: chrome.cookies.Cookie): void {
const domain = cookie.domain;
// console.log(domain);
if (!this.cookies[domain]) {
this.cookies[domain] = [];
}
this.cookies[domain].push(cookie);
}
public RemoveCookie(cookie: chrome.cookies.Cookie) {
const domain = cookie.domain;
const removed = this.cookies[cookie.domain]?.filter((x) => {
x.name !== cookie.name ||
x.domain !== cookie.domain ||
x.hostOnly !== cookie.hostOnly ||
x.path !== cookie.path ||
x.secure !== cookie.secure ||
x.httpOnly !== cookie.httpOnly ||
x.session !== cookie.session ||
x.storeId !== cookie.storeId;
});
this.cookies[domain] = removed;
if (removed === undefined || removed.length === 0) {
delete this.cookies[domain];
}
}
public GetDomains() {
return Object.keys(this.cookies).sort();
}
public GetCookie(domain: string) {
return this.cookies[domain] || [];
}
public Reset() {
this.cookies = {};
}
}
const cache = new CookieCache();
document["cache"] = cache;
function bindLogout() {
const logoutDiv = document.querySelector("#logoutDiv");
logoutDiv.innerHTML = "";
const template = document.querySelector("#deleteButton").innerHTML;
const domains = cache.GetDomains();
const mindsphereInstances = domains.map((x) => {
return x.split(/\.(.*)/)[1];
});
[...new Set(mindsphereInstances)].forEach((domain, index) => {
// console.log(domain, index);
logoutDiv.innerHTML += mustache(template, {
index,
domain,
});
});
logoutDiv.querySelectorAll("button").forEach(
(x) =>
x.hasAttribute("delete") &&
(x.onclick = () => {
const domain = x.getAttribute("data");
chrome.cookies.getAll({ domain: domain }, (cookies) => {
cookies.forEach((cookie) => {
cache.RemoveCookie(cookie);
chrome.cookies.remove({ url: `https://${cookie.domain}`, name: cookie.name });
});
bindList();
bindLogout();
});
})
);
}
function bindList() {
const list = document.querySelector("#cookieList");
list.innerHTML = "";
const template = document.querySelector("#listItem").innerHTML;
cache.GetDomains().forEach((domain, index) => {
const cookies = cache.GetCookie(domain);
const sessionCookie = cookies.filter((x) => x.name === "SESSION")[0];
const session = sessionCookie?.value || "[no session]";
const sessionShort = `${session.substr(0, 15)}...`;
const xsrftokenCookie = cookies.filter((x) => x.name === "XSRF-TOKEN")[0];
const xsrftoken = xsrftokenCookie?.value || "[no token]";
const xsrftokenShort = `${xsrftoken.substr(0, 15)}...`;
const cmd = btoa(
`set "MDSP_HOST=${domain}" & set "MDSP_SESSION=${session}" & set "MDSP_XSRF_TOKEN=${xsrftoken}"`
);
const linux = btoa(
`export MDSP_HOST="${domain}" && export MDSP_SESSION="${session}" && export MDSP_XSRF_TOKEN="${xsrftoken}"`
);
const ps = btoa(
`$Env:MDSP_HOST="${domain}"; $Env:MDSP_SESSION="${session}"; $Env:MDSP_XSRF_TOKEN="${xsrftoken}"`
);
const color = "accentBlueDark";
const sessionraw = btoa(`${session}`);
const xsrftokenraw = btoa(`${xsrftoken}`);
list.innerHTML += mustache(template, {
index,
domain,
session,
xsrftoken,
cmd,
linux,
ps,
color,
sessionraw,
xsrftokenraw,
sessionShort,
xsrftokenShort,
});
});
list.querySelectorAll("p").forEach((p) => {
if (p.hasAttribute("data")) {
p.onclick = () => (p.innerHTML = p.getAttribute("data"));
}
});
list.querySelectorAll("button").forEach(
(x) =>
(x.onclick = () => {
x.hasAttribute("data") && navigator.clipboard.writeText(atob(x.getAttribute("data")));
x.hasAttribute("row") &&
x.hasAttribute("title") &&
(document.getElementById(`status_${x.getAttribute("row")}`).innerHTML = x
.getAttribute("title")
.replace("Copy", "Copied"));
})
);
showEmpty();
return false;
}
const mustache = (template, data = {}) =>
Object.entries(data).reduce(
(res, [key, value]) => res.replace(new RegExp(`{{\\s*${key}\\s*}}`, "g"), value),
template
);
document.addEventListener("DOMContentLoaded", () => {
loadCookies();
});
function loadCookies() {
cache.Reset();
chrome.cookies.getAll({}, (cookies) => {
chrome.cookies.onChanged.addListener((info) => {
cache.RemoveCookie(info.cookie);
!info.removed && cache.AddCookie(info.cookie);
});
cookies.forEach((x) => {
x.session &&
(x.name === "SESSION" || x.name === "XSRF-TOKEN") &&
(!x.expirationDate || x.expirationDate < new Date().getTime()) &&
cache.AddCookie(x);
});
bindList();
bindLogout();
});
}
function showEmpty() {
cache.GetDomains().length !== 0
? document.querySelector(".emptyState").setAttribute("style", "display:none")
: document.querySelector(".emptyState").setAttribute("style", "display:block");
}