Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
chrome.contextMenus.update(titleId, {
"title": convertStr,
});
});
});
// 此文件在 Manifest V3 中不再需要,因为功能已移至 service-worker.js
// 保留此文件以保持向后兼容性,但内容已清空
11 changes: 9 additions & 2 deletions src/content.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* 发送消息到 background.js
* 发送消息到 service worker
*/
window.onmouseup = function () {
let selection = window.getSelection();
Expand All @@ -8,4 +8,11 @@ window.onmouseup = function () {
} else {
chrome.runtime.sendMessage("");
}
}
}

// 监听来自service worker的消息
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "showAlert") {
alert(request.message);
}
});
20 changes: 9 additions & 11 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"browser_action": {
"manifest_version": 3,
"name": "时间戳转化",
"version": "1.0.3",
"description": "时间戳转换小工具。右键菜单显示转化,工具页时间戳转化",
"action": {
"default_popup": "popup.html"
},
"description": "时间戳转换小工具。右键菜单显示转化,工具页时间戳转化",
"manifest_version": 2,
"name": "时间戳转化",
"content_scripts": [
{
"matches": [
Expand All @@ -16,15 +17,12 @@
}
],
"options_page": "popup.html",
"version": "1.0.3",
"permissions": [
"clipboardRead",
"contextMenus"
"contextMenus",
"storage"
],
"background": {
"scripts": [
"background.js",
"utils.js"
]
"service_worker": "service-worker.js"
}
}
}
4 changes: 2 additions & 2 deletions src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@
<div>
<label for="show-alert-checkbox">右键菜单单击是否alert</label><input id="show-alert-checkbox" type="checkbox" name="menu-action">
<br/>
<span >时间戳位数判断:</span>
<span>时间戳位数判断:</span>
<label for="only-10-radio">仅10位</label><input type="radio" id="only-10-radio" name="timestamp-judge-type">
<label for="only-13-radio">丨仅13位</label><input type="radio" id="only-13-radio" name="timestamp-judge-type">
<label for="both-10-13-radio">丨10/13位</label><input type="radio" id="both-10-13-radio" name="timestamp-judge-type">
</div>

<div>
<span id="tip" style="font-size: xx-small; color: #808080; ">小提示:鼠标滚轮中键点击可以复制或者粘贴,年月日时分秒字符串的分隔符不限</span>
<span id="tip" style="font-size: xx-small; color: #808080;">小提示:鼠标滚轮中键点击可以复制或者粘贴,年月日时分秒字符串的分隔符不限</span>
</div>
<div>
<span id="msg"> </span>
Expand Down
89 changes: 84 additions & 5 deletions src/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,89 @@ let only10Radio = document.getElementById("only-10-radio");
let only13Radio = document.getElementById("only-13-radio");
let both10_13 = document.getElementById("both-10-13-radio");


/**
* 获取当前时间的13位时间戳
* @returns {number} 当前时间的13位时间戳
*/
function getTimestamp13() {
return now.getTime();
}

/**
* 刷新时间显示
*/
function refresh() {
now = new Date();
timestampNowInput.value = getTimestamp13();
bjTimeInput.value = getTimeString(getTimestamp13(), localStorage.timestampJudgeType);
bjTimeInput.value = getTimeString(getTimestamp13(), getTypeFromStorage());
}

/**
* 从存储中获取时间戳判断类型
*/
function getTypeFromStorage() {
// 默认返回 "3" (10/13位)
return localStorage.timestampJudgeType || "3";
}

/**
* 刷新时间显示并重置定时器
*/
function refreshWithInterval() {
window.clearInterval(interval);
refresh();
interval = getInterval();
}

/**
* 执行转换操作
*/
function change() {
let s = inputInput.value.trim().toString();
resultInput.value = convert(s, localStorage.timestampJudgeType);
resultInput.value = convert(s, getTypeFromStorage());
}

/**
* 从剪贴板粘贴内容
* @param item 要粘贴到的元素
*/
function paste(item) {
item.select();
document.execCommand('paste');
msg("从剪切板获取!");
}

/**
* 交换输入输出框的内容
*/
function exchangeEachOther() {
sendStrToInput(resultInput.value);
}

/**
* 复制内容到剪贴板
* @param item 要复制的元素
*/
function copy(item) {
item.select();
document.execCommand('copy');
msg("已复制到剪切板");
}

/**
* 显示消息并在1秒后清除
* @param m 要显示的消息
*/
function msg(m) {
msgSpan.innerText = m;
setTimeout(function () {
msgSpan.innerText = " ";
}, 1000);
}

/**
* 更新刷新间隔
*/
function refreshGap() {
let value = gapInput.value;
if (!isNaN(value)) {
Expand All @@ -81,27 +119,36 @@ function refreshGap() {
}
}

/**
* 设置输入框的值并执行转换
* @param text 要设置的文本
*/
function sendStrToInput(text) {
inputInput.value = text;
change();
}

// 绑定刷新按钮事件
refreshButton.onclick = function () {
refreshWithInterval();
};

// 绑定转换按钮事件
changeButton.onclick = function () {
change();
};

// 绑定输入框输入事件
inputInput.oninput = function () {
change();
};

// 绑定刷新间隔输入框事件
gapInput.oninput = function () {
refreshGap();
};

// 绑定刷新间隔输入框按键事件
gapInput.onkeypress = function (e) {
refreshGap();

Expand All @@ -110,60 +157,74 @@ gapInput.onkeypress = function (e) {
}
};

// 绑定结果输入框输入事件
resultInput.oninput = function () {
let value = resultInput.value;
inputInput.value = formatTimeString(value);
};

// 绑定输入框鼠标按下事件
inputInput.onmousedown = function (e) {
if (e.button === 1) {
paste(inputInput);
}
};

// 绑定结果框鼠标按下事件
resultInput.onmousedown = function (e) {
if (e.button === 1) {
copy(resultInput);
}
};

// 绑定时间戳输入框鼠标按下事件
timestampNowInput.onmousedown = function (e) {
if (e.button === 1) {
copy(timestampNowInput);
}
};

// 绑定北京时间输入框鼠标按下事件
bjTimeInput.onmousedown = function (e) {
if (e.button === 1) {
copy(bjTimeInput);
}
};

// 绑定"now"按钮点击事件
nowButton.onclick = function () {
sendStrToInput(new Date().getTime());
};

// 绑定复制时间戳按钮事件
copyTimestampButton.onclick = function () {
copy(timestampNowInput);
};

// 绑定复制时间按钮事件
copyTimeButton.onclick = function () {
copy(bjTimeInput);
};

// 绑定粘贴按钮事件
pasteButton.onclick = function () {
paste(inputInput);
};

// 绑定交换按钮事件
exchangeEachOtherButton.onclick = function () {
exchangeEachOther();
};

// 绑定清空按钮事件
clearButton.onclick = function () {
resultInput.value = "";
inputInput.value = "";
};

/**
* 更改自动刷新复选框状态
*/
function changeGoCheckBox() {
goStatus = goonCheckBox.checked;
if (goStatus) {
Expand All @@ -172,12 +233,15 @@ function changeGoCheckBox() {
localStorage.goStatus = goStatus;
}

// 绑定自动刷新复选框事件
goonCheckBox.onclick = changeGoCheckBox;

// 绑定复制结果按钮事件
copyResultButton.onclick = function () {
copy(resultInput);
};

// 绑定显示警告复选框事件
showAlertCheckbox.onclick = function () {
let checked = showAlertCheckbox.checked;
if (checked) {
Expand All @@ -187,18 +251,24 @@ showAlertCheckbox.onclick = function () {
}
};

// 绑定仅10位时间戳单选框事件
only10Radio.onclick = function () {
localStorage.timestampJudgeType = "1";
};

// 绑定仅13位时间戳单选框事件
only13Radio.onclick = function () {
localStorage.timestampJudgeType = "2";
};

// 绑定10/13位时间戳单选框事件
both10_13.onclick = function () {
localStorage.timestampJudgeType = "3";
};

/**
* 加载时间戳判断类型设置
*/
function loadTimestampJudgeType() {
let timestampJudgeType = localStorage.timestampJudgeType;
switch (timestampJudgeType) {
Expand All @@ -217,16 +287,26 @@ function loadTimestampJudgeType() {
}
}

/**
* 加载自动刷新状态
*/
function loadGoonStatus() {
goStatus = !(localStorage.goStatus === "false");
goonCheckBox.checked = goStatus;
goonCheckBox.isChecked = goStatus;
}

/**
* 加载菜单提醒操作设置
*/
function loadMenuRadioAction() {
showAlertCheckbox.checked = !(localStorage.showAlert === "false");
}

/**
* 获取定时器
* @returns {number} 定时器ID
*/
function getInterval() {
return setInterval(function () {
if (goStatus) {
Expand All @@ -235,16 +315,15 @@ function getInterval() {
}, gap);
}

// 初始化
refresh();
inputInput.focus();

inputInput.value = localStorage.selectText;
change();

loadGoonStatus();

loadMenuRadioAction();

loadTimestampJudgeType();

interval = getInterval();
Loading