forked from ChinaGodMan/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_check.js
More file actions
108 lines (108 loc) · 2.99 KB
/
Copy pathtime_check.js
File metadata and controls
108 lines (108 loc) · 2.99 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as i18n from './i18n/i18n'
export function displayMessage(el) {
document
.querySelector('#js-repo-pjax-container')
.insertAdjacentElement('beforebegin', el)
}
export function renderWarning(timediff) {
const banner = document.createElement('div')
banner.id = 'zh-banner-warning'
banner.setAttribute(
'style',
`
background-color: red;
height: 100px;
margin-bottom: 20px;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 36px;
position: relative;
`
)
banner.textContent = i18n.translate.renderWarning
const smallTag = document.createElement('div')
smallTag.setAttribute(
'style',
`
position: absolute;
bottom: 0;
right: 0;
padding: 5px 10px;
font-size: 14px;
border-top-left-radius: 5px;
`
)
smallTag.textContent = timediff
banner.appendChild(smallTag)
displayMessage(banner)
}
export function renderCaution(timediff) {
const banner = document.createElement('div')
banner.id = 'zh-banner-warning'
banner.setAttribute(
'style',
`
background-color: yellow;
height: 50px;
margin-bottom: 20px;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
position: relative;
`
)
banner.textContent = i18n.translate.renderCaution
const smallTag = document.createElement('div')
smallTag.setAttribute(
'style',
`
position: absolute;
bottom: 0;
right: 0;
padding: 5px 10px;
font-size: 14px;
border-top-left-radius: 5px;
`
)
smallTag.textContent = timediff
banner.appendChild(smallTag)
displayMessage(banner)
}
export function checkCommitDate(datetimeString) {
if (document.querySelector('#zh-banner-warning')) return
const date = new Date(datetimeString)
const now = new Date()
const yearsDiff = now.getFullYear() - date.getFullYear()
const monthsDiff = now.getMonth() - date.getMonth()
const daysDiff = now.getDate() - date.getDate()
let adjustedMonths = monthsDiff
let adjustedDays = daysDiff
if (adjustedDays < 0) {
adjustedMonths--
const lastMonth = new Date(now.getFullYear(), now.getMonth(), 0)
adjustedDays += lastMonth.getDate()
}
let finalYears = yearsDiff
if (adjustedMonths < 0) {
finalYears--
adjustedMonths += 12
}
let result = i18n.translate.timediff
if (finalYears === 0) {
result = result.replace(/\{years\}.*?(?=\{months\})/, '')
}
result = result.replace('{years}', finalYears > 0 ? finalYears : '')
result = result.replace('{months}', adjustedMonths)
result = result.replace('{days}', adjustedDays)
const daysSinceLastCommit = (Date.now() - date.getTime()) / 1000 / 60 / 60 / 24
if (daysSinceLastCommit > 365) {
renderWarning(result)
} else if (daysSinceLastCommit > 182.5) {
renderCaution(result)
} else {
/* noop */
}
}