From 43ee47751b4f7b497e64431e3940bd72b23dbfc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=A8=E9=9C=96=E9=93=83?= Date: Wed, 12 Nov 2025 22:31:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(sidepanel):=20=E6=B7=BB=E5=8A=A0=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=88=B3=E8=BD=AC=E6=8D=A2=E5=8A=9F=E8=83=BD=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=BB=98=E8=AE=A4=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增时间戳与日期时间相互转换的功能,包括输入框、选择器和转换按钮。 同时将侧边栏默认状态设为关闭,并完善相关样式和逻辑。 此外,增强复制功能的上下文检查以提升稳定性。 --- content.js | 6 ++-- sidepanel/index.css | 25 ++++++++++++++- sidepanel/index.html | 30 ++++++++++++++++++ sidepanel/index.js | 74 ++++++++++++++++++++++++++++++++++++++------ 4 files changed, 122 insertions(+), 13 deletions(-) diff --git a/content.js b/content.js index 358ed97..32dda83 100644 --- a/content.js +++ b/content.js @@ -26,7 +26,7 @@ zIndex: '2147483647', boxShadow: '-4px 0 8px rgba(0,0,0,0.15)', backgroundColor: 'white', - transform: 'translateX(0)', + transform: 'translateX(100%)', // 默认关闭状态 transition: 'transform 0.3s ease', }); @@ -65,7 +65,7 @@ document.body.appendChild(toggleBtn); // 折叠 / 展开逻辑 - let visible = true; + let visible = false; // 默认关闭状态 toggleBtn.addEventListener('click', () => { visible = !visible; iframe.style.transform = visible ? 'translateX(0)' : 'translateX(100%)'; @@ -95,4 +95,4 @@ return true; } }); -})(); +})(); \ No newline at end of file diff --git a/sidepanel/index.css b/sidepanel/index.css index 818edbb..dd1b290 100644 --- a/sidepanel/index.css +++ b/sidepanel/index.css @@ -35,6 +35,29 @@ margin-left: auto; } +.input-text { + width: 100%; + padding: 0.5rem; + margin: 0.5rem 0; + box-sizing: border-box; +} + +.convert-button { + padding: 0.5rem 1rem; + background-color: #28a745; + color: white; + border: none; + border-radius: 0.25rem; + cursor: pointer; +} + +.select-box { + width: 100%; + padding: 0.5rem; + margin: 0.5rem 0; + box-sizing: border-box; +} + .content { padding: 1rem; } @@ -45,4 +68,4 @@ .page.active { display: block; -} \ No newline at end of file +} diff --git a/sidepanel/index.html b/sidepanel/index.html index 875aa87..6724b47 100644 --- a/sidepanel/index.html +++ b/sidepanel/index.html @@ -18,6 +18,7 @@
+

时间戳工具

@@ -32,6 +33,35 @@

+ + +
+

时间戳转日期时间

+
+ + +
+
+ +
+
+ + +
+
+ + +
+

日期时间转时间戳

+ + +

+
diff --git a/sidepanel/index.js b/sidepanel/index.js index d38e942..1b73ab6 100644 --- a/sidepanel/index.js +++ b/sidepanel/index.js @@ -48,6 +48,59 @@ toggleTimestampBtn.addEventListener('click', () => { updateTimestamp(showMilliseconds); }); +const inputTimestamp = document.getElementById('input-timestamp'); +const convertTimestampBtn = document.getElementById('convert-timestamp-btn'); +const timestampInputResult = document.getElementById('timestamp-input-result'); +const timestampUnitSelect = document.querySelector('#page-timestamp select'); // 时间戳单位选择器 + +convertTimestampBtn.addEventListener('click', () => { + const timestamp = inputTimestamp.value.trim(); + console.log('输入时间戳:', timestamp); + + if (!timestamp) { + timestampInputResult.value = '请输入时间戳'; + return; + } + + // 获取选择的时间戳单位(秒或毫秒) + const isSeconds = timestampUnitSelect.value === 'seconds'; + + // 转换为数字 + let timestampNum = Number(timestamp); + + // 如果是秒,转换为毫秒 + if (isSeconds) { + timestampNum = timestampNum * 1000; + } + + // 检查是否为有效数字 + if (isNaN(timestampNum)) { + timestampInputResult.value = '请输入有效的时间戳'; + return; + } + + // 创建日期对象 + const date = new Date(timestampNum); + + // 检查日期是否有效 + if (isNaN(date.getTime())) { + timestampInputResult.value = '无效的日期'; + return; + } + + console.log('转换日期对象:', date); + const year = date.getFullYear(); + const month = date.getMonth() + 1; + const day = date.getDate(); + const hours = date.getHours(); + const minutes = date.getMinutes(); + const seconds = date.getSeconds(); + + console.log('转换结果:', `${year}年${month}月${day}日 ${hours}时${minutes}分${seconds}秒`); + + timestampInputResult.value = `${year}年${month}月${day}日 ${hours}时${minutes}分${seconds}秒`; +}); + // 复制时间戳到剪贴板 const copyTimestampBtn = document.getElementById('copy-timestamp-btn'); const currentTimestamp = document.getElementById('current-timestamp'); @@ -55,15 +108,18 @@ copyTimestampBtn.addEventListener('click', () => { // 获取时间戳文本 const timestampText = currentTimestamp.textContent; - chrome.runtime.sendMessage({ action: 'copyText', data: timestampText }, (res) => { - if (chrome.runtime.lastError) { - console.error('❌ 传送时间戳文本失败:', chrome.runtime.lastError); - } else if (res?.success) { - console.log('✅ 已复制:', timestampText); - } else { - console.error('❌ 复制失败:', res); - } - }); + // 检查扩展上下文是否仍然有效 + if (chrome.runtime && chrome.runtime.sendMessage) { + chrome.runtime.sendMessage({ action: 'copyText', data: timestampText }, (res) => { + if (chrome.runtime.lastError) { + console.error('❌ 传送时间戳文本失败:', chrome.runtime.lastError); + } else if (res?.success) { + console.log('✅ 已复制:', timestampText); + } else { + console.error('❌ 复制失败:', res); + } + }); + } }); function switchPage(pageName) {