-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
94 lines (80 loc) · 3.5 KB
/
background.js
File metadata and controls
94 lines (80 loc) · 3.5 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
// helper function at the top
function sendAlertToPopup(message) {
chrome.runtime.sendMessage({ type: "SHOW_ALERT", text: message });
}
// main logic below
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
console.log("Action received:", message.action);
if (message.action === "apply" || message.action === "applyWithLink") {
const withLink = message.action === "applyWithLink";
try {
const data = await chrome.storage.local.get(["selectedCookieNames", "destinationHost"]);
const { selectedCookieNames, destinationHost } = data;
if (!selectedCookieNames?.length) {
sendAlertToPopup("No cookies selected previously!");
return;
}
// Get the current active tab URL (the source site)
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const sourceUrl = new URL(tab.url);
const domain = sourceUrl.hostname;
// Build the destination URL (append source path+search+hash if "applyWithLink")
const destUrl = withLink
? destinationHost.replace(/\/$/, "") + sourceUrl.pathname + sourceUrl.search + sourceUrl.hash
: destinationHost;
// Check if destination tab already open — if not, open it
const existingTab = (await chrome.tabs.query({ url: `${destinationHost}*` }))[0];
if (!existingTab) {
console.log(`🌐 Opening destination tab: ${destUrl}`);
await chrome.tabs.create({ url: destUrl });
} else {
console.log(`✅ Destination tab already open, navigating to: ${destUrl}`);
await chrome.tabs.update(existingTab.id, { url: destUrl });
}
// If lastAction was manual, merge stored manual cookie values
const { lastAction, manualCookies } = await chrome.storage.local.get(["lastAction", "manualCookies"]);
if (lastAction === "manual" && manualCookies?.length) {
for (const cookie of manualCookies) {
await chrome.cookies.set({
url: destinationHost,
name: cookie.name,
value: cookie.value,
path: "/",
});
console.log("✅ Manually set cookie:", cookie.name);
}
sendAlertToPopup(`🍪 ${manualCookies.length} manually added cookies applied!`);
return; // stop here, don't run the rest (the select-cookie flow)
}
// Fetch latest cookies from source domain
chrome.cookies.getAll({ domain }, async (cookies) => {
const selected = cookies.filter((c) => selectedCookieNames.includes(c.name));
if (!selected.length) {
sendAlertToPopup("No matching cookies found on this site!");
return;
}
const destUrl = new URL(destinationHost);
const destDomain = destUrl.hostname;
for (const cookie of selected) {
try {
await chrome.cookies.set({
url: destinationHost,
name: cookie.name,
value: cookie.value,
domain: destDomain,
path: "/",
});
console.log("✅ Set cookie:", cookie.name, "→", destinationHost);
} catch (err) {
console.error("❌ Error setting cookie:", cookie.name, err);
}
}
sendAlertToPopup(`🍪 ${selected.length} cookies applied successfully to ${destinationHost}`);
});
} catch (error) {
console.error("Error in Apply logic:", error);
sendAlertToPopup("Something went wrong while applying cookies!");
}
}
sendResponse({ status: "ok" });
});