forked from jan0991/github-actions-full-datetime
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent.js
78 lines (67 loc) · 2.36 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
if (window.location.href.match(/^https:\/\/github\.com\/.*$/)) {
function formatDateTime(date) {
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const str = new Intl.DateTimeFormat("en-US", {
timeZone: timeZone,
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
hour12: false,
}).format(date);
return str;
}
function updateRunTimeDisplay() {
const timeElements = document.querySelectorAll("relative-time");
console.log("Found time elements:", timeElements.length); // Debugging line
timeElements.forEach((el) => {
const fullTime = el.getAttribute("datetime");
const localTime = formatDateTime(new Date(fullTime));
if (fullTime && !el.classList.contains("full-time-updated")) {
const newElement = document.createElement("div");
newElement.textContent = `${localTime}`;
newElement.style.fontSize = "12px"; // A little smaller to fix the layout.
// newElement.style.cssText = 'position: absolute; top: 0; left: 0;';
el.parentElement.style = "position: relative; white-space: nowrap;";
// Hide the relative time.
// el.style = "display: none; visibility: hidden;";
// Remove or hide the SVG element
const svgElement = el.parentElement.querySelector(
"svg.octicon-calendar",
);
if (svgElement) {
svgElement.style.display = "none"; // Hide the SVG element
}
// Insert the new element into the DOM
el.parentElement.insertBefore(newElement, el.nextSibling);
el.classList.add("full-time-updated");
}
});
}
function hasClassWithSuffix(suffix) {
const allElements = document.querySelectorAll("*");
counter = 0;
for (const element of allElements) {
for (const className of element.classList) {
if (className.endsWith(suffix)) {
counter++;
if (counter > 5) {
return true;
}
}
}
}
return false;
}
function checkForChanges() {
const result = hasClassWithSuffix("full-time-updated");
if (!result) {
updateRunTimeDisplay();
}
}
// Initial call to update the display
updateRunTimeDisplay();
// Set up a polling mechanism to check for changes every 2 seconds
setInterval(checkForChanges, 2000);
}