@nzlov/dsh-bing-wallpaper 0.0.0-stage → 1.5.0
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 +25 -0
- package/README.md +50 -2
- package/README.zh-CN.md +51 -0
- package/THIRD_PARTY_NOTICES.md +18 -0
- package/bing-source.js +212 -0
- package/boot-theme.js +84 -0
- package/client.js +144 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +6 -0
- package/index.js +120 -0
- package/locale/en.json +59 -0
- package/locale/zh.json +59 -0
- package/package.json +63 -4
- package/third-party/material-color-utilities-LICENSE.txt +191 -0
- package/wallpaper-filter.js +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 nzlov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
This license covers the plugin code and original SVG icon, NOT photographs
|
|
24
|
+
retrieved from Bing. Wallpaper copyrights and usage terms remain with Bing
|
|
25
|
+
and the respective photographers; the plugin does not redistribute them.
|
package/README.md
CHANGED
|
@@ -1,3 +1,51 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Bing wallpaper for DSH
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[简体中文](README.zh-CN.md) · English
|
|
4
|
+
|
|
5
|
+
Daily Bing wallpapers, image-derived theme colours, and automatic light/dark appearance for DSH Web. A standalone plugin with no changes to DSH itself.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- China and international (US) Bing sources, with separate caches and no automatic cross-source fallback.
|
|
12
|
+
- Ten colour styles: M3 Content, M3 Tonal Spot, M3 Fruit Salad, M3 Rainbow, M3 Monochrome, Vibrant, Faithful, Soft, Dysfunctional, and Muted.
|
|
13
|
+
- Clickable palette cards show light and dark previews separated by a diagonal line.
|
|
14
|
+
- Wallpaper visibility from 10% to 100%, with a reading overlay. Icons and semantic status colours are not recoloured.
|
|
15
|
+
- Optional blur, frosted glass (blur with a mist layer), blue tint, grayscale, and dimming filters, each with its own strength and a live preview. Filters affect only the wallpaper; theme colours still come from the original image.
|
|
16
|
+
- Scheduled, system, follow-DSH, or fixed light/dark appearance. The default schedule is 07:00–19:00 in the browser’s local time zone.
|
|
17
|
+
- Image title, date, source, and copyright overlaid at the bottom of the photo; refresh icon in the top-right corner.
|
|
18
|
+
- Automatic update checks, keeping the previous image on failure; Chinese and English UI.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Requires DSH `>=0.2.2-rc.4 <0.3.0`.
|
|
23
|
+
|
|
24
|
+
In DSH’s plugin manager, install **`@nzlov/dsh-bing-wallpaper`**, or ask the DSH agent to install that npm bundle. The npm package is prebuilt; no manual build is needed. Installation affects all sessions in the current profile.
|
|
25
|
+
|
|
26
|
+
To install from source (requires Git, Node.js, and npm):
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
git clone https://github.com/nzlov/dsh-bing-wallpaper.git
|
|
30
|
+
cd dsh-bing-wallpaper
|
|
31
|
+
npm ci --ignore-scripts
|
|
32
|
+
npm run build
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Then install this directory in DSH using its absolute path.
|
|
36
|
+
|
|
37
|
+
Open **Settings → Bing daily wallpaper**. Rebuild after changing source files; follow Plugin Manager’s restart instructions when updating an installed bundle.
|
|
38
|
+
|
|
39
|
+
## Notes
|
|
40
|
+
|
|
41
|
+
- No API key or CDN is required. Requests go only to fixed Bing HTTPS endpoints; chat content and browser credentials are not forwarded to Bing.
|
|
42
|
+
- Images are cached in memory, not saved as an offline wallpaper library. Update checks use an hourly server cache, so refreshing does not necessarily download a new image. The two regions may show the same photo.
|
|
43
|
+
- Plugin controls are saved in the current browser. DSH’s appearance preference is shared across the profile; use scheduling in one primary browser to avoid competing schedules.
|
|
44
|
+
|
|
45
|
+
## License and credits
|
|
46
|
+
|
|
47
|
+
Author: [nzlov](https://github.com/nzlov).
|
|
48
|
+
|
|
49
|
+
- Plugin code and the original SVG icon: [MIT](LICENSE).
|
|
50
|
+
- Material Color Utilities: Apache-2.0; see [third-party notices](THIRD_PARTY_NOTICES.md) and the [included license](third-party/material-color-utilities-LICENSE.txt).
|
|
51
|
+
- Photographs belong to Bing and the respective photographers. The plugin’s license does not grant rights to redistribute or commercially use those photographs. Follow the copyright link shown on each image.
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# DSH Bing 每日壁纸
|
|
2
|
+
|
|
3
|
+
简体中文 · [English](README.md)
|
|
4
|
+
|
|
5
|
+
为 DSH Web 添加每日 Bing 壁纸、从图片提取的主题色,以及自动日夜外观。独立插件,不修改 DSH 本体。
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
## 特性
|
|
10
|
+
|
|
11
|
+
- 国内(中国)与国际(美国)Bing 来源,缓存独立,失败时不自动混用其他来源。
|
|
12
|
+
- 十种取色风格:M3 Content、M3 Tonal Spot、M3 Fruit Salad、M3 Rainbow、M3 Monochrome、Vibrant、Faithful、Soft、Dysfunctional、Muted。
|
|
13
|
+
- 平铺配色卡片,日夜预览以斜线分隔,点击即可切换。
|
|
14
|
+
- 壁纸可见度 10%–100%,保留阅读蒙层;图标及语义状态色不重新染色。
|
|
15
|
+
- 支持模糊(Blur)、毛玻璃(模糊加雾面)、蓝色调、黑白和压暗滤镜,可分别调节强度并实时预览。滤镜仅影响壁纸,主题色仍从原图提取。
|
|
16
|
+
- 支持定时、跟随系统、跟随 DSH,以及固定浅色或深色。默认按浏览器本地时间在 07:00–19:00 使用浅色。
|
|
17
|
+
- 标题、日期、来源与版权叠放在图片底部,右上角提供刷新图标。
|
|
18
|
+
- 自动检查更新,失败保留上一张图片;界面支持中英文。
|
|
19
|
+
|
|
20
|
+
## 安装
|
|
21
|
+
|
|
22
|
+
需要 DSH `>=0.2.2-rc.4 <0.3.0`。
|
|
23
|
+
|
|
24
|
+
在 DSH 插件管理中安装 **`@nzlov/dsh-bing-wallpaper`**,或让 DSH Agent 安装该 npm bundle。npm 包已预构建,无需手动构建。安装会影响当前 profile 的所有会话。
|
|
25
|
+
|
|
26
|
+
如需从源码安装(需要 Git、Node.js 和 npm):
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
git clone https://github.com/nzlov/dsh-bing-wallpaper.git
|
|
30
|
+
cd dsh-bing-wallpaper
|
|
31
|
+
npm ci --ignore-scripts
|
|
32
|
+
npm run build
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
随后在 DSH 中使用本目录的绝对路径安装。
|
|
36
|
+
|
|
37
|
+
启用后打开 **设置 → Bing 每日壁纸**。修改源码后需重新构建;更新已安装的插件时,按插件管理器提示决定是否重启。
|
|
38
|
+
|
|
39
|
+
## 使用须知
|
|
40
|
+
|
|
41
|
+
- 无需 API Key 或 CDN。上游仅访问固定 Bing HTTPS 接口,不向 Bing 发送聊天内容或浏览器凭据。
|
|
42
|
+
- 图片仅缓存在内存,不提供离线壁纸库。服务端按小时缓存,因此点击刷新不一定下载新图;两个地区也可能展示同一张照片。
|
|
43
|
+
- 插件控制项保存在当前浏览器,DSH 的外观偏好则由整个 profile 共享。建议仅在一个主要浏览器启用定时切换,避免不同时间表互相覆盖。
|
|
44
|
+
|
|
45
|
+
## 许可与版权
|
|
46
|
+
|
|
47
|
+
作者:[nzlov](https://github.com/nzlov)。
|
|
48
|
+
|
|
49
|
+
- 插件代码及原创 SVG 图标采用 [MIT 许可](LICENSE)。
|
|
50
|
+
- Material Color Utilities 采用 Apache-2.0,详见[第三方声明](THIRD_PARTY_NOTICES.md)及[附带许可](third-party/material-color-utilities-LICENSE.txt)。
|
|
51
|
+
- 图片版权归 Bing 及原摄影作者所有。插件许可不包含图片的再分发或商业使用授权,请遵守每张图片所附版权链接中的条款。
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
The prebuilt browser artifact includes **Material Color Utilities 0.3.0**
|
|
4
|
+
(`@material/material-color-utilities`), copyright 2021–2024 Google LLC and
|
|
5
|
+
contributors, licensed under the Apache License, Version 2.0.
|
|
6
|
+
|
|
7
|
+
Source: https://github.com/material-foundation/material-color-utilities
|
|
8
|
+
|
|
9
|
+
The utility implementation is bundled without source changes; the plugin maps
|
|
10
|
+
its HCT palettes to DSH theme roles and applies additional contrast adjustments.
|
|
11
|
+
The plugin’s custom Muted palette and DSH integration are separate additions.
|
|
12
|
+
|
|
13
|
+
The full Apache-2.0 license is included in
|
|
14
|
+
`third-party/material-color-utilities-LICENSE.txt`. Copyright and license notices
|
|
15
|
+
are also retained in the generated JavaScript artifact.
|
|
16
|
+
|
|
17
|
+
esbuild is a development-only build tool and is not part of the browser runtime.
|
|
18
|
+
Bing photographs remain owned by their respective rights holders.
|
package/bing-source.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// Intentionally dependency-free: each market uses only its fixed HTTPS Bing origin.
|
|
2
|
+
export const MARKET = 'zh-CN'
|
|
3
|
+
export const DAILY_PATH = '/bing-daily-theme/api/daily'
|
|
4
|
+
export const IMAGE_PATH = '/bing-daily-theme/api/image'
|
|
5
|
+
const BING_ORIGINS = new Map([
|
|
6
|
+
['zh-CN', 'https://cn.bing.com'],
|
|
7
|
+
['en-US', 'https://www.bing.com'],
|
|
8
|
+
])
|
|
9
|
+
|
|
10
|
+
export function isBingMarket(market) {
|
|
11
|
+
return BING_ORIGINS.has(market)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isSafeBingUrl(url) {
|
|
15
|
+
return [...BING_ORIGINS.values()].includes(url.origin) && !url.username && !url.password
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// No URLs, query strings, slashes, percent escapes, or alternate Bing resources.
|
|
19
|
+
export function isBingImageId(id) {
|
|
20
|
+
return typeof id === 'string' && id.length <= 240
|
|
21
|
+
&& /^OHR\.[A-Za-z0-9][A-Za-z0-9_-]*_1920x1080\.jpg$/.test(id)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class BingSourceError extends Error {}
|
|
25
|
+
|
|
26
|
+
function parseDate(value) {
|
|
27
|
+
if (typeof value !== 'string' || !/^\d{8}$/.test(value)) throw new BingSourceError('Invalid Bing date')
|
|
28
|
+
const date = `${value.slice(0, 4)}-${value.slice(4, 6)}-${value.slice(6, 8)}`
|
|
29
|
+
const parsed = new Date(`${date}T00:00:00Z`)
|
|
30
|
+
if (!Number.isFinite(+parsed) || parsed.toISOString().slice(0, 10) !== date) {
|
|
31
|
+
throw new BingSourceError('Invalid Bing date')
|
|
32
|
+
}
|
|
33
|
+
return date
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseArchive(bytes, market) {
|
|
37
|
+
const origin = BING_ORIGINS.get(market)
|
|
38
|
+
let image
|
|
39
|
+
try { image = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes)).images?.[0] } catch {
|
|
40
|
+
throw new BingSourceError('Invalid Bing archive JSON')
|
|
41
|
+
}
|
|
42
|
+
if (!image || typeof image.url !== 'string' || image.url.length > 2048) {
|
|
43
|
+
throw new BingSourceError('Missing Bing image')
|
|
44
|
+
}
|
|
45
|
+
let url
|
|
46
|
+
try { url = new URL(image.url, origin) } catch { throw new BingSourceError('Invalid Bing image URL') }
|
|
47
|
+
const id = url.searchParams.get('id')
|
|
48
|
+
if (!isSafeBingUrl(url) || url.pathname !== '/th'
|
|
49
|
+
|| url.hash || url.searchParams.getAll('id').length !== 1 || !isBingImageId(id)) {
|
|
50
|
+
throw new BingSourceError('Invalid Bing image ID')
|
|
51
|
+
}
|
|
52
|
+
let copyrightUrl = `${origin}/`
|
|
53
|
+
if (typeof image.copyrightlink === 'string' && image.copyrightlink.length <= 2048) {
|
|
54
|
+
try {
|
|
55
|
+
const link = new URL(image.copyrightlink, origin)
|
|
56
|
+
if (isSafeBingUrl(link)) copyrightUrl = link.href
|
|
57
|
+
} catch { /* Never pass an arbitrary or executable URL to the Client. */ }
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
id,
|
|
61
|
+
metadata: {
|
|
62
|
+
date: parseDate(image.startdate),
|
|
63
|
+
title: typeof image.title === 'string' ? image.title.slice(0, 300) : '',
|
|
64
|
+
copyright: typeof image.copyright === 'string' ? image.copyright.slice(0, 1000) : '',
|
|
65
|
+
copyrightUrl,
|
|
66
|
+
imageUrl: `${IMAGE_PATH}?${market === MARKET ? '' : `market=${market}&`}id=${encodeURIComponent(id)}`,
|
|
67
|
+
market,
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function readLimited(response, limit) {
|
|
73
|
+
const length = response.headers.get('content-length')
|
|
74
|
+
if (length !== null && (!/^\d+$/.test(length) || Number(length) > limit)) {
|
|
75
|
+
await response.body?.cancel().catch(() => {})
|
|
76
|
+
throw new BingSourceError('Bing response exceeds size limit')
|
|
77
|
+
}
|
|
78
|
+
if (!response.body) throw new BingSourceError('Empty Bing response')
|
|
79
|
+
const reader = response.body.getReader()
|
|
80
|
+
const chunks = []
|
|
81
|
+
let size = 0
|
|
82
|
+
try {
|
|
83
|
+
while (true) {
|
|
84
|
+
const { done, value } = await reader.read()
|
|
85
|
+
if (done) break
|
|
86
|
+
size += value.byteLength
|
|
87
|
+
if (size > limit) throw new BingSourceError('Bing response exceeds size limit')
|
|
88
|
+
chunks.push(value)
|
|
89
|
+
}
|
|
90
|
+
if (!size) throw new BingSourceError('Empty Bing response')
|
|
91
|
+
const bytes = new Uint8Array(size)
|
|
92
|
+
let offset = 0
|
|
93
|
+
for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.byteLength }
|
|
94
|
+
return bytes
|
|
95
|
+
} catch (error) {
|
|
96
|
+
await reader.cancel().catch(() => {})
|
|
97
|
+
throw error
|
|
98
|
+
} finally {
|
|
99
|
+
reader.releaseLock()
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Options are test seams, not plugin configuration. Production always uses defaults.
|
|
104
|
+
export function createBingSource({
|
|
105
|
+
fetchImpl = globalThis.fetch,
|
|
106
|
+
now = Date.now,
|
|
107
|
+
timeoutMs = 10_000,
|
|
108
|
+
metadataMaxBytes = 128 * 1024,
|
|
109
|
+
imageMaxBytes = 8 * 1024 * 1024,
|
|
110
|
+
refreshMs = 60 * 60 * 1000,
|
|
111
|
+
retryMs = 60 * 1000,
|
|
112
|
+
} = {}) {
|
|
113
|
+
let disposed = false
|
|
114
|
+
// No shared fallback or cache admission across markets, even for identical IDs.
|
|
115
|
+
const states = new Map([...BING_ORIGINS.keys()].map(market => [market, {
|
|
116
|
+
snapshot: undefined,
|
|
117
|
+
nextCheck: 0,
|
|
118
|
+
stale: false,
|
|
119
|
+
pending: undefined,
|
|
120
|
+
images: new Map(),
|
|
121
|
+
}]))
|
|
122
|
+
const controllers = new Set()
|
|
123
|
+
|
|
124
|
+
async function download(url, kind) {
|
|
125
|
+
if (disposed) throw new BingSourceError('Bing source disposed')
|
|
126
|
+
const controller = new AbortController()
|
|
127
|
+
controllers.add(controller)
|
|
128
|
+
const timer = setTimeout(() => controller.abort(new BingSourceError('Bing request timed out')), timeoutMs)
|
|
129
|
+
try {
|
|
130
|
+
const response = await fetchImpl(url, {
|
|
131
|
+
method: 'GET',
|
|
132
|
+
redirect: 'error',
|
|
133
|
+
credentials: 'omit',
|
|
134
|
+
signal: controller.signal,
|
|
135
|
+
headers: { accept: kind === 'image' ? 'image/jpeg' : 'application/json' },
|
|
136
|
+
})
|
|
137
|
+
if (!response.ok || response.redirected || (response.url && response.url !== url)) {
|
|
138
|
+
await response.body?.cancel().catch(() => {})
|
|
139
|
+
throw new BingSourceError('Bing upstream request failed')
|
|
140
|
+
}
|
|
141
|
+
const type = response.headers.get('content-type')?.split(';')[0].trim().toLowerCase()
|
|
142
|
+
if (kind === 'image' ? type !== 'image/jpeg' : !['application/json', 'text/javascript'].includes(type)) {
|
|
143
|
+
await response.body?.cancel().catch(() => {})
|
|
144
|
+
throw new BingSourceError('Unexpected Bing response type')
|
|
145
|
+
}
|
|
146
|
+
const bytes = await readLimited(response, kind === 'image' ? imageMaxBytes : metadataMaxBytes)
|
|
147
|
+
controller.signal.throwIfAborted()
|
|
148
|
+
if (kind === 'image' && !(bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff)) {
|
|
149
|
+
throw new BingSourceError('Invalid Bing JPEG')
|
|
150
|
+
}
|
|
151
|
+
return bytes
|
|
152
|
+
} finally {
|
|
153
|
+
clearTimeout(timer)
|
|
154
|
+
controllers.delete(controller)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function refresh(market, state) {
|
|
159
|
+
const origin = BING_ORIGINS.get(market)
|
|
160
|
+
try {
|
|
161
|
+
const archiveUrl = `${origin}/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=${market}`
|
|
162
|
+
const record = parseArchive(await download(archiveUrl, 'metadata'), market)
|
|
163
|
+
let image = state.images.get(record.id)
|
|
164
|
+
if (!image) {
|
|
165
|
+
// Archive URLs only admit an ID; never fetch their endpoint or parameters.
|
|
166
|
+
const url = `${origin}/th?id=${encodeURIComponent(record.id)}`
|
|
167
|
+
image = { bytes: await download(url, 'image'), contentType: 'image/jpeg' }
|
|
168
|
+
}
|
|
169
|
+
if (disposed) throw new BingSourceError('Bing source disposed')
|
|
170
|
+
// Atomic publication: a failed new download never replaces the old picture.
|
|
171
|
+
state.images.delete(record.id)
|
|
172
|
+
state.images.set(record.id, image)
|
|
173
|
+
while (state.images.size > 2) state.images.delete(state.images.keys().next().value)
|
|
174
|
+
state.snapshot = record.metadata
|
|
175
|
+
state.stale = false
|
|
176
|
+
state.nextCheck = now() + refreshMs
|
|
177
|
+
} catch (error) {
|
|
178
|
+
state.stale = true
|
|
179
|
+
state.nextCheck = now() + retryMs
|
|
180
|
+
if (disposed || !state.snapshot) throw error
|
|
181
|
+
}
|
|
182
|
+
return { ...state.snapshot, ...(state.stale ? { stale: true } : {}) }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
async daily(market = MARKET) {
|
|
187
|
+
if (disposed) throw new BingSourceError('Bing source disposed')
|
|
188
|
+
if (!isBingMarket(market)) throw new BingSourceError('Unsupported Bing market')
|
|
189
|
+
const state = states.get(market)
|
|
190
|
+
if (now() < state.nextCheck) {
|
|
191
|
+
if (!state.snapshot) throw new BingSourceError('Bing temporarily unavailable')
|
|
192
|
+
return { ...state.snapshot, ...(state.stale ? { stale: true } : {}) }
|
|
193
|
+
}
|
|
194
|
+
if (!state.pending) state.pending = refresh(market, state).finally(() => { state.pending = undefined })
|
|
195
|
+
return state.pending
|
|
196
|
+
},
|
|
197
|
+
image(id, market = MARKET) {
|
|
198
|
+
if (disposed || !isBingMarket(market) || !isBingImageId(id)) return undefined
|
|
199
|
+
// Even syntactically valid IDs must have been admitted by this market's archive.
|
|
200
|
+
return states.get(market).images.get(id)
|
|
201
|
+
},
|
|
202
|
+
dispose() {
|
|
203
|
+
disposed = true
|
|
204
|
+
for (const controller of controllers) controller.abort(new BingSourceError('Bing source disposed'))
|
|
205
|
+
controllers.clear()
|
|
206
|
+
for (const state of states.values()) {
|
|
207
|
+
state.images.clear()
|
|
208
|
+
state.snapshot = undefined
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
}
|
|
212
|
+
}
|
package/boot-theme.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Dependency-free bridge for the interval before Client plugins can run.
|
|
2
|
+
import { createWallpaperFilters, wallpaperFilters } from './wallpaper-filter.js';
|
|
3
|
+
|
|
4
|
+
export const PREFERENCES_KEY = 'dsh.bing-daily-theme.preferences.v1';
|
|
5
|
+
export const BOOT_CACHE_KEY = 'dsh.bing-daily-theme.boot.v1';
|
|
6
|
+
export const BOOT_STYLE_ID = 'bdt-boot-theme';
|
|
7
|
+
|
|
8
|
+
export function makeBootCache(settings, tokens, imageUrl) {
|
|
9
|
+
if (!settings.enabled || !settings.dynamic) return null;
|
|
10
|
+
const keys = {
|
|
11
|
+
background: '--dsw-alias-bg-layer-1', primary: '--dsw-alias-label-primary',
|
|
12
|
+
secondary: '--dsw-alias-label-secondary', tertiary: '--dsw-alias-label-tertiary',
|
|
13
|
+
border: '--dsw-alias-border-l2', brand: '--dsw-alias-brand-primary',
|
|
14
|
+
};
|
|
15
|
+
const colors = {};
|
|
16
|
+
for (const scheme of ['light', 'dark']) {
|
|
17
|
+
colors[scheme] = Object.fromEntries(Object.entries(keys).map(([role, key]) => [role, tokens[key][scheme]]));
|
|
18
|
+
}
|
|
19
|
+
return { version: 1, market: settings.market || 'zh-CN', paletteMode: settings.paletteMode, strength: settings.strength, colors, imageUrl };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// This pure function is also embedded verbatim in the Host's early script.
|
|
23
|
+
// All persisted values are untrusted: no arbitrary CSS or URL interpolation.
|
|
24
|
+
export function bootThemeCSS(settings, cache, now, systemDark, followDark, renderFilter = wallpaperFilters.css) {
|
|
25
|
+
settings ??= { enabled: true, dynamic: true, paletteMode: 'content', mode: 'schedule', day: '07:00', night: '19:00', strength: 28 };
|
|
26
|
+
if (!settings || settings.enabled !== true || settings.dynamic !== true || !cache || cache.version !== 1
|
|
27
|
+
|| cache.paletteMode !== settings.paletteMode || cache.strength !== settings.strength
|
|
28
|
+
|| !['zh-CN', 'en-US'].includes(settings.market ?? 'zh-CN') || (cache.market ?? 'zh-CN') !== (settings.market ?? 'zh-CN')
|
|
29
|
+
|| !Number.isFinite(settings.strength) || settings.strength < 10 || settings.strength > 100) return '';
|
|
30
|
+
// Follow stays selector-driven: structured row activation order may change
|
|
31
|
+
// after a Host plugin reload, and the native body selector is authoritative.
|
|
32
|
+
if (settings.mode === 'follow' && typeof followDark !== 'boolean') {
|
|
33
|
+
return bootThemeCSS(settings, cache, now, systemDark, false, renderFilter).replaceAll('[data-dsh-boot]', 'body:not([data-ds-dark-theme]) [data-dsh-boot]')
|
|
34
|
+
+ bootThemeCSS(settings, cache, now, systemDark, true, renderFilter).replaceAll('[data-dsh-boot]', 'body[data-ds-dark-theme] [data-dsh-boot]');
|
|
35
|
+
}
|
|
36
|
+
let dark;
|
|
37
|
+
if (settings.mode === 'schedule') {
|
|
38
|
+
const time = value => typeof value === 'string' && /^([01]\d|2[0-3]):[0-5]\d$/.test(value);
|
|
39
|
+
if (!time(settings.day) || !time(settings.night) || settings.day === settings.night) return '';
|
|
40
|
+
const minutes = value => Number(value.slice(0, 2)) * 60 + Number(value.slice(3));
|
|
41
|
+
const start = minutes(settings.day), end = minutes(settings.night);
|
|
42
|
+
const current = now.getHours() * 60 + now.getMinutes();
|
|
43
|
+
dark = !(start < end ? current >= start && current < end : current >= start || current < end);
|
|
44
|
+
} else if (settings.mode === 'system') dark = systemDark;
|
|
45
|
+
else if (settings.mode === 'follow') dark = followDark;
|
|
46
|
+
else if (settings.mode === 'light' || settings.mode === 'dark') dark = settings.mode === 'dark';
|
|
47
|
+
else return '';
|
|
48
|
+
const colors = cache.colors?.[dark ? 'dark' : 'light'];
|
|
49
|
+
const roles = ['background', 'primary', 'secondary', 'tertiary', 'border', 'brand'];
|
|
50
|
+
if (!colors || !roles.every(role => {
|
|
51
|
+
if (typeof colors[role] !== 'string') return false;
|
|
52
|
+
const match = /^rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)$/.exec(colors[role]);
|
|
53
|
+
return match && match.slice(1).every(channel => Number(channel) <= 255);
|
|
54
|
+
})) return '';
|
|
55
|
+
// The opaque backing also works when the in-memory image cache is empty.
|
|
56
|
+
// Never store blob URLs or request an arbitrary cached URL at startup.
|
|
57
|
+
const url = cache.imageUrl;
|
|
58
|
+
const alpha = Math.sqrt(1 - settings.strength / 100);
|
|
59
|
+
const matte = colors.background.replace('rgb(', 'rgba(').replace(')', `, ${alpha})`);
|
|
60
|
+
const imageMatch = typeof url === 'string' && url.length <= 300
|
|
61
|
+
&& /^\/bing-daily-theme\/api\/image\?(market=en-US&)?id=OHR\.[A-Za-z0-9][A-Za-z0-9_-]*_1920x1080\.jpg$/.exec(url);
|
|
62
|
+
const photo = imageMatch && Boolean(imageMatch[1]) === (settings.market === 'en-US') ? `url("${url}")` : '';
|
|
63
|
+
const shade = dark ? 'rgba(0, 0, 0, 0.62)' : 'rgba(255, 255, 255, 0.56)';
|
|
64
|
+
const image = photo ? `linear-gradient(${matte},${matte}),linear-gradient(${shade},${shade}),${photo}` : 'none';
|
|
65
|
+
const filter = renderFilter({ selector: '[data-dsh-boot]', image: photo, overlays: [matte, shade], settings });
|
|
66
|
+
return `[data-dsh-boot]{--dsw-alias-bg-base:${colors.background};--dsw-alias-label-primary:${colors.primary};--dsw-alias-label-secondary:${colors.secondary};--dsw-alias-label-tertiary:${colors.tertiary};--dsw-alias-border-l2:${colors.border};--dsw-alias-brand-primary:${colors.brand};background-color:${colors.background}!important;background-image:${image}!important;background-size:cover;background-position:center;color-scheme:${dark ? 'dark' : 'light'}}${filter}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// No network await, timer, observer, global preference or body-style writes.
|
|
70
|
+
function startBootTheme(preferencesKey, cacheKey, styleId, resolveCSS, createFilters) {
|
|
71
|
+
try {
|
|
72
|
+
const css = resolveCSS(JSON.parse(localStorage.getItem(preferencesKey)), JSON.parse(localStorage.getItem(cacheKey)),
|
|
73
|
+
new Date(), typeof matchMedia === 'function' && matchMedia('(prefers-color-scheme: dark)').matches, undefined, createFilters().css);
|
|
74
|
+
if (!css) return;
|
|
75
|
+
document.getElementById(styleId)?.remove();
|
|
76
|
+
const style = document.createElement('style');
|
|
77
|
+
style.id = styleId; style.textContent = css; document.head.append(style);
|
|
78
|
+
} catch { /* Storage denial or damaged cache must never block shell startup. */ }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function bootThemeInjection() {
|
|
82
|
+
const text = `;(${startBootTheme.toString()})(${JSON.stringify(PREFERENCES_KEY)},${JSON.stringify(BOOT_CACHE_KEY)},${JSON.stringify(BOOT_STYLE_ID)},${bootThemeCSS.toString()},${createWallpaperFilters.toString()});`;
|
|
83
|
+
return { kind: 'script', placement: 'body', text };
|
|
84
|
+
}
|