feat(sidepanel): 添加时间戳转换功能并优化默认状态
新增时间戳与日期时间相互转换的功能,包括输入框、选择器和转换按钮。 同时将侧边栏默认状态设为关闭,并完善相关样式和逻辑。 此外,增强复制功能的上下文检查以提升稳定性。
This commit is contained in:
+2
-2
@@ -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%)';
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|
||||||
<!-- 其他工具页面 -->
|
<!-- 其他工具页面 -->
|
||||||
|
|||||||
@@ -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,6 +108,8 @@ copyTimestampBtn.addEventListener('click', () => {
|
|||||||
// 获取时间戳文本
|
// 获取时间戳文本
|
||||||
const timestampText = currentTimestamp.textContent;
|
const timestampText = currentTimestamp.textContent;
|
||||||
|
|
||||||
|
// 检查扩展上下文是否仍然有效
|
||||||
|
if (chrome.runtime && chrome.runtime.sendMessage) {
|
||||||
chrome.runtime.sendMessage({ action: 'copyText', data: timestampText }, (res) => {
|
chrome.runtime.sendMessage({ action: 'copyText', data: timestampText }, (res) => {
|
||||||
if (chrome.runtime.lastError) {
|
if (chrome.runtime.lastError) {
|
||||||
console.error('❌ 传送时间戳文本失败:', chrome.runtime.lastError);
|
console.error('❌ 传送时间戳文本失败:', chrome.runtime.lastError);
|
||||||
@@ -64,6 +119,7 @@ copyTimestampBtn.addEventListener('click', () => {
|
|||||||
console.error('❌ 复制失败:', res);
|
console.error('❌ 复制失败:', res);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function switchPage(pageName) {
|
function switchPage(pageName) {
|
||||||
|
|||||||
Reference in New Issue
Block a user