248 lines
9.3 KiB
JavaScript
248 lines
9.3 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function() {
|
||
const copyButton = document.getElementById('copyCookie');
|
||
const copyAllButton = document.getElementById('copyAllCookies');
|
||
const resultDiv = document.getElementById('result');
|
||
|
||
// 复制SESSION cookie的功能
|
||
copyButton.addEventListener('click', function() {
|
||
// 获取当前标签页信息
|
||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||
const currentTab = tabs[0];
|
||
const url = new URL(currentTab.url);
|
||
|
||
// 获取域名及其父域名下的所有cookie
|
||
// 例如:如果当前网址是breeze.opd.netease.com,也会查找netease.com的cookie
|
||
const domain = url.hostname;
|
||
const domains = getDomainAndParents(domain);
|
||
|
||
// 创建一个Promise数组,每个Promise获取一个域的cookies
|
||
const cookiePromises = domains.map(domain => {
|
||
return new Promise((resolve) => {
|
||
chrome.cookies.getAll({domain: domain}, (cookies) => {
|
||
resolve(cookies || []);
|
||
});
|
||
});
|
||
});
|
||
|
||
// 合并所有域名下的cookie结果
|
||
Promise.all(cookiePromises).then(cookiesArrays => {
|
||
// 合并所有cookie数组
|
||
const allCookies = [].concat(...cookiesArrays);
|
||
|
||
// 查找名为SESSION的cookie(不区分大小写)
|
||
const sessionCookie = allCookies.find(cookie =>
|
||
cookie.name.toUpperCase() === 'SESSION');
|
||
|
||
if (sessionCookie) {
|
||
// 格式化为SESSION=xxx
|
||
const cookieText = `SESSION=${sessionCookie.value}`;
|
||
|
||
// 复制到剪贴板
|
||
navigator.clipboard.writeText(cookieText)
|
||
.then(() => {
|
||
resultDiv.textContent = '已复制: ' + cookieText;
|
||
resultDiv.className = 'success';
|
||
})
|
||
.catch(err => {
|
||
resultDiv.textContent = '复制失败: ' + err;
|
||
resultDiv.className = 'error';
|
||
});
|
||
} else {
|
||
// 尝试从document.cookie中提取,使用scripting API
|
||
chrome.scripting.executeScript({
|
||
target: {tabId: tabs[0].id},
|
||
func: function() {
|
||
// 获取所有cookie
|
||
const cookies = document.cookie.split(';');
|
||
// 查找SESSION cookie
|
||
let sessionCookie = null;
|
||
for (let i = 0; i < cookies.length; i++) {
|
||
const cookie = cookies[i].trim();
|
||
if (cookie.toUpperCase().startsWith('SESSION=')) {
|
||
sessionCookie = cookie;
|
||
break;
|
||
}
|
||
}
|
||
return sessionCookie;
|
||
}
|
||
}).then(injectionResults => {
|
||
const result = injectionResults[0];
|
||
if (result && result.result) {
|
||
navigator.clipboard.writeText(result.result)
|
||
.then(() => {
|
||
resultDiv.textContent = '已复制: ' + result.result;
|
||
resultDiv.className = 'success';
|
||
})
|
||
.catch(err => {
|
||
resultDiv.textContent = '复制失败: ' + err;
|
||
resultDiv.className = 'error';
|
||
});
|
||
} else {
|
||
// 如果仍未找到,尝试从HTTP头部中解析(仅用于演示,实际上扩展无法直接读取HTTP头)
|
||
resultDiv.textContent = '没有找到SESSION cookie,请确认网页包含此cookie';
|
||
resultDiv.className = 'error';
|
||
}
|
||
}).catch(err => {
|
||
resultDiv.textContent = '脚本执行失败: ' + err.message;
|
||
resultDiv.className = 'error';
|
||
});
|
||
}
|
||
});
|
||
});
|
||
});
|
||
|
||
// 复制HTTP头部格式的cookie值
|
||
copyAllButton.addEventListener('click', function() {
|
||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||
const currentTab = tabs[0];
|
||
const url = new URL(currentTab.url);
|
||
const exactDomain = url.hostname;
|
||
|
||
// 检查是否是网易相关域名
|
||
const isNeteaseHost = exactDomain.includes('netease.com') || exactDomain.includes('breeze');
|
||
|
||
// 获取域名及其父域名
|
||
const domains = getDomainAndParents(exactDomain);
|
||
|
||
// 如果是网易域名,加入特殊的网易域名
|
||
if (isNeteaseHost) {
|
||
domains.push('netease.com');
|
||
domains.push('.netease.com');
|
||
domains.push('gameyw.netease.com');
|
||
domains.push('opd.netease.com');
|
||
domains.push('breeze.opd.netease.com');
|
||
domains.push('breeze.gameyw.netease.com');
|
||
}
|
||
|
||
// 为每个域名创建一个Promise,获取该域名下的所有cookie
|
||
const cookiePromises = domains.map(domain => {
|
||
return new Promise((resolve) => {
|
||
chrome.cookies.getAll({domain: domain}, (cookies) => {
|
||
resolve(cookies || []);
|
||
});
|
||
});
|
||
});
|
||
|
||
// 使用注入脚本获取页面上的document.cookie
|
||
chrome.scripting.executeScript({
|
||
target: {tabId: tabs[0].id},
|
||
func: function() {
|
||
return document.cookie;
|
||
}
|
||
}).then(docCookieResult => {
|
||
const docCookies = docCookieResult[0].result ?
|
||
docCookieResult[0].result.split(';').map(cookie => cookie.trim()) : [];
|
||
|
||
// 等待所有cookie获取完成
|
||
Promise.all(cookiePromises).then(cookiesArrays => {
|
||
// 合并并去重所有cookie
|
||
const cookieMap = new Map();
|
||
|
||
// 处理Chrome API获取的cookie
|
||
cookiesArrays.forEach(cookies => {
|
||
cookies.forEach(cookie => {
|
||
cookieMap.set(cookie.name, cookie.value);
|
||
});
|
||
});
|
||
|
||
// 处理document.cookie获取的cookie
|
||
docCookies.forEach(cookieStr => {
|
||
const parts = cookieStr.split('=');
|
||
if (parts.length >= 2) {
|
||
const name = parts[0].trim();
|
||
const value = parts.slice(1).join('=');
|
||
cookieMap.set(name, value);
|
||
}
|
||
});
|
||
|
||
// 如果是网易域名,确保包含SESSION等重要cookie
|
||
const importantCookieNames = [
|
||
'SESSION', 'sensorsdata2015jssdkcross', 'Hm_lvt_', '_ga', 'COSPREAD_SESSIONID',
|
||
'identity', 'csrftoken', 'accessToken', 'refreshToken', 'sessionid'
|
||
];
|
||
|
||
// 获取所有cookie项
|
||
let cookieStrings = [];
|
||
|
||
if (isNeteaseHost) {
|
||
// 网易域名特殊处理:确保SESSION在结果的最前面
|
||
let sessionCookie = null;
|
||
let importantCookies = [];
|
||
let otherCookies = [];
|
||
|
||
// 分类处理cookie
|
||
for (const [name, value] of cookieMap.entries()) {
|
||
if (name.toUpperCase() === 'SESSION') {
|
||
sessionCookie = `${name}=${value}`;
|
||
} else if (importantCookieNames.some(key => name.includes(key))) {
|
||
importantCookies.push(`${name}=${value}`);
|
||
} else {
|
||
otherCookies.push(`${name}=${value}`);
|
||
}
|
||
}
|
||
|
||
// 按优先级组装cookie字符串
|
||
if (sessionCookie) {
|
||
cookieStrings.push(sessionCookie);
|
||
}
|
||
cookieStrings = cookieStrings.concat(importantCookies);
|
||
|
||
// 如果没有其他重要cookie,考虑添加部分普通cookie
|
||
if (cookieStrings.length < 3 && otherCookies.length > 0) {
|
||
cookieStrings = cookieStrings.concat(otherCookies.slice(0, 5));
|
||
}
|
||
} else {
|
||
// 非网易域名,使用所有cookie
|
||
cookieStrings = Array.from(cookieMap.entries()).map(([name, value]) => `${name}=${value}`);
|
||
}
|
||
|
||
// 格式化为HTTP头格式
|
||
const headerCookieFormat = cookieStrings.join('; ');
|
||
|
||
// 复制到剪贴板
|
||
navigator.clipboard.writeText(headerCookieFormat)
|
||
.then(() => {
|
||
resultDiv.textContent = '已复制Header格式Cookie';
|
||
resultDiv.className = 'success';
|
||
|
||
// 显示cookie数量
|
||
const cookieCount = cookieStrings.length;
|
||
resultDiv.textContent += ` (共${cookieCount}项)`;
|
||
|
||
// 预览
|
||
if (cookieStrings.length > 0) {
|
||
const preview = headerCookieFormat.length > 80 ?
|
||
headerCookieFormat.substring(0, 80) + '...' :
|
||
headerCookieFormat;
|
||
resultDiv.textContent += '\n预览: ' + preview;
|
||
}
|
||
})
|
||
.catch(err => {
|
||
resultDiv.textContent = '复制失败: ' + err;
|
||
resultDiv.className = 'error';
|
||
});
|
||
});
|
||
}).catch(err => {
|
||
resultDiv.textContent = '脚本执行失败: ' + err.message;
|
||
resultDiv.className = 'error';
|
||
});
|
||
});
|
||
});
|
||
|
||
// 辅助函数:获取域名及其父域名
|
||
// 例如:breeze.opd.netease.com -> ['breeze.opd.netease.com', 'opd.netease.com', 'netease.com']
|
||
function getDomainAndParents(domain) {
|
||
const parts = domain.split('.');
|
||
const domains = [];
|
||
|
||
// 添加完整域名
|
||
domains.push(domain);
|
||
|
||
// 添加父域名
|
||
for (let i = 1; i < parts.length - 1; i++) {
|
||
domains.push(parts.slice(i).join('.'));
|
||
}
|
||
|
||
return domains;
|
||
}
|
||
});
|