// ==UserScript== // @name 2记录账号和密码升级版 // @namespace https://bbs.tampermonkey.net.cn/ // @version 0.2.1 // @description 记录账号和密码,ctrl+x让窗口显示,支持添加多个账号密码组合 // @match *://*/* // @author xiaoxiami // @grant GM_addStyle // @license MIT // ==/UserScript== (function() { 'use strict'; // 创建主容器 const container = document.createElement('div'); container.className = 'password-manager'; container.style.display = 'none'; // 创建账号列表容器 const accountList = document.createElement('div'); accountList.className = 'account-list'; // 创建添加表单 const form = document.createElement('div'); form.className = 'add-form'; form.innerHTML = ` `; // 添加到主容器 container.appendChild(form); container.appendChild(accountList); document.body.appendChild(container); // 从localStorage获取数据 function getStoredAccounts() { try { const accounts = localStorage.getItem('stored_accounts'); return accounts ? JSON.parse(accounts) : []; } catch (e) { console.error('读取账号数据失败:', e); return []; } } // 保存数据到localStorage function saveAccounts(accounts) { try { localStorage.setItem('stored_accounts', JSON.stringify(accounts)); } catch (e) { console.error('保存账号数据失败:', e); alert('保存失败,可能是存储空间已满!'); } } // 加载保存的账号数据 function loadAccounts() { const accounts = getStoredAccounts(); accountList.innerHTML = ''; accounts.forEach((account, index) => { const accountItem = createAccountItem(account, index); accountList.appendChild(accountItem); }); } // 创建账号项 function createAccountItem(account, index) { const item = document.createElement('div'); item.className = 'account-item'; item.innerHTML = `
${account.site ? `
${account.site}
` : ''}
账号: ${account.username} 密码: ${account.password}
`; return item; } // 事件处理 form.querySelector('.add-btn').addEventListener('click', () => { const site = document.getElementById('site-input').value.trim(); const username = document.getElementById('username-input').value.trim(); const password = document.getElementById('password-input').value.trim(); if (!username || !password) { alert('账号和密码不能为空!'); return; } const accounts = getStoredAccounts(); accounts.push({ site, username, password }); saveAccounts(accounts); loadAccounts(); // 清空输入框 document.getElementById('site-input').value = ''; document.getElementById('username-input').value = ''; document.getElementById('password-input').value = ''; }); // 复制和删除功能 accountList.addEventListener('click', (e) => { if (e.target.classList.contains('copy-btn')) { const text = e.target.dataset.text; navigator.clipboard.writeText(text).then(() => { const originalText = e.target.textContent; e.target.textContent = '已复制!'; setTimeout(() => { e.target.textContent = originalText; }, 1000); }); } else if (e.target.classList.contains('delete-btn')) { const index = parseInt(e.target.dataset.index); const accounts = getStoredAccounts(); accounts.splice(index, 1); saveAccounts(accounts); loadAccounts(); } }); // 快捷键显示/隐藏 window.addEventListener('keydown', (e) => { if (e.ctrlKey && e.key.toLowerCase() === 'x') { container.style.display = container.style.display === 'none' ? 'block' : 'none'; } }); // 初始加载 loadAccounts(); })(); GM_addStyle(` .password-manager { position: fixed; top: 20px; right: 20px; width: 320px; background: #ffffff; border-radius: 12px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); padding: 16px; z-index: 9999; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; } .add-form { margin-bottom: 16px; padding-bottom: 16px; border-bottom: 1px solid #eee; } .input-field { width: 100%; padding: 8px 12px; margin-bottom: 8px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; outline: none; transition: border-color 0.2s; } .input-field:focus { border-color: #4a90e2; } .add-btn { width: 100%; padding: 8px; background: #4a90e2; color: white; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; transition: background 0.2s; } .add-btn:hover { background: #357abd; } .account-list { max-height: 400px; overflow-y: auto; } .account-item { background: #f8f9fa; border-radius: 8px; padding: 12px; margin-bottom: 8px; } .account-info { margin-bottom: 8px; } .site-url { color: #666; font-size: 12px; margin-bottom: 4px; } .credentials { display: flex; flex-direction: column; gap: 4px; font-size: 14px; } .actions { display: flex; gap: 8px; } .copy-btn, .delete-btn { padding: 6px 12px; border: none; border-radius: 4px; cursor: pointer; font-size: 12px; transition: background 0.2s; } .copy-btn { background: #e9ecef; color: #495057; } .copy-btn:hover { background: #dee2e6; } .delete-btn { background: #dc3545; color: white; } .delete-btn:hover { background: #c82333; } /* 滚动条样式 */ .account-list::-webkit-scrollbar { width: 6px; } .account-list::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 3px; } .account-list::-webkit-scrollbar-thumb { background: #888; border-radius: 3px; } .account-list::-webkit-scrollbar-thumb:hover { background: #555; } `);