Skip to content

Commit

Permalink
time-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Incpi committed Oct 15, 2024
1 parent 07722b9 commit 641ae27
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 58 deletions.
Binary file removed bin/Dark CPI Extension.zip
Binary file not shown.
25 changes: 5 additions & 20 deletions popup/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,28 @@
--btn-active-color: #00941e;
--btn-hover-color: #007bff;
}

.dark-theme {
--bg-color: #121212;
--text-color: #e0e0e0;
}

.old-theme {
--bg-color: #ffffff;
--text-color: #1e2a38;
}

a.logo.btn-sm {
background: transparent;
border-color: transparent !important;
}

a.logo.btn-sm > * {
fill: var(--text-color);
}

.old-theme nav {
color: var(--bg-color);
background-color: var(--text-color);

a.logo.btn-sm > * {
fill: var(--bg-color) !important;
}
}

main h1 {
font-size: 1rem;
font-weight: bold;
Expand All @@ -42,51 +35,46 @@ main h1 {
justify-content: center !important;
margin-bottom: 1rem !important;
}

body {
font-family: Arial, sans-serif;
text-align: center;
max-height: 800px;
min-width: 320px;
margin: 0;
background-color: var(--bg-color);
color: var(--text-color);
}

span#output-text {
white-space: pre-wrap;
white-space-collapse: break-spaces;
text-wrap: pretty;
}

.darkcpiglobal .navbar {
justify-content: space-between;
}

main:not(:has(div[role='alert'])) {
padding: 1em;
}

#options {
display: inline-flex;
gap: 1em;

.btn-sm {
width: 7em;
}
}

.stat-value {
font-size: large !important;
}

.alert.alert-error {
border-radius: 0px;
font-size: medium;
font-weight: bolder;
font-family: sans-serif;
padding: 0.75rem;
}

.hidden {
display: none !important;
}
.darkcpiglobal {
.btn-sm:not(.btn-sm-ghost).active,
.btn-sm:not(.btn-sm-ghost):focus {
Expand All @@ -95,17 +83,14 @@ main:not(:has(div[role='alert'])) {
color: var(--text-color);
border-color: transparent transparent transparent;
}

.btn-sm:not(.btn-sm-ghost):hover {
background: var(--btn-hover-color);
background-color: var(--btn-hover-color);
color: var(--text-color);
border-color: transparent transparent transparent;
}

.alert {
grid-auto-flow: column;
/* grid-template-columns: auto minmax(auto, 1fr); */
grid-auto-flow: column; /* grid-template-columns: auto minmax(auto, 1fr); */
justify-items: start;
text-align: start;
span {
Expand Down
13 changes: 10 additions & 3 deletions popup/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<link rel="stylesheet" href="popup.css">
</head>

<body class="darkcpiglobal">
<body class="darkcpiglobal" data-theme="light">
<nav class="navbar shadow-lg">
<div class="avatar">
<div class="rounded" style="width:2em">
Expand Down Expand Up @@ -51,7 +51,7 @@ <h1 class="text-xl font-bold"><span class="activeapp"></span> Application: <span
<div class="bg-gray-100 rounded-lg p-4 shadow border border-gray-300">
<input type="text" id="time-input" class="input input-bordered input-primary" style="width: 90% !important;"
placeholder="Enter Unix or ISO timestamp">
<div class="stat" id="converted-output">
<div class="stat hidden" id="converted-output">
<div class="stat-title" id="time-type"></div>
<div class="stat-value" id="output-text"></div>
<div id="copy-icon" class="stat-actions">
Expand All @@ -64,7 +64,14 @@ <h1 class="text-xl font-bold"><span class="activeapp"></span> Application: <span
<span style="font-size: small;"></span>
</div>
</div>

<div class="max-w-lg w-full bg-white rounded-xl shadow-lg p-6 space-y-6">
<div class="divider divider-info"><span style="font-size: larger;font-weight: bold;">About</span></div>
<div class="bg-gray-100 rounded-lg p-4 shadow border border-gray-300">
<a href="https://github.com/Incpi/Dark-CPI-Web-Extension" target="_blank" class="btn btn-block btn-outline btn-accent">
Contribute or Support on GitHub
</a>
</div>
</div>
<script src="./../lib/Daisyui.min.js"></script>
<script src="./../common/constants.js"></script>
<script type="module" src="popup.js"></script>
Expand Down
95 changes: 60 additions & 35 deletions popup/time-convert.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,104 @@
"use strict";

// Cache DOM elements for performance
const localTimeElement = document.getElementById("local-time");
const utcTimeElement = document.getElementById("utc-time");
const outputTextElement = document.getElementById("output-text");
const convertedOutputElement = document.querySelector("#converted-output");
const timeTypeElement = document.getElementById("time-type");
const copyIcon = document.getElementById("copy-icon");
const timeInput = document.getElementById("time-input");
const toast = document.getElementById("toast");

function formatDate(date) {
const options = { hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false };
const day = date.getDate();
const month = date.toLocaleString("default", { month: "short" });
const year = date.getFullYear();

const suffix = (day) => {
if (day > 3 && day < 21) return "th";
switch (day % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
};

return `${day}${suffix(day)} ${month} ${year}, ${date.toLocaleString(undefined, options)}`;
}

function updateTime() {
const localTime = new Date();
const utcTime = new Date(localTime.toISOString());

document.getElementById("local-time").innerText = localTime.toLocaleString().toUpperCase();
document.getElementById("utc-time").innerText = utcTime.toLocaleString("en-US", { timeZone: "UTC" });
localTimeElement.innerText = formatDate(localTime);
const utcTime = new Date(localTime.getTime() + localTime.getTimezoneOffset() * 60000);
utcTimeElement.innerText = formatDate(utcTime);
}

function convertTimestamp() {
const input = document.getElementById("time-input").value.trim();
const outputTextElement = document.getElementById("output-text");
const localTimeDisplay = document.getElementById("local-time");
const utcTimeDisplay = document.getElementById("utc-time");
const input = timeInput.value.trim();
if (!input) {
outputTextElement.innerText = "";
updateTime();
convertedOutputElement.classList.add("hidden");
return;
}

let date;
let timeType = "Error";
// Check if the input is a valid Unix timestamp (e.g., all digits).
convertedOutputElement.classList.add("hidden");
// Check if the input is a valid Unix timestamp
if (/^\d+$/.test(input)) {
const unixTimestamp = parseInt(input, 10);
date = new Date(unixTimestamp * 1000); // Convert to milliseconds.
outputTextElement.innerText = `${date.toISOString()}`;
date = new Date(unixTimestamp * 1000);
outputTextElement.innerText = date.toISOString();
timeType = "ISO";
}
// Check if the input is a valid ISO string.
// Check if the input is a valid ISO string
else if (!isNaN(Date.parse(input))) {
date = new Date(input);
const unixTimestamp = Math.floor(date.getTime() / 1000);
outputTextElement.innerText = unixTimestamp;
timeType = "Unix";
outputTextElement.innerText = `${unixTimestamp}`;
} else {
showToast("Invalid Unix or ISO timestamp", "error");
return;
}
document.getElementById("time-type").innerHTML = timeType;
// Display the local time and UTC time derived from the date.
localTimeDisplay.innerText = date.toLocaleString();
utcTimeDisplay.innerText = date.toLocaleString("en-US", { timeZone: "UTC" });

convertedOutputElement.classList.remove("hidden");
timeTypeElement.innerHTML = timeType;
}

// Copy the output to clipboard when the icon is clicked.
document.getElementById("copy-icon").addEventListener("click", function () {
const outputText = document.getElementById("output-text").innerText;
const copyOutputToClipboard = () => {
const outputText = outputTextElement.innerText;

if (outputText) {
navigator.clipboard
.writeText(outputText)
.then(() => {
showToast("Copied Stamp");
})
.catch(() => {
showToast("Failed to Copy", "error");
});
.then(() => showToast("Copied Stamp"))
.catch(() => showToast("Failed to Copy", "error"));
}
});
};

// Function to show the toast message
function showToast(message, type = "success", toast = document.getElementById("toast")) {
// Show toast message
function showToast(message, type = "success") {
toast.querySelector("span").innerText = message;
toast.style.display = "block";
toast.querySelector(".alert").classList = "alert alert-" + type;
toast.querySelector(".alert").className = `alert alert-${type}`;

// Hide the toast after 3 seconds
setTimeout(() => {
toast.style.display = "none";
console.log("closed");
}, 3000);
}

// Add event listener for input change.
document.getElementById("time-input").addEventListener("input", convertTimestamp);
// Event listeners
timeInput.addEventListener("input", convertTimestamp);
copyIcon.addEventListener("click", copyOutputToClipboard);

// Update the time every second.
// Update the time every second
setInterval(updateTime, 1000);

0 comments on commit 641ae27

Please sign in to comment.