diff --git a/docs/QUICK_REF.md b/docs/QUICK_REF.md index 9b81d7d..d3dc22b 100644 --- a/docs/QUICK_REF.md +++ b/docs/QUICK_REF.md @@ -7,7 +7,7 @@ ## 版本速查 ### 当前版本 -`0.0.5` → 下一目标 `0.1.1` ([M2.1](./M2.md)) +`0.1.1` → 下一目标 `0.1.2` ([M2.2](./M2.md)) ### 模块版本范围 diff --git a/docs/README.md b/docs/README.md index c1abe5f..b4db761 100644 --- a/docs/README.md +++ b/docs/README.md @@ -65,9 +65,9 @@ M11.10完成 → 1.0.0 (正式发布) ## 当前状态 -**当前版本**: `0.0.5` -**当前进度**: 5/97 (5%) -**下一任务**: [M2.1 文本选择检测](./M2.md#m21-文本选择检测--目标版本-011) +**当前版本**: `0.1.1` +**当前进度**: 6/97 (6%) +**下一任务**: [M2.2 获取选中文本坐标](./M2.md#m22-获取选中文本坐标--目标版本-0112) --- diff --git a/docs/VERSION.md b/docs/VERSION.md index cd510b3..f071ec5 100644 --- a/docs/VERSION.md +++ b/docs/VERSION.md @@ -29,7 +29,7 @@ | 任务 | 版本 | 描述 | 状态 | 日期 | |------|------|------|------|------| -| M2.1 | 0.1.1 | 文本选择检测 | ⬜ | - | +| M2.1 | 0.1.1 | 文本选择检测 | ✅ | 2026-02-09 | | M2.2 | 0.1.2 | 获取选中文本坐标 | ⬜ | - | | M2.3 | 0.1.3 | 沙拉图标组件 | ⬜ | - | | M2.4 | 0.1.4 | 图标定位显示 | ⬜ | - | diff --git a/manifest.json b/manifest.json index ded5627..a12439e 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "沙拉查词", - "version": "0.0.5", + "version": "0.1.1", "description": "聚合词典划词翻译", "permissions": [ "storage", diff --git a/package.json b/package.json index 62ce9a6..1df2ad6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "salad-dict", - "version": "0.0.5", + "version": "0.1.1", "description": "聚合词典划词翻译", "private": true, "type": "module", diff --git a/src/content/index.js b/src/content/index.js index 2273416..6248c58 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,31 +1,34 @@ // Content Script import { messaging, ping } from '../shared/messaging.js'; +import { initSelectionListener } from './selection.js'; +import { logger } from './logger.js'; -console.log('[SaladDict] Content script loaded'); +logger.info('Content script loaded'); + +// 初始化文本选择监听 +initSelectionListener(); // Ping 测试 - 验证通信链路 async function testCommunication() { - console.log('[SaladDict] Testing communication...'); + logger.info('Testing communication...'); - // 测试 1: 简单 Ping const pingResult = await ping(); - console.log('[SaladDict] Ping test:', pingResult ? '✅ OK' : '❌ Failed'); + logger.info('Ping test:', pingResult ? 'OK' : 'Failed'); - // 测试 2: Echo 消息 try { const echoResult = await messaging.sendToBackground('ECHO', { message: 'Hello from content' }); - console.log('[SaladDict] Echo test:', echoResult); + logger.info('Echo test:', echoResult); } catch (error) { - console.error('[SaladDict] Echo test failed:', error.message); + logger.error('Echo test failed:', error.message); } } -// 延迟执行测试,确保 background 已就绪 +// 延迟执行测试 setTimeout(testCommunication, 1000); // 监听来自 Background 的消息 chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { - console.log('[SaladDict] Content received message:', message); + logger.info('Content received message:', message); sendResponse({ received: true }); return true; }); diff --git a/src/content/logger.js b/src/content/logger.js new file mode 100644 index 0000000..b11f0b1 --- /dev/null +++ b/src/content/logger.js @@ -0,0 +1,20 @@ +/** + * @file Content Script 日志工具 + */ + +const NAMESPACE = '[SaladDict]'; + +export const logger = { + debug(...args) { + console.debug(NAMESPACE, ...args); + }, + info(...args) { + console.info(NAMESPACE, ...args); + }, + warn(...args) { + console.warn(NAMESPACE, ...args); + }, + error(...args) { + console.error(NAMESPACE, ...args); + } +}; diff --git a/src/content/selection.js b/src/content/selection.js new file mode 100644 index 0000000..db2d48d --- /dev/null +++ b/src/content/selection.js @@ -0,0 +1,59 @@ +/** + * @file 文本选择检测 + * @description 监听网页文本选择事件 + */ + +import { logger } from './logger.js'; + +/** + * 获取当前选中的文本 + * @returns {string} 选中的文本 + */ +export function getSelectedText() { + const selection = window.getSelection(); + return selection ? selection.toString().trim() : ''; +} + +/** + * 检查是否有有效文本被选中 + * @returns {boolean} + */ +export function hasSelection() { + const text = getSelectedText(); + return text.length > 0; +} + +/** + * 处理鼠标抬起事件 + * @param {MouseEvent} event + */ +function handleMouseUp(event) { + // 延迟执行,等待选区完成 + setTimeout(() => { + const selectedText = getSelectedText(); + + if (selectedText.length > 0) { + logger.info('Text selected:', selectedText); + console.log('[SaladDict] Selected text:', selectedText); + } + }, 10); +} + +/** + * 初始化文本选择监听 + */ +export function initSelectionListener() { + logger.info('Initializing selection listener'); + + document.addEventListener('mouseup', handleMouseUp); + + logger.info('Selection listener initialized'); +} + +/** + * 销毁文本选择监听 + */ +export function destroySelectionListener() { + document.removeEventListener('mouseup', handleMouseUp); + logger.info('Selection listener destroyed'); +}