Greasy Fork is available in English.
可折叠、可拖拽、滚动优化、全局通用快捷关键词复制面板
// ==UserScript==
// @name 全局快捷关键词复制·可折叠增强版
// @namespace http://tampermonkey.net/
// @version 2.0
// @description 可折叠、可拖拽、滚动优化、全局通用快捷关键词复制面板
// @match *://*/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// —————————— 自定义关键词 ——————————
const KEYWORDS = [
"俄乌战争", "伊朗", "巴以冲突", "叙利亚", "中东局势",
"俄罗斯", "乌克兰", "军事新闻", "地缘政治", "美军动态",
"北约", "朝鲜", "国际局势", "中国外交", "台海",
"日本动态", "韩国新闻", "欧盟", "非洲局势", "拉美动态"
];
// —————————— 配置 ——————————
const CONFIG = {
panelWidth: 220,
btnHeight: 32,
foldable: true,
defaultFolded: false,
scrollBarStyle: true
};
// 防止重复注入
if (document.getElementById('quick-copy-panel')) return;
// —————————— 主容器 ——————————
const root = document.createElement('div');
root.id = 'quick-copy-panel';
root.style.cssText = `
all: initial;
position: fixed;
z-index: 9999999;
left: 20px;
bottom: 120px;
width: ${CONFIG.panelWidth}px;
font-family: system-ui, sans-serif;
user-select: none;
`;
document.body.appendChild(root);
// —————————— 折叠开关栏 ——————————
const bar = document.createElement('div');
bar.style.cssText = `
background: #1677ff;
color: white;
padding: 10px 12px;
border-radius: 10px 10px 0 0;
font-size: 14px;
font-weight: 500;
display: flex;
justify-content: space-between;
align-items: center;
cursor: move;
`;
bar.innerHTML = `<span>快捷复制</span><span id="fold-btn">−</span>`;
root.appendChild(bar);
// —————————— 内容面板 ——————————
const panel = document.createElement('div');
panel.id = 'panel-body';
panel.style.cssText = `
background: #ffffff;
border-radius: 0 0 10px 10px;
padding: 10px;
box-shadow: 0 4px 16px rgba(0,0,0,0.15);
max-height: 320px;
overflow-y: auto;
${CONFIG.scrollBarStyle ? `
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 5px; }
::-webkit-scrollbar-track { background: #f5f5f5; }
` : ''}
`;
root.appendChild(panel);
// —————————— 输入框 ——————————
const input = document.createElement('input');
input.type = 'text';
input.style.cssText = `
width: 100%;
box-sizing: border-box;
padding: 8px 10px;
border: 1px solid #e0e0e0;
border-radius: 6px;
font-size: 14px;
margin-bottom: 8px;
outline: none;
`;
panel.appendChild(input);
// —————————— 复制按钮 ——————————
const copyBtn = document.createElement('button');
copyBtn.textContent = '复制当前内容';
copyBtn.style.cssText = `
width: 100%;
padding: 8px;
background: #1677ff;
color: white;
border: none;
border-radius: 6px;
font-size: 14px;
cursor: pointer;
margin-bottom: 10px;
`;
panel.appendChild(copyBtn);
// —————————— 关键词按钮列表 ——————————
const list = document.createElement('div');
list.style.cssText = `
display: flex;
flex-wrap: wrap;
gap: 6px;
`;
panel.appendChild(list);
KEYWORDS.forEach(word => {
const btn = document.createElement('button');
btn.textContent = word;
btn.style.cssText = `
padding: 6px 10px;
border: none;
border-radius: 6px;
background: #f2f2f2;
font-size: 13px;
cursor: pointer;
white-space: nowrap;
`;
btn.onclick = () => {
input.value = word;
copyText(word);
};
list.appendChild(btn);
});
// —————————— 折叠功能 ——————————
const foldBtn = document.getElementById('fold-btn');
foldBtn.style.cursor = 'pointer';
foldBtn.onclick = () => {
const isHidden = panel.style.display === 'none';
panel.style.display = isHidden ? 'block' : 'none';
foldBtn.textContent = isHidden ? '−' : '+';
bar.style.borderRadius = isHidden ? '10px' : '10px 10px 0 0';
};
// —————————— 复制功能(兼容全浏览器) ——————————
function copyText(text) {
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.cssText = 'position:fixed;left:-9999px;top:-9999px;';
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
// 轻提示
const tip = document.createElement('div');
tip.textContent = `已复制:${text}`;
tip.style.cssText = `
position: fixed;
left: 50%;
bottom: 80px;
transform: translateX(-50%);
background: rgba(0,0,0,0.7);
color: white;
padding: 6px 12px;
border-radius: 6px;
font-size: 13px;
z-index: 99999999;
white-space: nowrap;
`;
document.body.appendChild(tip);
setTimeout(() => tip.remove(), 1200);
}
copyBtn.onclick = () => copyText(input.value);
// —————————— 拖拽功能 ——————————
let isDrag = false, startX, startY, origLeft, origTop;
bar.addEventListener('mousedown', e => {
if (e.target === foldBtn) return;
isDrag = false;
startX = e.clientX;
startY = e.clientY;
origLeft = root.offsetLeft;
origTop = root.offsetTop;
const move = ev => {
const dx = ev.clientX - startX;
const dy = ev.clientY - startY;
if (Math.abs(dx) > 4 || Math.abs(dy) > 4) isDrag = true;
root.style.left = origLeft + dx + 'px';
root.style.top = origTop + dy + 'px';
};
const up = () => {
document.removeEventListener('mousemove', move);
document.removeEventListener('mouseup', up);
};
document.addEventListener('mousemove', move);
document.addEventListener('mouseup', up);
});
// 初始化默认折叠
if (CONFIG.defaultFolded) {
panel.style.display = 'none';
foldBtn.textContent = '+';
bar.style.borderRadius = '10px';
}
})();