// ==UserScript==
// @name 一键获取Cookie(可拖动+右对齐展开)
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 可拖动悬浮按钮,一键获取Cookie,无多余提示,展开按钮右对齐(HttpOnly类型仍无法获取)
// @author RinTaigyoku
// @match *://*/*
// @grant GM_setClipboard
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
// 创建悬浮按钮
const btn = document.createElement('button');
btn.innerHTML = `
获取Cookie
(按钮可拖动哦!)
`;
btn.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 99999;
padding: 10px 16px;
background: #4285F4;
color: white;
border: none;
border-radius: 6px;
cursor: move;
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
user-select: none;
transition: background 0.2s;
line-height: 1.4;
text-align: center;
`;
// 拖动相关变量
let isDragging = false;
let startX, startY, initialLeft, initialTop;
// 鼠标按下:开始拖动
btn.addEventListener('mousedown', (e) => {
isDragging = true;
startX = e.clientX;
startY = e.clientY;
const rect = btn.getBoundingClientRect();
initialLeft = rect.left;
initialTop = rect.top;
btn.style.right = 'auto';
btn.style.left = initialLeft + 'px';
btn.style.top = initialTop + 'px';
e.preventDefault();
});
// 鼠标移动:拖动中
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const dx = e.clientX - startX;
const dy = e.clientY - startY;
btn.style.left = (initialLeft + dx) + 'px';
btn.style.top = (initialTop + dy) + 'px';
});
// 鼠标松开:结束拖动
document.addEventListener('mouseup', (e) => {
if (!isDragging) return;
isDragging = false;
const dx = Math.abs(e.clientX - startX);
const dy = Math.abs(e.clientY - startY);
if (dx < 5 && dy < 5) {
const cookie = document.cookie;
if (cookie) {
GM_setClipboard(cookie);
showCustomAlert(cookie);
} else {
alert('❌ 当前网站无可获取的Cookie(或Cookie为HttpOnly类型,JS无法访问)');
}
}
});
// 自定义弹窗(无多余提示,展开按钮右对齐)
function showCustomAlert(cookie) {
// 创建遮罩层
const overlay = document.createElement('div');
overlay.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 100000;
`;
// 创建弹窗容器
const modal = document.createElement('div');
modal.style.cssText = `
background: #222;
color: #fff;
padding: 20px;
border-radius: 8px;
width: 450px;
max-width: 90%;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
font-family: sans-serif;
`;
// 弹窗内容:去掉多余提示,展开按钮右对齐
modal.innerHTML = `
Cookie已复制到剪贴板! ✅
${cookie.slice(0, 100)}${cookie.length > 100 ? '...' : ''}
`;
overlay.appendChild(modal);
document.body.appendChild(overlay);
// 关闭按钮事件
document.getElementById('closeBtn').addEventListener('click', () => {
overlay.remove();
});
// 展开/收起逻辑(无多余标题)
const toggleBtn = document.getElementById('toggleBtn');
const cookieContent = document.getElementById('cookieContent');
let isExpanded = false;
const shortContent = cookie.slice(0, 100) + '...';
const fullContent = cookie;
if (cookie.length > 100) {
toggleBtn.addEventListener('click', () => {
isExpanded = !isExpanded;
cookieContent.innerHTML = isExpanded ? fullContent : shortContent;
toggleBtn.textContent = isExpanded ? '🔼 收起' : '🔽 展开查看完整';
if (isExpanded) cookieContent.scrollTop = 0;
});
}
}
// 鼠标悬停效果
btn.addEventListener('mouseenter', () => {
btn.style.background = '#3367D6';
});
btn.addEventListener('mouseleave', () => {
btn.style.background = '#4285F4';
});
// 将按钮添加到页面
document.body.appendChild(btn);
})();