add cookie_grabber extension; minor fixes to TMWebDriver/SOPs/simphtml
This commit is contained in:
24
assets/cookie_grabber/background.js
Normal file
24
assets/cookie_grabber/background.js
Normal file
@@ -0,0 +1,24 @@
|
||||
// background.js - 保留原有事件 + 新增消息监听
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
console.log('Cookie Grabber installed');
|
||||
});
|
||||
|
||||
// content script 请求cookie时的处理
|
||||
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||
if (msg.type === 'getCookies' && sender.tab) {
|
||||
const url = sender.tab.url;
|
||||
// 普通cookie + partitioned cookie 双查合并
|
||||
chrome.cookies.getAll({url}, cookies => {
|
||||
console.log('[CookieGrabber] normal cookies:', cookies.map(c => c.name));
|
||||
chrome.cookies.getAll({url, partitionKey: {topLevelSite: url.match(/^https?:\/\/[^/]+/)[0]}}, pCookies => {
|
||||
console.log('[CookieGrabber] partitioned cookies:', pCookies.map(c => c.name));
|
||||
const map = {};
|
||||
cookies.forEach(c => map[c.name] = c.value);
|
||||
pCookies.forEach(c => map[c.name] = c.value);
|
||||
const str = Object.entries(map).map(([k,v]) => k + '=' + v).join('; ');
|
||||
sendResponse({cookies: str});
|
||||
});
|
||||
});
|
||||
return true; // 异步响应
|
||||
}
|
||||
});
|
||||
18
assets/cookie_grabber/content.js
Normal file
18
assets/cookie_grabber/content.js
Normal file
@@ -0,0 +1,18 @@
|
||||
// content.js - MutationObserver 监听触发元素
|
||||
const TRIGGER_ID = '__ljqcg__';
|
||||
|
||||
const obs = new MutationObserver(muts => {
|
||||
for (const m of muts) {
|
||||
for (const node of m.addedNodes) {
|
||||
if (node.nodeType === 1 && node.id === TRIGGER_ID) {
|
||||
chrome.runtime.sendMessage({type: 'getCookies'}, res => {
|
||||
if (res && res.cookies) node.textContent = res.cookies;
|
||||
else node.textContent = '__cg_error__';
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
obs.observe(document.documentElement, {childList: true, subtree: true});
|
||||
32
assets/cookie_grabber/manifest.json
Normal file
32
assets/cookie_grabber/manifest.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"manifest_version": 3,
|
||||
"name": "Cookie Grabber",
|
||||
"version": "1.0",
|
||||
"description": "获取所有 cookies",
|
||||
"permissions": [
|
||||
"cookies",
|
||||
"storage",
|
||||
"tabs",
|
||||
"activeTab"
|
||||
],
|
||||
"background": {
|
||||
"service_worker": "background.js"
|
||||
},
|
||||
"action": {
|
||||
"default_popup": "popup.html"
|
||||
},
|
||||
"host_permissions": [
|
||||
"<all_urls>"
|
||||
],
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": [
|
||||
"<all_urls>"
|
||||
],
|
||||
"js": [
|
||||
"content.js"
|
||||
],
|
||||
"run_at": "document_idle"
|
||||
}
|
||||
]
|
||||
}
|
||||
20
assets/cookie_grabber/popup.html
Normal file
20
assets/cookie_grabber/popup.html
Normal file
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>HttpOnly Cookie Grabber</title>
|
||||
<script src="popup.js" defer></script>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; padding: 10px; }
|
||||
ul { padding-left: 20px; }
|
||||
li { margin: 5px 0; }
|
||||
pre { background-color: #f8f8f8; padding: 10px; border: 1px solid #ccc; max-height: 150px; overflow-y: auto; }
|
||||
button { margin-top: 10px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Get All Cookies</h1>
|
||||
<ul id="cookiesDisplay">No available cookies</ul>
|
||||
<button id="refresh">Refresh and Copy Cookies</button>
|
||||
</body>
|
||||
</html>
|
||||
56
assets/cookie_grabber/popup.js
Normal file
56
assets/cookie_grabber/popup.js
Normal file
@@ -0,0 +1,56 @@
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// 当刷新按钮被点击时,获取当前页面的 cookies
|
||||
document.getElementById('refresh').addEventListener('click', fetchCookies);
|
||||
// 加载时显示当前页面的 cookies
|
||||
fetchCookies();
|
||||
});
|
||||
|
||||
// 从 cookies 存储中获取当前页面的 cookies
|
||||
function fetchCookies() {
|
||||
// 获取当前活动的标签页
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||
const activeTab = tabs[0]; // 获取活动标签页
|
||||
const currentUrl = activeTab.url; // 当前页面的 URL
|
||||
console.log("当前活动的 URL:", currentUrl);
|
||||
|
||||
// 双查: 普通 + partitioned
|
||||
const origin = currentUrl.match(/^https?:\/\/[^\/]+/)[0];
|
||||
chrome.cookies.getAll({ url: currentUrl }, (cookies) => {
|
||||
chrome.cookies.getAll({ url: currentUrl, partitionKey: { topLevelSite: origin } }, (pCookies) => {
|
||||
const map = {};
|
||||
cookies.forEach(c => map[c.name] = c.value);
|
||||
pCookies.forEach(c => map[c.name] = c.value);
|
||||
const allCookies = Object.entries(map);
|
||||
|
||||
const cookiesDisplay = document.getElementById('cookiesDisplay');
|
||||
cookiesDisplay.innerHTML = '';
|
||||
|
||||
if (allCookies.length === 0) {
|
||||
cookiesDisplay.innerHTML = '<li>No available cookies</li>';
|
||||
} else {
|
||||
let cookiesString = '';
|
||||
allCookies.forEach(([name, value]) => {
|
||||
const li = document.createElement('li');
|
||||
li.textContent = `${name}: ${value}`;
|
||||
cookiesDisplay.appendChild(li);
|
||||
cookiesString += `${name}=${value}; `;
|
||||
});
|
||||
console.log('cookies:', allCookies.length);
|
||||
copyCookiesToClipboard(cookiesString.trim());
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 将 cookies 复制到剪贴板
|
||||
function copyCookiesToClipboard(cookiesString) {
|
||||
// 使用 Clipboard API 复制到剪贴板
|
||||
navigator.clipboard.writeText(cookiesString)
|
||||
.then(() => {
|
||||
console.log("Cookies copied to clipboard:", cookiesString);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("Unable to copy to clipboard:", err);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user