-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharch-packages.user.js
75 lines (67 loc) · 2.4 KB
/
arch-packages.user.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
// ==UserScript==
// @name Arch Packages time
// @namespace https://github.com/lilydjwg/userscripts
// @version 0.3
// @description use local time format for package dates
// @match https://archlinux.org/packages/*
// @require https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.12/dayjs.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.12/locale/en.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.12/plugin/customParseFormat.min.js
// @grant none
// ==/UserScript==
'use strict'
function format_relative_time(d1, d2) {
// in miliseconds
const units = {
year: 24 * 60 * 60 * 1000 * 365,
month: (24 * 60 * 60 * 1000 * 365) / 12,
day: 24 * 60 * 60 * 1000,
hour: 60 * 60 * 1000,
minute: 60 * 1000,
second: 1000,
};
const rtf = new Intl.RelativeTimeFormat()
// https://stackoverflow.com/a/60688789
const elapsed = d1.valueOf() - d2.valueOf()
for (const [u, period] of Object.entries(units)) {
if (Math.abs(elapsed) > period || u === "second") {
// https://stackoverflow.com/a/64972112
return rtf.format(
Math.round(elapsed / period),
u
)
}
}
}
// July 31, 2024, 6:45 a.m. UTC
const parse_format = 'MMMM D, YYYY, H:mm a ZZ'
const NOW = new Date()
function parse_date(t) {
t = t.replace(/([ap])\.m\./, '$1m')
t = t.replace('UTC', '+0000')
// e.g. Dec. 22, 2023, 11 p.m. UTC
t = t.replace(/( [0-9]+) ([ap]m)/, '$1:00 $2')
return dayjs(t, parse_format, 'en')
}
{
const nodes = document.evaluate('//table[@id="pkginfo"]//td[contains(text(), "UTC")]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)
for(let i=0, len=nodes.snapshotLength; i<len; i++) {
const el = nodes.snapshotItem(i)
const d = parse_date(el.textContent)
el.textContent = d.format('YYYY-MM-DD HH:mm ZZ')
el.title = format_relative_time(d.toDate(), NOW)
}
}
{
const formatter2 = new Intl.DateTimeFormat(undefined, {
dateStyle: "long",
})
const nodes2 = document.evaluate('//div[@id="pkglist-results" or @id="exact-matches"]//tr/td[position()=6 or position()=7]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)
for(let i=0, len=nodes2.snapshotLength; i<len; i++) {
const el = nodes2.snapshotItem(i)
if(el.textContent !== '') {
const d = dayjs(el.textContent, 'MMMM D, YYYY', 'en')
el.textContent = d.format('YYYY-MM-DD')
}
}
}