feat(sidepanel): 添加时间戳转换功能并优化默认状态

新增时间戳与日期时间相互转换的功能,包括输入框、选择器和转换按钮。
同时将侧边栏默认状态设为关闭,并完善相关样式和逻辑。
此外,增强复制功能的上下文检查以提升稳定性。
This commit is contained in:
雨霖铃
2025-11-12 22:31:00 +08:00
parent a42f1f2cce
commit 43ee47751b
4 changed files with 122 additions and 13 deletions
+2 -2
View File
@@ -26,7 +26,7 @@
zIndex: '2147483647', zIndex: '2147483647',
boxShadow: '-4px 0 8px rgba(0,0,0,0.15)', boxShadow: '-4px 0 8px rgba(0,0,0,0.15)',
backgroundColor: 'white', backgroundColor: 'white',
transform: 'translateX(0)', transform: 'translateX(100%)', // 默认关闭状态
transition: 'transform 0.3s ease', transition: 'transform 0.3s ease',
}); });
@@ -65,7 +65,7 @@
document.body.appendChild(toggleBtn); document.body.appendChild(toggleBtn);
// 折叠 / 展开逻辑 // 折叠 / 展开逻辑
let visible = true; let visible = false; // 默认关闭状态
toggleBtn.addEventListener('click', () => { toggleBtn.addEventListener('click', () => {
visible = !visible; visible = !visible;
iframe.style.transform = visible ? 'translateX(0)' : 'translateX(100%)'; iframe.style.transform = visible ? 'translateX(0)' : 'translateX(100%)';
+23
View File
@@ -35,6 +35,29 @@
margin-left: auto; 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 { .content {
padding: 1rem; padding: 1rem;
} }
+30
View File
@@ -18,6 +18,7 @@
<!-- 页面内容容器 --> <!-- 页面内容容器 -->
<div class="content"> <div class="content">
<!-- 时间戳工具页面 --> <!-- 时间戳工具页面 -->
<div id="page-timestamp" class="page active"> <div id="page-timestamp" class="page active">
<h2>时间戳工具</h2> <h2>时间戳工具</h2>
@@ -32,6 +33,35 @@
<button id="start-timestamp-btn" style="display: none">开始</button> <button id="start-timestamp-btn" style="display: none">开始</button>
</p> </p>
</div> </div>
<!-- 时间戳转日期时间 -->
<div>
<h2>时间戳转日期时间</h2>
<div>
<input type="text" id="input-timestamp" placeholder="请输入时间戳" class="input-text" />
<select class="select-box">
<option value="milliseconds">毫秒(ms)</option>
<option value="seconds">秒(s)</option>
</select>
</div>
<div>
<button id="convert-timestamp-btn" class="convert-button">转换</button>
</div>
<div>
<input type="text" id="timestamp-input-result" placeholder="转换结果" class="input-text"></input>
<select class="select-box">
<option>Asia/Shanghai</option>
</select>
</div>
</div>
<!-- 日期时间转时间戳 -->
<div>
<h2>日期时间转时间戳</h2>
<button id="convert-datetime-btn">转换</button>
<p id="converted-timestamp"></p>
</div>
</div> </div>
<!-- 其他工具页面 --> <!-- 其他工具页面 -->
+65 -9
View File
@@ -48,6 +48,59 @@ toggleTimestampBtn.addEventListener('click', () => {
updateTimestamp(showMilliseconds); 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 copyTimestampBtn = document.getElementById('copy-timestamp-btn');
const currentTimestamp = document.getElementById('current-timestamp'); const currentTimestamp = document.getElementById('current-timestamp');
@@ -55,15 +108,18 @@ copyTimestampBtn.addEventListener('click', () => {
// 获取时间戳文本 // 获取时间戳文本
const timestampText = currentTimestamp.textContent; const timestampText = currentTimestamp.textContent;
chrome.runtime.sendMessage({ action: 'copyText', data: timestampText }, (res) => { // 检查扩展上下文是否仍然有效
if (chrome.runtime.lastError) { if (chrome.runtime && chrome.runtime.sendMessage) {
console.error('❌ 传送时间戳文本失败:', chrome.runtime.lastError); chrome.runtime.sendMessage({ action: 'copyText', data: timestampText }, (res) => {
} else if (res?.success) { if (chrome.runtime.lastError) {
console.log('✅ 已复制', timestampText); console.error('❌ 传送时间戳文本失败', chrome.runtime.lastError);
} else { } else if (res?.success) {
console.error('❌ 复制失败', res); console.log('✅ 已复制', timestampText);
} } else {
}); console.error('❌ 复制失败:', res);
}
});
}
}); });
function switchPage(pageName) { function switchPage(pageName) {