mirror of
https://github.com/Zippland/Snap-Solver.git
synced 2026-01-19 01:21:13 +08:00
Feature: 配置剪切板功能支持,现在支持网页修改宿主机的剪切板
This commit is contained in:
46
app.py
46
app.py
@@ -1013,37 +1013,33 @@ def update_clipboard():
|
||||
if not isinstance(text, str) or not text.strip():
|
||||
return jsonify({"success": False, "message": "剪贴板内容不能为空"}), 400
|
||||
|
||||
if not pyperclip.is_available():
|
||||
return jsonify({"success": False, "message": "服务器未配置剪贴板支持"}), 503
|
||||
|
||||
pyperclip.copy(text)
|
||||
return jsonify({"success": True})
|
||||
except pyperclip.PyperclipException:
|
||||
app.logger.exception("复制到剪贴板失败")
|
||||
return jsonify({"success": False, "message": "复制到剪贴板失败,请检查服务器环境"}), 500
|
||||
except Exception:
|
||||
# 直接尝试复制,不使用is_available()检查
|
||||
try:
|
||||
pyperclip.copy(text)
|
||||
return jsonify({"success": True})
|
||||
except Exception as e:
|
||||
return jsonify({"success": False, "message": f"复制到剪贴板失败: {str(e)}"}), 500
|
||||
except Exception as e:
|
||||
app.logger.exception("更新剪贴板时发生异常")
|
||||
return jsonify({"success": False, "message": "服务器内部错误"}), 500
|
||||
return jsonify({"success": False, "message": f"服务器内部错误: {str(e)}"}), 500
|
||||
|
||||
@app.route('/api/clipboard', methods=['GET'])
|
||||
def get_clipboard():
|
||||
"""从服务器剪贴板读取文本"""
|
||||
try:
|
||||
if not pyperclip.is_available():
|
||||
return jsonify({"success": False, "message": "服务器未配置剪贴板支持"}), 503
|
||||
|
||||
text = pyperclip.paste()
|
||||
if text is None:
|
||||
text = ""
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"text": text,
|
||||
"message": "成功读取剪贴板内容"
|
||||
})
|
||||
except pyperclip.PyperclipException as e:
|
||||
app.logger.exception("读取剪贴板失败")
|
||||
return jsonify({"success": False, "message": f"读取剪贴板失败: {str(e)}"}), 500
|
||||
# 直接尝试读取,不使用is_available()检查
|
||||
try:
|
||||
text = pyperclip.paste()
|
||||
if text is None:
|
||||
text = ""
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"text": text,
|
||||
"message": "成功读取剪贴板内容"
|
||||
})
|
||||
except Exception as e:
|
||||
return jsonify({"success": False, "message": f"读取剪贴板失败: {str(e)}"}), 500
|
||||
except Exception as e:
|
||||
app.logger.exception("读取剪贴板时发生异常")
|
||||
return jsonify({"success": False, "message": f"服务器内部错误: {str(e)}"}), 500
|
||||
|
||||
@@ -38,6 +38,7 @@ class SnapSolver {
|
||||
this.analysisIndicator = document.querySelector('.analysis-indicator');
|
||||
this.clipboardTextarea = document.getElementById('clipboardText');
|
||||
this.clipboardSendButton = document.getElementById('clipboardSend');
|
||||
this.clipboardReadButton = document.getElementById('clipboardRead');
|
||||
this.clipboardStatus = document.getElementById('clipboardStatus');
|
||||
|
||||
// Crop elements
|
||||
@@ -73,6 +74,7 @@ class SnapSolver {
|
||||
this.stopGenerationBtn = document.getElementById('stopGenerationBtn');
|
||||
this.clipboardTextarea = document.getElementById('clipboardText');
|
||||
this.clipboardSendButton = document.getElementById('clipboardSend');
|
||||
this.clipboardReadButton = document.getElementById('clipboardRead');
|
||||
this.clipboardStatus = document.getElementById('clipboardStatus');
|
||||
|
||||
// 处理按钮事件
|
||||
@@ -951,16 +953,24 @@ class SnapSolver {
|
||||
}
|
||||
|
||||
setupClipboardFeature() {
|
||||
if (!this.clipboardTextarea || !this.clipboardSendButton) {
|
||||
if (!this.clipboardTextarea || !this.clipboardSendButton || !this.clipboardReadButton) {
|
||||
console.warn('Clipboard controls not found in DOM');
|
||||
return;
|
||||
}
|
||||
|
||||
// 读取剪贴板按钮事件
|
||||
this.clipboardReadButton.addEventListener('click', (event) => {
|
||||
event.preventDefault();
|
||||
this.readClipboardText();
|
||||
});
|
||||
|
||||
// 发送到剪贴板按钮事件
|
||||
this.clipboardSendButton.addEventListener('click', (event) => {
|
||||
event.preventDefault();
|
||||
this.sendClipboardText();
|
||||
});
|
||||
|
||||
// 键盘快捷键:Ctrl/Cmd + Enter 发送到剪贴板
|
||||
this.clipboardTextarea.addEventListener('keydown', (event) => {
|
||||
if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
|
||||
event.preventDefault();
|
||||
@@ -1021,6 +1031,45 @@ class SnapSolver {
|
||||
this.clipboardStatus.dataset.status = status;
|
||||
}
|
||||
|
||||
async readClipboardText() {
|
||||
if (!this.clipboardTextarea || !this.clipboardReadButton) return;
|
||||
|
||||
this.updateClipboardStatus('读取中...', 'pending');
|
||||
this.clipboardReadButton.disabled = true;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/clipboard', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
const result = await response.json().catch(() => ({}));
|
||||
|
||||
if (response.ok && result?.success) {
|
||||
// 将读取到的内容填入文本框
|
||||
this.clipboardTextarea.value = result.text || '';
|
||||
|
||||
const successMessage = result.text ?
|
||||
`成功读取剪贴板内容 (${result.text.length} 字符)` :
|
||||
'剪贴板为空';
|
||||
this.updateClipboardStatus(successMessage, 'success');
|
||||
window.uiManager?.showToast(successMessage, 'success');
|
||||
} else {
|
||||
const errorMessage = result?.message || '读取失败,请稍后重试';
|
||||
this.updateClipboardStatus(errorMessage, 'error');
|
||||
window.uiManager?.showToast(errorMessage, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to read clipboard text:', error);
|
||||
const networkErrorMessage = '网络错误,读取失败';
|
||||
this.updateClipboardStatus(networkErrorMessage, 'error');
|
||||
window.uiManager?.showToast(networkErrorMessage, 'error');
|
||||
} finally {
|
||||
this.clipboardReadButton.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
async sendClipboardText() {
|
||||
if (!this.clipboardTextarea) return;
|
||||
|
||||
|
||||
@@ -3515,12 +3515,30 @@ textarea:focus {
|
||||
margin-top: 0.75rem;
|
||||
}
|
||||
|
||||
.clipboard-send-btn {
|
||||
.clipboard-send-btn,
|
||||
.clipboard-read-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.clipboard-read-btn {
|
||||
background-color: var(--primary);
|
||||
color: white;
|
||||
border: 1px solid var(--primary);
|
||||
}
|
||||
|
||||
.clipboard-read-btn:hover {
|
||||
background-color: var(--primary-dark);
|
||||
border-color: var(--primary-dark);
|
||||
}
|
||||
|
||||
.clipboard-read-btn:disabled {
|
||||
background-color: var(--text-tertiary);
|
||||
border-color: var(--text-tertiary);
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.clipboard-status {
|
||||
font-size: 0.85rem;
|
||||
color: var(--text-tertiary);
|
||||
|
||||
@@ -168,11 +168,15 @@
|
||||
|
||||
<div class="clipboard-panel">
|
||||
<div class="clipboard-header">
|
||||
<h3><i class="fas fa-clipboard"></i> 发送到服务端剪贴板</h3>
|
||||
<p class="clipboard-hint">输入内容后点击按钮或按 <kbd>Ctrl</kbd>/<kbd>Cmd</kbd> + <kbd>Enter</kbd> 快速发送</p>
|
||||
<h3><i class="fas fa-clipboard"></i> 剪贴板操作</h3>
|
||||
<p class="clipboard-hint">读取宿主机剪贴板内容或发送内容到服务端剪贴板</p>
|
||||
</div>
|
||||
<textarea id="clipboardText" rows="4" placeholder="输入要复制到服务端剪贴板的文字"></textarea>
|
||||
<textarea id="clipboardText" rows="4" placeholder="剪贴板内容将显示在这里,也可以手动输入内容"></textarea>
|
||||
<div class="clipboard-actions">
|
||||
<button id="clipboardRead" class="btn-action clipboard-read-btn" type="button">
|
||||
<i class="fas fa-download"></i>
|
||||
<span>读取剪贴板</span>
|
||||
</button>
|
||||
<button id="clipboardSend" class="btn-action clipboard-send-btn" type="button">
|
||||
<i class="fas fa-clipboard-check"></i>
|
||||
<span>发送至剪贴板</span>
|
||||
|
||||
Reference in New Issue
Block a user