// ==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 = `