dsh-desktop-statusbar 3.0.3 → 3.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.en.md +145 -145
- package/README.md +142 -142
- package/cordis.patch.yml +14 -14
- package/icon.svg +10 -10
- package/lib/balance-api.js +782 -516
- package/lib/client.js +12364 -11436
- package/lib/exchange-rate.js +199 -199
- package/lib/holiday.js +128 -128
- package/lib/index.js +7784 -7190
- package/lib/release-age.js +509 -509
- package/lib/self-update.js +913 -913
- package/locale/de.json +6 -0
- package/locale/en.json +6 -6
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -6
- package/package.json +72 -72
- package/tools/shot.cjs +18 -13
package/lib/holiday.js
CHANGED
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 节假日日历:把国务院办公厅放假通知的正文解析成"哪些放假日落在周一至周五"。
|
|
3
|
-
*
|
|
4
|
-
* 只在 host 侧用:客户端拿不到跨域页面,由 host 抓取 + 解析后把日期数组交给客户端。
|
|
5
|
-
*
|
|
6
|
-
* 通知里的四种写法(2023-2026 四年原文都覆盖到了):
|
|
7
|
-
* 一、元旦:1月1日(周四)至3日(周六)放假调休,共3天。
|
|
8
|
-
* 一、元旦:1月1日放假,与周末连休。
|
|
9
|
-
* 二、春节:1月28日(农历除夕、周二)至2月4日(农历正月初七、周二)放假调休,共8天。
|
|
10
|
-
* 一、元旦:2022年12月31日至2023年1月2日放假调休,共3天。
|
|
11
|
-
*
|
|
12
|
-
* 只返回周一至周五的放假日 —— 周末本来就整天空闲,调休上班的周末也一样空闲,列进来没用。
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/** 通知里的日期:年份与月份都可以省略,例 `2022年12月31日` / `1月2日` / `至6日` */
|
|
16
|
-
const NOTICE_DATE = /(?:(\d{4})年)?(?:(\d{1,2})月)?(\d{1,2})日/g
|
|
17
|
-
const DAY_MS = 86400000
|
|
18
|
-
/** 一段假期最长不会超过这个天数,防正则意外把超长区间展开 */
|
|
19
|
-
const MAX_RANGE_DAYS = 40
|
|
20
|
-
/** HTML 解析的体积上限(字符):真实通知页 100~300KB,2MB 已经非常宽松。
|
|
21
|
-
* 超过就只看前 2MB —— 宁可解析不全,也不能让 host 主线程卡住(见 holidayHtmlToText)。 */
|
|
22
|
-
const HOLIDAY_HTML_MAX = 2 * 1024 * 1024
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 线性切掉成对标签里的整段内容(`<script ...>...</script>` 这类)。
|
|
26
|
-
* 用 indexOf 前进而不是懒惰量词:找不到闭合标签时直接放弃剩余部分,
|
|
27
|
-
* 复杂度严格 O(n);`/<script[\s\S]*?<\/script>/gi` 在缺闭合时会从每个 `<` 往后扫到文末,
|
|
28
|
-
* 实测 40 万个未闭合 `<script>` 要 6 秒(卡死 host 事件循环)。
|
|
29
|
-
*/
|
|
30
|
-
function stripBlocks(text, tag) {
|
|
31
|
-
const open = '<' + tag
|
|
32
|
-
const close = '</' + tag
|
|
33
|
-
const lower = text.toLowerCase()
|
|
34
|
-
let out = ''
|
|
35
|
-
let at = 0
|
|
36
|
-
for (;;) {
|
|
37
|
-
const start = lower.indexOf(open, at)
|
|
38
|
-
if (start === -1) return out + text.slice(at)
|
|
39
|
-
out += text.slice(at, start)
|
|
40
|
-
const end = lower.indexOf(close, start)
|
|
41
|
-
if (end === -1) return out /* 没有闭合:整段丢弃,不再往后扫 */
|
|
42
|
-
const gt = text.indexOf('>', end)
|
|
43
|
-
if (gt === -1) return out
|
|
44
|
-
at = gt + 1
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** HTML → 纯文本:去脚本、样式与标签,解常见实体。
|
|
49
|
-
*
|
|
50
|
-
* 这段处理的是**外部网页**(国务院通知),内容不受我们控制,所以两处做了防御:
|
|
51
|
-
* ① 先按长度截断,避免超大文档把主线程占满;
|
|
52
|
-
* ② `<[^<>]+>` 而不是 `<[^>]+>` —— 后者不排除 `<`,大量孤立 `<` 时会反复跨越大段文本,
|
|
53
|
-
* 实测 20 万字符要 12 秒。 */
|
|
54
|
-
export function holidayHtmlToText(html) {
|
|
55
|
-
const raw = String(html === null || html === undefined ? '' : html).slice(0, HOLIDAY_HTML_MAX)
|
|
56
|
-
return stripBlocks(stripBlocks(raw, 'script'), 'style')
|
|
57
|
-
.replace(/<br\s*\/?>/gi, '\n')
|
|
58
|
-
.replace(/<\/p>/gi, '\n')
|
|
59
|
-
.replace(/<[^<>]+>/g, '')
|
|
60
|
-
.replace(/ /g, ' ')
|
|
61
|
-
.replace(/&/g, '&')
|
|
62
|
-
.replace(/</g, '<')
|
|
63
|
-
.replace(/>/g, '>')
|
|
64
|
-
.replace(/"/g, '"')
|
|
65
|
-
.replace(/[ \t\u3000]+/g, ' ')
|
|
66
|
-
.split('\n')
|
|
67
|
-
.map((line) => line.trim())
|
|
68
|
-
.filter((line) => line.length > 0)
|
|
69
|
-
.join('\n')
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const pad = (n) => (n < 10 ? '0' + String(n) : String(n))
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 从通知正文里取出该年的放假日(只留周一至周五,升序)。
|
|
76
|
-
* @param {string} text 通知正文,纯文本或 HTML 都行(HTML 会自动剥标签)
|
|
77
|
-
* @param {number} year 通知对应的年份
|
|
78
|
-
* @returns {string[]} `YYYY-MM-DD` 数组
|
|
79
|
-
*/
|
|
80
|
-
export function parseHolidayNotice(text, year) {
|
|
81
|
-
const days = new Set()
|
|
82
|
-
const flat = String(text === null || text === undefined ? '' : text)
|
|
83
|
-
.slice(0, HOLIDAY_HTML_MAX)
|
|
84
|
-
/* `[^<>]` 而不是 `[^>]`:理由同 holidayHtmlToText —— 大量孤立 `<` 时会退化 */
|
|
85
|
-
.replace(/<[^<>]+>/g, ' ')
|
|
86
|
-
.replace(/\s+/g, ' ')
|
|
87
|
-
/* 只认含"放假"的句子;含"上班"的句子是调休上班日,不能算进空闲 */
|
|
88
|
-
flat.split(/[。;;\n]/).forEach((sentence) => {
|
|
89
|
-
if (sentence.indexOf('放假') === -1 || sentence.indexOf('上班') !== -1) return
|
|
90
|
-
NOTICE_DATE.lastIndex = 0
|
|
91
|
-
const hits = []
|
|
92
|
-
let hit = null
|
|
93
|
-
while ((hit = NOTICE_DATE.exec(sentence)) !== null) {
|
|
94
|
-
hits.push({
|
|
95
|
-
year: hit[1] === undefined ? null : Number(hit[1]),
|
|
96
|
-
month: hit[2] === undefined ? null : Number(hit[2]),
|
|
97
|
-
day: Number(hit[3]),
|
|
98
|
-
})
|
|
99
|
-
}
|
|
100
|
-
if (hits.length === 0) return
|
|
101
|
-
/* 句子里有"至"就是一段区间(取前两个日期当起止);否则每个日期各算一天 */
|
|
102
|
-
const ranges = sentence.indexOf('至') !== -1 && hits.length >= 2
|
|
103
|
-
? [[hits[0], hits[1]]]
|
|
104
|
-
: hits.map((one) => [one, one])
|
|
105
|
-
ranges.forEach((pair) => {
|
|
106
|
-
const from = pair[0]
|
|
107
|
-
const to = pair[1]
|
|
108
|
-
/* 区间终点常省略月份("4月4日至6日"),用起点的月份补上;孤立的"6日"没法定位,跳过 */
|
|
109
|
-
const startMonth = from.month
|
|
110
|
-
if (startMonth === null) return
|
|
111
|
-
const endMonth = to.month === null ? startMonth : to.month
|
|
112
|
-
const startYear = from.year === null ? year : from.year
|
|
113
|
-
let endYear = to.year === null ? startYear : to.year
|
|
114
|
-
/* 终点月份比起点小 = 跨年,例:12月31日至1月2日 */
|
|
115
|
-
if (to.year === null && endMonth < startMonth) endYear = startYear + 1
|
|
116
|
-
const last = Date.UTC(endYear, endMonth - 1, to.day)
|
|
117
|
-
let cursor = Date.UTC(startYear, startMonth - 1, from.day)
|
|
118
|
-
for (let i = 0; cursor <= last && i < MAX_RANGE_DAYS; i += 1, cursor += DAY_MS) {
|
|
119
|
-
const at = new Date(cursor)
|
|
120
|
-
if (at.getUTCFullYear() !== year) continue
|
|
121
|
-
const weekday = at.getUTCDay()
|
|
122
|
-
if (weekday === 0 || weekday === 6) continue
|
|
123
|
-
days.add(String(at.getUTCFullYear()) + '-' + pad(at.getUTCMonth() + 1) + '-' + pad(at.getUTCDate()))
|
|
124
|
-
}
|
|
125
|
-
})
|
|
126
|
-
})
|
|
127
|
-
return Array.from(days).sort()
|
|
128
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 节假日日历:把国务院办公厅放假通知的正文解析成"哪些放假日落在周一至周五"。
|
|
3
|
+
*
|
|
4
|
+
* 只在 host 侧用:客户端拿不到跨域页面,由 host 抓取 + 解析后把日期数组交给客户端。
|
|
5
|
+
*
|
|
6
|
+
* 通知里的四种写法(2023-2026 四年原文都覆盖到了):
|
|
7
|
+
* 一、元旦:1月1日(周四)至3日(周六)放假调休,共3天。
|
|
8
|
+
* 一、元旦:1月1日放假,与周末连休。
|
|
9
|
+
* 二、春节:1月28日(农历除夕、周二)至2月4日(农历正月初七、周二)放假调休,共8天。
|
|
10
|
+
* 一、元旦:2022年12月31日至2023年1月2日放假调休,共3天。
|
|
11
|
+
*
|
|
12
|
+
* 只返回周一至周五的放假日 —— 周末本来就整天空闲,调休上班的周末也一样空闲,列进来没用。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** 通知里的日期:年份与月份都可以省略,例 `2022年12月31日` / `1月2日` / `至6日` */
|
|
16
|
+
const NOTICE_DATE = /(?:(\d{4})年)?(?:(\d{1,2})月)?(\d{1,2})日/g
|
|
17
|
+
const DAY_MS = 86400000
|
|
18
|
+
/** 一段假期最长不会超过这个天数,防正则意外把超长区间展开 */
|
|
19
|
+
const MAX_RANGE_DAYS = 40
|
|
20
|
+
/** HTML 解析的体积上限(字符):真实通知页 100~300KB,2MB 已经非常宽松。
|
|
21
|
+
* 超过就只看前 2MB —— 宁可解析不全,也不能让 host 主线程卡住(见 holidayHtmlToText)。 */
|
|
22
|
+
const HOLIDAY_HTML_MAX = 2 * 1024 * 1024
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 线性切掉成对标签里的整段内容(`<script ...>...</script>` 这类)。
|
|
26
|
+
* 用 indexOf 前进而不是懒惰量词:找不到闭合标签时直接放弃剩余部分,
|
|
27
|
+
* 复杂度严格 O(n);`/<script[\s\S]*?<\/script>/gi` 在缺闭合时会从每个 `<` 往后扫到文末,
|
|
28
|
+
* 实测 40 万个未闭合 `<script>` 要 6 秒(卡死 host 事件循环)。
|
|
29
|
+
*/
|
|
30
|
+
function stripBlocks(text, tag) {
|
|
31
|
+
const open = '<' + tag
|
|
32
|
+
const close = '</' + tag
|
|
33
|
+
const lower = text.toLowerCase()
|
|
34
|
+
let out = ''
|
|
35
|
+
let at = 0
|
|
36
|
+
for (;;) {
|
|
37
|
+
const start = lower.indexOf(open, at)
|
|
38
|
+
if (start === -1) return out + text.slice(at)
|
|
39
|
+
out += text.slice(at, start)
|
|
40
|
+
const end = lower.indexOf(close, start)
|
|
41
|
+
if (end === -1) return out /* 没有闭合:整段丢弃,不再往后扫 */
|
|
42
|
+
const gt = text.indexOf('>', end)
|
|
43
|
+
if (gt === -1) return out
|
|
44
|
+
at = gt + 1
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** HTML → 纯文本:去脚本、样式与标签,解常见实体。
|
|
49
|
+
*
|
|
50
|
+
* 这段处理的是**外部网页**(国务院通知),内容不受我们控制,所以两处做了防御:
|
|
51
|
+
* ① 先按长度截断,避免超大文档把主线程占满;
|
|
52
|
+
* ② `<[^<>]+>` 而不是 `<[^>]+>` —— 后者不排除 `<`,大量孤立 `<` 时会反复跨越大段文本,
|
|
53
|
+
* 实测 20 万字符要 12 秒。 */
|
|
54
|
+
export function holidayHtmlToText(html) {
|
|
55
|
+
const raw = String(html === null || html === undefined ? '' : html).slice(0, HOLIDAY_HTML_MAX)
|
|
56
|
+
return stripBlocks(stripBlocks(raw, 'script'), 'style')
|
|
57
|
+
.replace(/<br\s*\/?>/gi, '\n')
|
|
58
|
+
.replace(/<\/p>/gi, '\n')
|
|
59
|
+
.replace(/<[^<>]+>/g, '')
|
|
60
|
+
.replace(/ /g, ' ')
|
|
61
|
+
.replace(/&/g, '&')
|
|
62
|
+
.replace(/</g, '<')
|
|
63
|
+
.replace(/>/g, '>')
|
|
64
|
+
.replace(/"/g, '"')
|
|
65
|
+
.replace(/[ \t\u3000]+/g, ' ')
|
|
66
|
+
.split('\n')
|
|
67
|
+
.map((line) => line.trim())
|
|
68
|
+
.filter((line) => line.length > 0)
|
|
69
|
+
.join('\n')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const pad = (n) => (n < 10 ? '0' + String(n) : String(n))
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 从通知正文里取出该年的放假日(只留周一至周五,升序)。
|
|
76
|
+
* @param {string} text 通知正文,纯文本或 HTML 都行(HTML 会自动剥标签)
|
|
77
|
+
* @param {number} year 通知对应的年份
|
|
78
|
+
* @returns {string[]} `YYYY-MM-DD` 数组
|
|
79
|
+
*/
|
|
80
|
+
export function parseHolidayNotice(text, year) {
|
|
81
|
+
const days = new Set()
|
|
82
|
+
const flat = String(text === null || text === undefined ? '' : text)
|
|
83
|
+
.slice(0, HOLIDAY_HTML_MAX)
|
|
84
|
+
/* `[^<>]` 而不是 `[^>]`:理由同 holidayHtmlToText —— 大量孤立 `<` 时会退化 */
|
|
85
|
+
.replace(/<[^<>]+>/g, ' ')
|
|
86
|
+
.replace(/\s+/g, ' ')
|
|
87
|
+
/* 只认含"放假"的句子;含"上班"的句子是调休上班日,不能算进空闲 */
|
|
88
|
+
flat.split(/[。;;\n]/).forEach((sentence) => {
|
|
89
|
+
if (sentence.indexOf('放假') === -1 || sentence.indexOf('上班') !== -1) return
|
|
90
|
+
NOTICE_DATE.lastIndex = 0
|
|
91
|
+
const hits = []
|
|
92
|
+
let hit = null
|
|
93
|
+
while ((hit = NOTICE_DATE.exec(sentence)) !== null) {
|
|
94
|
+
hits.push({
|
|
95
|
+
year: hit[1] === undefined ? null : Number(hit[1]),
|
|
96
|
+
month: hit[2] === undefined ? null : Number(hit[2]),
|
|
97
|
+
day: Number(hit[3]),
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
if (hits.length === 0) return
|
|
101
|
+
/* 句子里有"至"就是一段区间(取前两个日期当起止);否则每个日期各算一天 */
|
|
102
|
+
const ranges = sentence.indexOf('至') !== -1 && hits.length >= 2
|
|
103
|
+
? [[hits[0], hits[1]]]
|
|
104
|
+
: hits.map((one) => [one, one])
|
|
105
|
+
ranges.forEach((pair) => {
|
|
106
|
+
const from = pair[0]
|
|
107
|
+
const to = pair[1]
|
|
108
|
+
/* 区间终点常省略月份("4月4日至6日"),用起点的月份补上;孤立的"6日"没法定位,跳过 */
|
|
109
|
+
const startMonth = from.month
|
|
110
|
+
if (startMonth === null) return
|
|
111
|
+
const endMonth = to.month === null ? startMonth : to.month
|
|
112
|
+
const startYear = from.year === null ? year : from.year
|
|
113
|
+
let endYear = to.year === null ? startYear : to.year
|
|
114
|
+
/* 终点月份比起点小 = 跨年,例:12月31日至1月2日 */
|
|
115
|
+
if (to.year === null && endMonth < startMonth) endYear = startYear + 1
|
|
116
|
+
const last = Date.UTC(endYear, endMonth - 1, to.day)
|
|
117
|
+
let cursor = Date.UTC(startYear, startMonth - 1, from.day)
|
|
118
|
+
for (let i = 0; cursor <= last && i < MAX_RANGE_DAYS; i += 1, cursor += DAY_MS) {
|
|
119
|
+
const at = new Date(cursor)
|
|
120
|
+
if (at.getUTCFullYear() !== year) continue
|
|
121
|
+
const weekday = at.getUTCDay()
|
|
122
|
+
if (weekday === 0 || weekday === 6) continue
|
|
123
|
+
days.add(String(at.getUTCFullYear()) + '-' + pad(at.getUTCMonth() + 1) + '-' + pad(at.getUTCDate()))
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
return Array.from(days).sort()
|
|
128
|
+
}
|