-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
148 lines (125 loc) · 4.44 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
// popup.js
const form = document.getElementById('control-row');
const targetInput = document.getElementById('target');
const sourceInput = document.getElementById('source');
const message = document.getElementById('message');
// The async IIFE is necessary because Chrome <89 does not support top level await.
(async function initPopupWindow() {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// 从Chrome存储中获取默认源域名
chrome.storage.sync.get(['defaultSource'], function(result) {
if (result.defaultSource) {
sourceInput.value = result.defaultSource;
} else {
sourceInput.value = 'test.com'; // 初始默认值
}
});
if (tab?.url) {
try {
let url = new URL(tab.url)
targetInput.value = url.hostname
} catch {
// ignore
}
} else {
targetInput.value = 'localhost';
}
input.focus();
})();
form.addEventListener('submit', handleFormSubmit);
async function handleFormSubmit(event) {
event.preventDefault();
clearMessage();
// 保存用户设置的源域名到Chrome存储中
chrome.storage.sync.set({defaultSource: sourceInput.value}, function() {
console.log('Default source domain saved: ' + sourceInput.value);
});
let targetUrl = stringToUrl(targetInput.value);
if (!targetUrl) {
setMessage('Invalid URL');
return;
}
let message = await copyCookie(sourceInput.value, targetInput.value);
setMessage(message);
}
function stringToUrl(input) {
// Start with treating the provided value as a URL
try {
return new URL(input);
} catch {
// ignore
}
// If that fails, try assuming the provided input is an HTTP host
try {
return new URL('http://' + input);
} catch {
// ignore
}
// If that fails ¯\_(ツ)_/¯
return null;
}
async function copyCookie(sourceDomain, targetUrl) {
let totalCookies = 0;
try {
const cookies = await chrome.cookies.getAll({ domain: sourceDomain });
if (cookies.length === 0) {
return 'No cookies found';
}
cookies.forEach(function(cookie) {
// 注意这里的 url 参数应该是一个完整的 URL 字符串
chrome.cookies.set({
url: `https://${targetUrl}`, // 这里假设你的目标域名使用 http 协议
name: cookie.name,
value: cookie.value,
domain: targetUrl, // 使用 targetUrl 作为域名
path: cookie.path,
secure: cookie.secure,
httpOnly: cookie.httpOnly,
expirationDate: cookie.expirationDate
});
chrome.cookies.set({
url: `http://${targetUrl}`, // 这里假设你的目标域名使用 http 协议
name: cookie.name,
value: cookie.value,
domain: targetUrl, // 使用 targetUrl 作为域名
path: cookie.path,
secure: cookie.secure,
httpOnly: cookie.httpOnly,
expirationDate: cookie.expirationDate
});
totalCookies++;
});
} catch (error) {
return `Unexpected error: ${error.message}`;
}
return `${totalCookies} cookies copied successfully`;
}
function deleteCookie(cookie) {
// Cookie deletion is largely modeled off of how deleting cookies works when using HTTP headers.
// Specific flags on the cookie object like `secure` or `hostOnly` are not exposed for deletion
// purposes. Instead, cookies are deleted by URL, name, and storeId. Unlike HTTP headers, though,
// we don't have to delete cookies by setting Max-Age=0; we have a method for that ;)
//
// To remove cookies set with a Secure attribute, we must provide the correct protocol in the
// details object's `url` property.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#Secure
const protocol = cookie.secure ? 'https:' : 'http:';
// Note that the final URL may not be valid. The domain value for a standard cookie is prefixed
// with a period (invalid) while cookies that are set to `cookie.hostOnly == true` do not have
// this prefix (valid).
// https://developer.chrome.com/docs/extensions/reference/cookies/#type-Cookie
const cookieUrl = `${protocol}//${cookie.domain}${cookie.path}`;
return chrome.cookies.remove({
url: cookieUrl,
name: cookie.name,
storeId: cookie.storeId
});
}
function setMessage(str) {
message.textContent = str;
message.hidden = false;
}
function clearMessage() {
message.hidden = true;
message.textContent = '';
}