-
Notifications
You must be signed in to change notification settings - Fork 22
/
popup.js
195 lines (179 loc) · 6.28 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
195
document.addEventListener("DOMContentLoaded", function () {
const sendButton = document.getElementById("sendButton");
const receiveButton = document.getElementById("receiveButton");
const generateIdButton = document.getElementById("generateIdButton");
const saveUrlButton = document.getElementById("saveUrlButton");
const messageDiv = document.getElementById("message");
const errorMessageDiv = document.getElementById("errorMessage");
const cookieIdInput = document.getElementById("cookieId");
const customUrlInput = document.getElementById("customUrl");
sendButton.addEventListener("click", handleSendCookies);
receiveButton.addEventListener("click", handleReceiveCookies);
generateIdButton.addEventListener("click", handleGenerateId);
saveUrlButton.addEventListener("click", handleSaveUrl);
// Load the saved URL from storage
chrome.storage.sync.get(["customUrl"], (result) => {
if (result.customUrl) {
customUrlInput.value = result.customUrl;
}
});
function isValidId(id) {
return /^[a-zA-Z0-9]+$/.test(id);
}
function generateRandomId() {
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < 10; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
function showMessage(message) {
messageDiv.textContent = message;
errorMessageDiv.textContent = ""; // Clear any error messages
}
function showError(message) {
errorMessageDiv.textContent = message;
messageDiv.textContent = ""; // Clear any success messages
}
function handleSendCookies() {
const cookieId = cookieIdInput.value.trim();
const customUrl = customUrlInput.value.trim();
if (!cookieId) {
showError("Please enter a cookie ID");
return;
}
if (!isValidId(cookieId)) {
showError("Invalid ID. Only letters and numbers are allowed.");
return;
}
if (!customUrl) {
showError("Please enter a custom URL");
return;
}
sendCookies(cookieId, customUrl);
}
function handleReceiveCookies() {
const cookieId = cookieIdInput.value.trim();
const customUrl = customUrlInput.value.trim();
if (!cookieId) {
showError("Please enter a cookie ID");
return;
}
if (!isValidId(cookieId)) {
showError("Invalid ID. Only letters and numbers are allowed.");
return;
}
if (!customUrl) {
showError("Please enter a custom URL");
return;
}
receiveCookies(cookieId, customUrl);
}
function handleGenerateId() {
const randomId = generateRandomId();
cookieIdInput.value = randomId;
showMessage("Random ID generated: " + randomId);
}
function handleSaveUrl() {
const customUrl = customUrlInput.value.trim();
if (!customUrl) {
showError("Please enter a URL");
return;
}
chrome.storage.sync.set({ customUrl: customUrl }, () => {
showMessage("Custom URL saved!");
});
}
function sendCookies(cookieId, customUrl) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTab = tabs[0];
const url = new URL(currentTab.url);
chrome.cookies.getAll({ url: url.origin }, function (cookies) {
const cookieData = cookies.map(function (cookie) {
return {
name: cookie.name,
value: cookie.value,
domain: cookie.domain,
path: cookie.path,
httpOnly: cookie.httpOnly,
secure: cookie.secure,
sameSite: cookie.sameSite,
};
});
fetch(`${customUrl}/send-cookies`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: cookieId,
url: currentTab.url,
cookies: cookieData,
}),
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
showMessage("Cookies sent successfully!");
} else {
showError(data.message || "Error sending cookies");
}
})
.catch((error) => {
showError("Error sending cookies: " + error.message);
});
});
});
}
function receiveCookies(cookieId, customUrl) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTab = tabs[0];
const url = new URL(currentTab.url);
fetch(`${customUrl}/receive-cookies/${cookieId}`)
.then((response) => response.json())
.then((data) => {
if (data.success && data.cookies) {
const promises = data.cookies.map((cookie) => {
return new Promise((resolve) => {
chrome.cookies.set(
{
url: url.origin,
name: cookie.name,
value: cookie.value,
domain: cookie.domain || url.hostname,
path: cookie.path || "/",
secure: cookie.secure || false,
httpOnly: cookie.httpOnly || false,
sameSite: cookie.sameSite || "lax",
expirationDate:
cookie.expirationDate ||
Math.floor(Date.now() / 1000) + 3600,
},
(result) => {
if (chrome.runtime.lastError) {
console.error(
"Error setting cookie:",
chrome.runtime.lastError
);
}
resolve();
}
);
});
});
Promise.all(promises).then(() => {
showMessage("Cookies received and set successfully!");
chrome.tabs.reload(currentTab.id);
});
} else {
showError(data.message || "Error receiving cookies");
}
})
.catch((error) => {
showError("Error receiving cookies: " + error.message);
});
});
}
});