From bcd15f141fdf2558e58c7a827ca1650ef0c284e5 Mon Sep 17 00:00:00 2001 From: zhukang <274546966@qq.com> Date: Wed, 15 Jan 2025 21:59:46 +0800 Subject: [PATCH] feat: add content validation - Add content validation to filter empty or invalid content - Add special handling for excalidraw clipboard content - Add minimum content length check - Add user feedback for invalid content - Improve error messages --- project/llmclipboard/llmclipboard/app.py | 43 +++++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/project/llmclipboard/llmclipboard/app.py b/project/llmclipboard/llmclipboard/app.py index 98fa728..86f9e55 100644 --- a/project/llmclipboard/llmclipboard/app.py +++ b/project/llmclipboard/llmclipboard/app.py @@ -40,6 +40,33 @@ class TextCaptureService: ) self.logger = logging.getLogger(__name__) + def is_valid_content(self, content): + """验证内容是否有效""" + if not content: + return False + + # 检查是否是空的JSON对象 + if content.strip().startswith('{') and content.strip().endswith('}'): + try: + import json + data = json.loads(content) + # 检查是否是空的excalidraw内容 + if data.get('type') == 'excalidraw/clipboard' and not data.get('elements'): + self.logger.info("跳过空的excalidraw内容") + return False + except: + pass + + # 检查内容是否全是空白字符 + if not content.strip(): + return False + + # 检查内容是否太短 + if len(content.strip()) < 5: + return False + + return True + def get_clipboard_content(self): """获取剪贴板内容""" try: @@ -50,7 +77,7 @@ class TextCaptureService: try: content = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT) self.logger.info("获取Unicode文本格式内容") - if content: + if content and self.is_valid_content(content): return content except Exception as e: self.logger.debug(f"获取Unicode文本失败: {e}") @@ -76,7 +103,7 @@ class TextCaptureService: html_content = html_content[start:end+7] content = h.handle(html_content).strip() - if content: + if content and self.is_valid_content(content): return content except Exception as e: self.logger.debug(f"获取HTML格式失败: {e}") @@ -86,18 +113,24 @@ class TextCaptureService: text_content = win32clipboard.GetClipboardData(win32con.CF_TEXT) self.logger.info("获取普通文本格式内容") if text_content: - return text_content.decode('gbk') + content = text_content.decode('gbk') + if self.is_valid_content(content): + return content except Exception as e: self.logger.debug(f"获取普通文本失败: {e}") - if not content: - self.logger.warning("剪贴板内容为空") + if not content or not self.is_valid_content(content): + self.logger.warning("剪贴板内容为空或无效") + if hasattr(self, 'gui') and self.gui: + self.gui.show_message("剪贴板内容为空或无效", error=True) return None return content except Exception as e: self.logger.error(f"获取剪贴板内容失败: {e}") + if hasattr(self, 'gui') and self.gui: + self.gui.show_message(f"获取剪贴板内容失败: {str(e)}", error=True) return None finally: try: