// ==UserScript== // @name 通用题目提取 + DeepSeek API // @namespace http://tampermonkey.net/ // @version 1.0 // @description 提取题目,调用DeepSeek官方API,右侧面板展示 // @author learn // @match *://*/* // @grant GM_xmlhttpRequest // @connect api.deepseek.com // @license MIT // ==/UserScript== (function() { 'use strict'; const API_URL = "https://api.deepseek.com/v1/chat/completions"; const API_KEY = "deepseek_api_key_123456"; // 替换为你的DeepSeek API Key const MODEL_NAME = "deepseek-chat"; const PROMPT_TPL = "你是答题助手,请解答下面这道题目,给出答案+简要解析:\n"; // 结果面板 const panel = document.createElement('div'); Object.assign(panel.style, { position: 'fixed', top: '120px', right: '16px', width: '340px', maxHeight: '500px', overflowY: 'auto', zIndex: '999999', padding: '12px', borderRadius: '8px', border: '1px solid #ccc', background: '#fff', color: '#222', fontSize: '14px', display: 'none' }); document.body.appendChild(panel); const closeBtn = document.createElement('button'); closeBtn.innerText = "✕ 关闭"; Object.assign(closeBtn.style, { float: 'right', marginBottom: '8px', border: 'none', background: '#ef4444', color: '#fff', borderRadius: '4px', padding: '4px 8px', cursor: 'pointer' }); closeBtn.onclick = () => panel.style.display = 'none'; panel.appendChild(closeBtn); const copyAnsBtn = document.createElement('button'); copyAnsBtn.innerText = "📋 复制答案"; Object.assign(copyAnsBtn.style, { marginBottom: '8px', marginLeft: '6px', border: 'none', background: '#2563eb', color: '#fff', borderRadius: '4px', padding: '4px 8px', cursor: 'pointer' }); let lastAnswerText = ""; copyAnsBtn.onclick = async () => { if(lastAnswerText){ await navigator.clipboard.writeText(lastAnswerText); alert("答案已复制!"); } }; panel.appendChild(copyAnsBtn); const contentDom = document.createElement('div'); panel.appendChild(contentDom); // 按钮 const btn = document.createElement('button'); btn.innerText = "🤖 提取题目并AI查询"; Object.assign(btn.style, { position: 'fixed', top: '80px', right: '16px', zIndex: '999999', padding: '6px 10px', background: '#2563eb', color: '#fff', border: 'none', borderRadius: '4px', cursor: 'pointer' }); document.body.appendChild(btn); function callAiApi(questionText) { const sendContent = PROMPT_TPL + questionText; contentDom.innerText = "⏳ 请求中,请稍候..."; panel.style.display = 'block'; GM_xmlhttpRequest({ method: "POST", url: API_URL, headers: { "Content-Type": "application/json", "Authorization": `Bearer ${API_KEY}` }, data: JSON.stringify({ model: MODEL_NAME, messages: [ {role:"user", content: sendContent} ] }), onload: function(res) { try { const json = JSON.parse(res.responseText); if (json.error) { contentDom.innerText = "❌ API报错:\n" + JSON.stringify(json.error,null,2); return; } const answer = json.choices[0].message.content; lastAnswerText = answer; contentDom.innerText = "🤖 AI答案:\n\n" + answer; } catch (e) { contentDom.innerText = "❌ 返回解析失败:\n" + res.responseText; } }, onerror: function(err) { contentDom.innerText = "❌ API请求失败,检查网络/密钥"; console.error(err); } }); } btn.onclick = async () => { const sel = window.getSelection().toString().trim(); let outText = sel; if (!outText) { const guessList = ['.question', '.item', '.topic', '.qus', '[class*="question"]', '[class*="q-"]', '[data-question]']; for(let selStr of guessList){ const els = document.querySelectorAll(selStr); if(els.length > 0){ outText = Array.from(els).map(el=>el.innerText).join("\n"); break; } } } outText = outText.trim(); if (!outText) { alert("❌ 识别不到题目!手动框选题目文字再点按钮"); return; } console.log("提取题目:", outText); await navigator.clipboard.writeText(outText); callAiApi(outText); } })();