// ==UserScript== // @name 称谓计算器 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 计算亲戚关系称谓的工具 // @author Your name // @match *://*/* // @grant none // @run-at document-end // ==/UserScript== (function() { 'use strict'; console.log('脚本开始执行,当前状态:', document.readyState); // API Key管理 let apiKey = localStorage.getItem('deepseek_api_key') || ''; // 显示加密后的API Key function maskApiKey(key) { if (!key) return ''; return key.substring(0, 5) + '****' + key.substring(key.length - 4); } // 关系数据结构 const relationData = { father: { name: '父亲', relations: { father: '爷爷', mother: '奶奶', brother: '伯父/叔叔', sister: '姑姑', son: '哥哥/弟弟', daughter: '姐姐/妹妹' } }, mother: { name: '母亲', relations: { father: '外公', mother: '外婆', brother: '舅舅', sister: '姨妈', son: '哥哥/弟弟', daughter: '姐姐/妹妹' } }, son: { name: '儿子', relations: { spouse: '儿媳', son: '孙子', daughter: '孙女' } }, daughter: { name: '女儿', relations: { spouse: '女婿', son: '外孙', daughter: '外孙女' } }, brother: { name: '兄弟', relations: { son: '侄子', daughter: '侄女', spouse: '嫂子' } }, sister: { name: '姐妹', relations: { son: '外甥', daughter: '外甥女', spouse: '姐夫/妹夫' } } }; let selectedRelations = []; // AI询问功能 async function askDeepSeek(question) { const askAIButton = document.querySelector('#askAI'); const aiSpinner = document.querySelector('#aiSpinner'); const aiButtonText = document.querySelector('#aiButtonText'); if (!apiKey) { alert('请先设置API Key'); return; } try { // 设置加载状态 askAIButton.disabled = true; aiSpinner.style.display = 'inline-block'; aiButtonText.textContent = '计算中...'; const response = await fetch('https://api.deepseek.com/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` }, body: JSON.stringify({ model: "deepseek-chat", messages: [ { role: "system", content: "你是一个专门帮助计算中国亲戚关系称谓的助手。请只回答称谓结果,不要有多余的解释。" }, { role: "user", content: question } ], temperature: 0.7, max_tokens: 100, stream: false }) }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); if (data.choices && data.choices[0] && data.choices[0].message) { const result = document.querySelector('#result'); result.innerHTML = `AI计算结果:
${data.choices[0].message.content}`; } else { throw new Error('API返回格式错误'); } } catch (error) { console.error('API Error:', error); alert('调用AI服务出错:' + error.message); } finally { // 恢复按钮状态 askAIButton.disabled = false; aiSpinner.style.display = 'none'; aiButtonText.textContent = '询问AI'; } } // 创建模态框 function createModal() { const modal = document.createElement('div'); modal.id = 'relationCalcModal'; modal.style.cssText = ` position: fixed !important; top: 0 !important; left: 0 !important; width: 100% !important; height: 100% !important; background: rgba(0, 0, 0, 0.5) !important; z-index: 2147483646 !important; display: none !important; align-items: center !important; justify-content: center !important; `; const content = document.createElement('div'); content.style.cssText = ` background: white !important; border-radius: 10px !important; padding: 20px !important; max-width: 600px !important; width: 90% !important; max-height: 80% !important; overflow-y: auto !important; font-family: Arial, sans-serif !important; `; content.innerHTML = `

亲戚关系称谓计算器

API 配置
请选择关系
🤖 AI 助手
遇到问题? 联系作者
相关网站: lddgo tool
`; modal.appendChild(content); document.body.appendChild(modal); // 添加事件监听 setupModalEvents(modal); return modal; } // 设置模态框事件 function setupModalEvents(modal) { // 初始化API Key显示 const apiKeyInput = modal.querySelector('#apiKeyInput'); apiKeyInput.value = maskApiKey(apiKey); // API Key显示/隐藏切换 modal.querySelector('#toggleApiKey').onclick = () => { const input = modal.querySelector('#apiKeyInput'); const toggleBtn = modal.querySelector('#toggleApiKey'); if (input.type === 'password') { input.type = 'text'; input.value = apiKey; toggleBtn.textContent = '🙈'; } else { input.type = 'password'; input.value = maskApiKey(apiKey); toggleBtn.textContent = '👁'; } }; // 保存API Key - 更简单的逻辑 modal.querySelector('#saveApiKey').onclick = () => { const input = modal.querySelector('#apiKeyInput'); const toggleBtn = modal.querySelector('#toggleApiKey'); // 如果是password模式且显示的是加密值,先切换到明文模式获取真实值 if (input.type === 'password') { input.type = 'text'; input.value = apiKey || ''; // 显示当前的API Key或空值 } const newApiKey = input.value.trim(); if (!newApiKey) { alert('请输入有效的API Key'); input.focus(); return; } // 保存API Key localStorage.setItem('deepseek_api_key', newApiKey); apiKey = newApiKey; // 保存后切换回password模式并显示加密值 input.type = 'password'; input.value = maskApiKey(apiKey); toggleBtn.textContent = '👁'; alert('API Key已保存!'); }; // 关闭模态框 modal.querySelector('#closeModal').onclick = () => hideModal(); modal.onclick = (e) => { if (e.target === modal) hideModal(); }; // 关系选择按钮 modal.querySelectorAll('.relation-btn').forEach(btn => { btn.onclick = () => { const relation = btn.dataset.relation; selectedRelations.push(relation); updatePath(); btn.style.background = '#4e73df'; btn.style.color = 'white'; setTimeout(() => { btn.style.background = '#f8f9fa'; btn.style.color = 'black'; }, 200); }; }); // 回退按钮 modal.querySelector('#undoBtn').onclick = () => { if (selectedRelations.length > 0) { selectedRelations.pop(); updatePath(); } }; // 清空按钮 modal.querySelector('#clearBtn').onclick = () => { selectedRelations = []; updatePath(); modal.querySelector('#result').innerHTML = ''; }; // 计算按钮 modal.querySelector('#calculateBtn').onclick = () => { calculateResult(); }; // AI询问按钮 modal.querySelector('#askAI').onclick = () => { const complexRelation = modal.querySelector('#complexRelation').value; if (!complexRelation) { alert('请输入需要计算的关系描述'); return; } askDeepSeek(`请计算这个称谓:${complexRelation}`); }; } // 更新关系路径显示 function updatePath() { const pathDiv = document.querySelector('#selectedPath'); if (!pathDiv) return; if (selectedRelations.length === 0) { pathDiv.innerHTML = '请选择关系'; return; } const path = selectedRelations.map(r => relationData[r].name).join(' → '); pathDiv.innerHTML = path; } // 计算结果 function calculateResult() { const resultDiv = document.querySelector('#result'); if (!resultDiv) return; if (selectedRelations.length === 0) { resultDiv.innerHTML = '请先选择关系'; return; } const gender = document.querySelector('input[name="gender"]:checked').value; const direction = document.querySelector('input[name="direction"]:checked').value; // 计算关系链 let result = ''; let currentRelation = null; for (let i = 0; i < selectedRelations.length; i++) { const relation = selectedRelations[i]; if (!currentRelation) { currentRelation = relationData[relation]; result = currentRelation.name; } else { if (currentRelation.relations && currentRelation.relations[relation]) { result = currentRelation.relations[relation]; currentRelation = relationData[relation]; } else { result += '的' + relationData[relation].name; currentRelation = relationData[relation]; } } } // 处理称呼方式 if (direction === 'fromOther') { result = '对方的' + result; } // 根据性别调整称呼 if (gender === 'female') { result = result.replace(/兄弟/g, '姐妹') .replace(/儿子/g, '女儿') .replace(/父亲/g, '母亲'); } resultDiv.innerHTML = `${result}
已计算 ${selectedRelations.length} 层关系`; } // 显示模态框 function showModal() { const modal = document.getElementById('relationCalcModal') || createModal(); modal.style.display = 'flex'; updatePath(); } // 隐藏模态框 function hideModal() { const modal = document.getElementById('relationCalcModal'); if (modal) { modal.style.display = 'none'; } } // 创建按钮的函数 function createButton() { console.log('开始创建按钮'); if (document.getElementById('relationCalcButton')) { console.log('按钮已存在,跳过创建'); return; } const btn = document.createElement('div'); btn.id = 'relationCalcButton'; btn.style.cssText = ` position: fixed !important; bottom: 20px !important; right: 20px !important; width: 60px !important; height: 60px !important; background: #4e73df !important; color: white !important; border-radius: 50% !important; display: flex !important; align-items: center !important; justify-content: center !important; cursor: pointer !important; z-index: 2147483647 !important; font-size: 12px !important; font-weight: bold !important; font-family: Arial, sans-serif !important; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3) !important; user-select: none !important; transition: all 0.3s ease !important; `; btn.textContent = '称谓'; btn.onclick = function() { console.log('按钮被点击'); showModal(); }; btn.onmouseover = function() { btn.style.transform = 'translateY(-2px)'; btn.style.background = '#224abe'; }; btn.onmouseout = function() { btn.style.transform = 'translateY(0)'; btn.style.background = '#4e73df'; }; try { if (document.body) { document.body.appendChild(btn); console.log('按钮已成功添加到body'); } else { document.documentElement.appendChild(btn); console.log('按钮已添加到html元素'); } } catch (error) { console.error('添加按钮失败:', error); } } // 初始化 function initialize() { console.log('开始初始化,文档状态:', document.readyState); createButton(); if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function() { console.log('DOMContentLoaded 触发'); setTimeout(createButton, 100); }); } window.addEventListener('load', function() { console.log('window load 触发'); setTimeout(createButton, 200); }); setTimeout(function() { console.log('延迟创建触发'); createButton(); }, 1000); } initialize(); })();