dsh-paste-image 0.0.0-stage → 0.2.3
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/CHANGELOG.md +80 -0
- package/LICENSE +21 -0
- package/README.md +131 -2
- package/README.zh.md +132 -0
- package/assets/icon-180.png +0 -0
- package/client.js +747 -0
- package/cordis.patch.yml +5 -0
- package/lib/index.js +10 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +70 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.2.3] - 2026-11-18
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- One paste could insert the same image several times: every copy of the client bundle that had been loaded added another document-level listener, and the old `install()` returned a no-op disposer so nothing was ever removed. Five loaded copies produced five identical attachments. `install()` is now idempotent, returns a reference-counted, abort-based disposer that removes both listeners once the last holder releases them, and each source paste/drop event is claimed once, so duplicate listeners can no longer convert and re-route the same event twice.
|
|
14
|
+
|
|
15
|
+
### Tests
|
|
16
|
+
|
|
17
|
+
- 21 tests: the new one installs twice, asserts a single paste/drop listener pair, disposes, and installs again.
|
|
18
|
+
|
|
19
|
+
## [0.2.2] - 2026-11-18
|
|
20
|
+
|
|
21
|
+
### Added
|
|
22
|
+
|
|
23
|
+
- Plugin Manager display metadata: `locale/en.json` and `locale/zh.json` ship `meta.title` and `meta.description`, so plugin rows and the detail page describe the capability instead of falling back to the manifest name and README.
|
|
24
|
+
- A package-root `icon` (`./assets/icon-180.png`) and a `./locale/*.json` export; the icon and locales now travel inside the npm tarball.
|
|
25
|
+
- `.github/workflows/release.yml` publishes GitHub Releases to npm through OIDC trusted publishing, with no stored token.
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
|
|
29
|
+
- `dsh-paste-image@0.2.1` is the first npm release; both READMEs now lead with `dsh plugin --profile <profile> add dsh-paste-image` and keep the GitHub install as the no-registry alternative.
|
|
30
|
+
|
|
31
|
+
### Tests
|
|
32
|
+
|
|
33
|
+
- 20 tests: the four new ones assert both locale files carry usable `meta.title`/`meta.description`, the icon is a PNG under 256 KiB, and the locales plus icon are packaged and exported.
|
|
34
|
+
|
|
35
|
+
## [0.2.1] - 2026-11-18
|
|
36
|
+
|
|
37
|
+
### Fixed
|
|
38
|
+
|
|
39
|
+
- The release checklist in `docs/releasing.md` reported the 0.2.0 test count; it now matches the suite (16 tests).
|
|
40
|
+
|
|
41
|
+
## [0.2.0] - 2026-11-18
|
|
42
|
+
|
|
43
|
+
### Added
|
|
44
|
+
|
|
45
|
+
- Full BMP/DIB coverage in `decodeBmp()`: 1/4/8/16/24/32-bit pixels, `BI_RLE8`/`BI_RLE4`, `BI_BITFIELDS`/`BI_ALPHABITFIELDS`, `BITMAPCOREHEADER`, `BITMAPV2/V3/V4/V5HEADER`, OS/2 v2 headers and headerless DIBs (the `CF_DIB` shape).
|
|
46
|
+
- Embedded JPEG/PNG payloads (`BI_JPEG`/`BI_PNG`) are unwrapped and handed to the browser decoder.
|
|
47
|
+
- SVG pastes are rasterised through an `<img>` element; `viewBox` provides the size when `width`/`height` are missing.
|
|
48
|
+
- ICO/CUR fall back to the largest PNG-compressed directory entry when the browser cannot decode the icon.
|
|
49
|
+
- Magic-byte detection for AVIF, HEIC/HEIF, ICO/CUR, SVG and headerless DIBs.
|
|
50
|
+
- MIME alias table so `image/jpg`, `image/x-png`, `image/x-bmp`, `image/x-xpixmap`, `image/svg+xml` and friends are treated as the canonical type.
|
|
51
|
+
- `MAX_PIXELS` guard (64 Mi px) before a BMP/XPM canvas is allocated.
|
|
52
|
+
- `docs/formats.md`: the decision pipeline, the full format matrix, the BMP variant grid and the checklist for adding a format.
|
|
53
|
+
- `docs/releasing.md`: version policy, release checklist, npm publish steps and marketplace sync.
|
|
54
|
+
- Open-source scaffolding: `.github/CODEOWNERS`, issue-template contact links, README badges, and a Community section linking contributing, conduct, security, changelog and releases.
|
|
55
|
+
|
|
56
|
+
### Changed
|
|
57
|
+
|
|
58
|
+
- A native image with an empty declared type is re-wrapped with the sniffed type instead of being passed through and rejected by the host.
|
|
59
|
+
- Row stride, palette entries, bit masks and alpha handling in the BMP decoder are shared by every pixel depth instead of being hard-coded for 24/32-bit.
|
|
60
|
+
- BMP files using `BI_CMYK` compression (11/12/13) are rejected with `bmp: unsupported compression` instead of being decoded as RGB.
|
|
61
|
+
|
|
62
|
+
### Fixed
|
|
63
|
+
|
|
64
|
+
- 32-bit BMPs whose encoder leaves the alpha channel at zero no longer export as a fully transparent PNG.
|
|
65
|
+
- Palette BMP entries are loaded with the correct entry size (3 bytes for the OS/2 core header, 4 otherwise), and a zero alpha byte in a palette entry no longer makes that colour transparent.
|
|
66
|
+
- `cordis.patch.yml` is now part of the published `files` list. GitHub (and future npm) installs used to omit the loader patch, so the bundle entry never mounted; CI checks the tarball for the file.
|
|
67
|
+
|
|
68
|
+
## [0.1.0] - 2026-11-15
|
|
69
|
+
|
|
70
|
+
### Added
|
|
71
|
+
|
|
72
|
+
- Capture-phase `paste` and `drop` interception in the DSH renderer.
|
|
73
|
+
- Magic-byte detection for PNG, JPEG, GIF, WebP, BMP, TIFF and XPM, independent of the declared MIME type.
|
|
74
|
+
- Native pass-through for PNG, JPEG, WebP and GIF when the declared type agrees with the bytes.
|
|
75
|
+
- XPM3 text decoder (`decodeXpm`) and a 24/32-bit uncompressed BMP fallback decoder (`decodeBmp`).
|
|
76
|
+
- Canvas re-encoding to PNG with a 4096 px longest-side cap and `.png` file naming.
|
|
77
|
+
- Synthetic `drop` rerouting to `[data-composer-card]`, guarded by `__dshPasteImageRerouted`.
|
|
78
|
+
- `__ModuleLoader__` module registration with a `ctx.effect` lifecycle hook.
|
|
79
|
+
- Node test suite covering sniffing, candidate detection, BMP decoding, format-lie conversion and native pass-through.
|
|
80
|
+
- MIT license.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 pengtang1024
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,132 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-paste-image
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/pengtang1024/dsh-paste-image/actions/workflows/ci.yml)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://www.npmjs.com/package/dsh-paste-image)
|
|
6
|
+
[](package.json)
|
|
7
|
+
|
|
8
|
+
Paste or drop almost any image into DeepSeek Harness — even when the clipboard says `image/xpm` or lies about the MIME type.
|
|
9
|
+
|
|
10
|
+
## Why this exists
|
|
11
|
+
|
|
12
|
+
The DSH desktop client (Electron/Chromium) accepts exactly four attachment types: `image/png`, `image/jpeg`, `image/webp` and `image/gif`. Anything else gives you the red rejection:
|
|
13
|
+
|
|
14
|
+
> 仅支持 PNG、JPG、WebP、GIF
|
|
15
|
+
|
|
16
|
+
That whitelist breaks two things that happen all the time on Linux:
|
|
17
|
+
|
|
18
|
+
- **Screenshots declared as XPM.** Some screenshot tools, notably on Ubuntu/Wayland, put the image on the clipboard with the type `image/xpm`. The pixels are fine; the label is not on the whitelist, so the paste is rejected.
|
|
19
|
+
- **BMP bytes wearing a PNG label.** Some applications hand over a BMP (or TIFF) payload while declaring `image/png`. The declared type passes the whitelist check, the decoder later fails, and you get the same red error.
|
|
20
|
+
|
|
21
|
+
`dsh-paste-image` handles both cases inside the renderer: it looks at the actual bytes, decodes what the clipboard really contains, re-encodes it as PNG, and hands the result back to the normal attachment channel.
|
|
22
|
+
|
|
23
|
+
## How it works
|
|
24
|
+
|
|
25
|
+
1. **Capture.** `install()` registers `paste` and `drop` listeners on `document` in the capture phase, so the plugin sees the event before the composer does.
|
|
26
|
+
2. **Sniff.** `handle()` reads the `File` objects from `clipboardData` or `dataTransfer`, and `sniff()` identifies the real format from magic bytes — not from the declared MIME type.
|
|
27
|
+
3. **Re-encode.** `normalize()` lets native files through untouched. Everything else is decoded (dedicated XPM, BMP/DIB and SVG paths, the browser's own codec for the rest), drawn onto a canvas capped at 4096 px on the longest side, and exported as `image/png` with a `.png` file name.
|
|
28
|
+
4. **Reroute.** `handle()` dispatches a synthetic `drop` event carrying the new `File` at `[data-composer-card]`, which is the same path a real drag-and-drop takes. A `__dshPasteImageRerouted` marker stops the plugin from intercepting its own event.
|
|
29
|
+
|
|
30
|
+
All of this is client-side. There is no host process, no clipboard polling and no network request.
|
|
31
|
+
|
|
32
|
+
## Supported formats
|
|
33
|
+
|
|
34
|
+
This table is the short version. The [format support reference](docs/formats.md) explains the decision pipeline, the full BMP variant grid, and what is intentionally out of scope.
|
|
35
|
+
|
|
36
|
+
| Format | Typical declared type | What happens |
|
|
37
|
+
| ---------------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
|
38
|
+
| PNG | `image/png` | Passed through unchanged |
|
|
39
|
+
| JPEG / JFIF | `image/jpeg` | Passed through unchanged |
|
|
40
|
+
| WebP | `image/webp` | Passed through unchanged |
|
|
41
|
+
| GIF | `image/gif` | Passed through unchanged |
|
|
42
|
+
| XPM (XPM3 text) | `image/xpm` | Decoded by `decodeXpm()`, re-encoded as PNG |
|
|
43
|
+
| BMP / DIB | `image/bmp` | Browser decode first, `decodeBmp()` fallback: 1/4/8/16/24/32-bit, RLE4/RLE8, BITFIELDS, OS/2 headers and headerless DIBs |
|
|
44
|
+
| SVG | `image/svg+xml` | Rasterised through an `<img>` element, re-encoded as PNG |
|
|
45
|
+
| ICO / CUR | `image/x-icon` | Browser decode first, then falls back to the largest PNG-compressed entry |
|
|
46
|
+
| TIFF | `image/tiff` | Detected by magic bytes; decoded only if the browser ships a TIFF codec (Chromium does not), otherwise the original is passed through |
|
|
47
|
+
| AVIF | `image/avif` | Decoded by Chromium, re-encoded as PNG |
|
|
48
|
+
| HEIC / HEIF, other `image/*` | `image/...` | Detected by magic bytes; decoded only when the browser has the codec (Chromium does not decode HEIC), otherwise passed through |
|
|
49
|
+
|
|
50
|
+
Pass-through only happens when the magic bytes match one of the four native types **and** the declared type agrees. A file whose bytes are JPEG but which claims `image/png` is re-encoded as a real PNG, so the declared type can no longer lie. A native file with no declared type at all is re-wrapped with the sniffed type instead of being re-encoded, and common alias spellings (`image/jpg`, `image/x-png`, `image/x-bmp`, `image/x-xpixmap`, `image/svg+xml`, …) count as agreement.
|
|
51
|
+
|
|
52
|
+
A file is considered at all when its MIME type starts with `image/` or its name ends in `.png`, `.jpg`, `.jpeg`, `.jfif`, `.gif`, `.webp`, `.bmp`, `.dib`, `.tif`, `.tiff`, `.xpm`, `.avif`, `.ico`, `.cur`, `.svg`, `.heic` or `.heif`. Non-image files in the same paste are forwarded untouched.
|
|
53
|
+
|
|
54
|
+
## Install
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
dsh plugin --profile <profile> add dsh-paste-image
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The package ships prebuilt, so the install pulls eight small files through the npm registry. GitHub installs work too and skip the registry:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
dsh plugin --profile <profile> add github:pengtang1024/dsh-paste-image
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
For local development, point the profile at your checkout:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
dsh plugin --profile <profile> add file:/absolute/path/to/dsh-paste-image
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Replace `<profile>` with the profile your app runs. **Reload the page after installing** (`Ctrl+R`, or `Cmd+R` on macOS). The client module is only injected when the renderer loads, so a freshly installed plugin intercepts nothing until you reload.
|
|
73
|
+
|
|
74
|
+
## Verify it works
|
|
75
|
+
|
|
76
|
+
Run the repository checks:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
cd dsh-paste-image
|
|
80
|
+
npm test
|
|
81
|
+
npm run check
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`npm test` runs the Node test suite (21 tests at 0.2.3) covering magic-byte sniffing, candidate detection, every BMP variant, the BMP-labelled-PNG conversion, headerless DIBs and native pass-through. `npm run check` syntax-checks `client.js` and `lib/index.js`.
|
|
85
|
+
|
|
86
|
+
Then check the real flow:
|
|
87
|
+
|
|
88
|
+
1. Reload the DSH window.
|
|
89
|
+
2. Take a screenshot with your usual tool and paste it into the composer, or drop an image file onto it.
|
|
90
|
+
3. The attachment appears. Open DevTools (`Ctrl+Shift+I`) → Console, switch the level to **Verbose**, and look for a line like:
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
[dsh-paste-image] image/xpm -> image/png 1440x900
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
If conversion fails, the plugin keeps the original file and logs `keep original (...)` instead.
|
|
97
|
+
|
|
98
|
+
## Limitations
|
|
99
|
+
|
|
100
|
+
- **XPM:** XPM3 text format only. The parser reads the `c` colour key; other keys such as `m`, `g` and `s` are ignored. Colours are resolved by the browser's CSS parser, so palette names that are not valid CSS colours, such as the multi-word `light gray`, fall back to opaque black.
|
|
101
|
+
- **BMP:** 1/4/8/16/24/32-bit, `BI_RGB`, `BI_RLE8`, `BI_RLE4`, `BI_BITFIELDS` and `BI_ALPHABITFIELDS`, with core, extended, V4/V5 or OS/2 headers — plus headerless DIBs. 2-bit and 64-bit-per-pixel variants are not supported. For 32-bit files without a declared alpha mask, an all-zero alpha channel is treated as opaque instead of producing a fully transparent PNG.
|
|
102
|
+
- **Other codecs:** SVG is rasterised in-house; ICO/CUR fall back to PNG-compressed entries; AVIF relies on the browser decoder. TIFF and HEIC are detected but Chromium ships no codec for them, so the original is passed through and the host rejects it as before. No third-party decoder is bundled.
|
|
103
|
+
- **Size:** converted images are scaled down to at most 4096 px on the longest side. Native files passed through untouched are not resized.
|
|
104
|
+
- **File count:** one paste or drop may contain at most 8 files. Larger batches are left entirely to the native path.
|
|
105
|
+
- **Flattening:** a converted image becomes a single PNG frame. Animation, EXIF metadata and embedded colour profiles are not preserved.
|
|
106
|
+
- **Scope:** only the `paste` and `drop` paths are covered. The file picker and other insertion paths keep their native behaviour.
|
|
107
|
+
|
|
108
|
+
## Security and privacy
|
|
109
|
+
|
|
110
|
+
- Everything runs in the renderer. The plugin makes no network requests, uploads nothing and stores nothing.
|
|
111
|
+
- There is no background process, no clipboard polling and no change to DSH host code.
|
|
112
|
+
- Image bytes are read only from the paste or drop event, decoded locally, and handed back to the same window.
|
|
113
|
+
- The decoders are small, but they do process untrusted input. If you find a crash or a parsing bug, see [SECURITY.md](SECURITY.md).
|
|
114
|
+
|
|
115
|
+
## Documentation
|
|
116
|
+
|
|
117
|
+
- [How it works](docs/how-it-works.md) — event flow, decoder internals and a function-by-function code map.
|
|
118
|
+
- [Format support](docs/formats.md) — decision pipeline, format matrix, BMP variants and the checklist for adding a format.
|
|
119
|
+
- [Troubleshooting](docs/troubleshooting.md) — common failures and how to read the logs.
|
|
120
|
+
- [Releasing](docs/releasing.md) — version policy and the maintainer release checklist.
|
|
121
|
+
|
|
122
|
+
## Community
|
|
123
|
+
|
|
124
|
+
- [Contributing](CONTRIBUTING.md) — setup, tests, style and what a format contribution needs.
|
|
125
|
+
- [Code of Conduct](CODE_OF_CONDUCT.md) — Contributor Covenant 2.1.
|
|
126
|
+
- [Security policy](SECURITY.md) — private reporting, scope and supported versions.
|
|
127
|
+
- [Changelog](CHANGELOG.md) and [releases](https://github.com/pengtang1024/dsh-paste-image/releases) — what changed and why.
|
|
128
|
+
- [Issues](https://github.com/pengtang1024/dsh-paste-image/issues) — bug reports and format requests.
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
MIT © 2026 pengtang1024. See [LICENSE](LICENSE).
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# dsh-paste-image
|
|
2
|
+
|
|
3
|
+
[](https://github.com/pengtang1024/dsh-paste-image/actions/workflows/ci.yml)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://www.npmjs.com/package/dsh-paste-image)
|
|
6
|
+
[](package.json)
|
|
7
|
+
|
|
8
|
+
把几乎任何图片粘进或拖进 DeepSeek Harness —— 哪怕剪贴板声称它是 `image/xpm`,或者干脆把 MIME 类型写错了。
|
|
9
|
+
|
|
10
|
+
## 为什么需要它
|
|
11
|
+
|
|
12
|
+
DSH 桌面客户端(Electron/Chromium)的附件通道只接受四种类型:`image/png`、`image/jpeg`、`image/webp`、`image/gif`。其它类型一律报红:
|
|
13
|
+
|
|
14
|
+
> 仅支持 PNG、JPG、WebP、GIF
|
|
15
|
+
|
|
16
|
+
这份白名单会打断两种在 Linux 上很常见的场景:
|
|
17
|
+
|
|
18
|
+
- **截图被声明成 XPM。** 部分截图工具(尤其是 Ubuntu/Wayland 上)会把图片以 `image/xpm` 类型放进剪贴板。像素本身没问题,但这个类型不在白名单里,粘贴直接被拒。
|
|
19
|
+
- **BMP 字节披着 PNG 的皮。** 有些应用交上来的是 BMP(或 TIFF)数据,却声明成 `image/png`。声明类型过了白名单检查,后面解码失败,还是同一个红字。
|
|
20
|
+
|
|
21
|
+
`dsh-paste-image` 在渲染进程里把这两类问题一起解决:看真实字节、解出剪贴板里真正的内容、重编码成 PNG,再交回原生附件通道。
|
|
22
|
+
|
|
23
|
+
## 工作原理
|
|
24
|
+
|
|
25
|
+
1. **捕获。** `install()` 在 `document` 上以捕获阶段挂 `paste` 和 `drop` 监听,确保先于输入框自己的处理逻辑看到事件。
|
|
26
|
+
2. **嗅探。** `handle()` 从 `clipboardData` 或 `dataTransfer` 取出 `File`,`sniff()` 按魔数判断真实格式 —— 不看声明的 MIME 类型。
|
|
27
|
+
3. **重编码。** `normalize()` 对四种原生格式原样放行;其余先解码(XPM、BMP/DIB、SVG 走自带路径,其它交给浏览器),画到画布上(最长边超过 4096 会被缩放),再导出为 `image/png`,文件名改成 `.png`。
|
|
28
|
+
4. **回投。** `handle()` 在 `[data-composer-card]` 上派发一个携带新 `File` 的合成 `drop` 事件,和真实拖拽走同一条路径;`__dshPasteImageRerouted` 标记防止插件截获自己的事件。
|
|
29
|
+
|
|
30
|
+
全部逻辑都在客户端。没有宿主进程、不轮询剪贴板、不发网络请求。
|
|
31
|
+
|
|
32
|
+
## 支持的格式
|
|
33
|
+
|
|
34
|
+
下表是简版;完整决策流程、BMP 变体网格以及明确不做的格式,见[格式支持参考](docs/formats.md)。
|
|
35
|
+
|
|
36
|
+
| 格式 | 常见声明类型 | 处理方式 |
|
|
37
|
+
| ------------------------ | --------------- | --------------------------------------------------------------------------------------------------------- |
|
|
38
|
+
| PNG | `image/png` | 原样放行,不改写 |
|
|
39
|
+
| JPEG / JFIF | `image/jpeg` | 原样放行,不改写 |
|
|
40
|
+
| WebP | `image/webp` | 原样放行,不改写 |
|
|
41
|
+
| GIF | `image/gif` | 原样放行,不改写 |
|
|
42
|
+
| XPM(XPM3 文本) | `image/xpm` | 由 `decodeXpm()` 解码,重编码为 PNG |
|
|
43
|
+
| BMP / DIB | `image/bmp` | 先交给浏览器解码,失败则用 `decodeBmp()` 兜底:1/4/8/16/24/32 位、RLE4/RLE8、BITFIELDS、OS/2 头与无头 DIB |
|
|
44
|
+
| SVG | `image/svg+xml` | 通过 `<img>` 元素光栅化,重编码为 PNG |
|
|
45
|
+
| ICO / CUR | `image/x-icon` | 先交给浏览器解码,失败则取最大的 PNG 压缩条目 |
|
|
46
|
+
| TIFF | `image/tiff` | 能按魔数识别;仅当浏览器自带 TIFF 解码器时才解码(Chromium 没有),否则原样放行 |
|
|
47
|
+
| AVIF | `image/avif` | Chromium 能解码,重编码为 PNG |
|
|
48
|
+
| HEIC / HEIF 等 `image/*` | `image/...` | 能按魔数识别;浏览器有对应解码器才解码(Chromium 不解 HEIC),否则原样放行 |
|
|
49
|
+
|
|
50
|
+
只有在「魔数属于四种原生格式」**且**「声明类型与之一致」时才原样放行。字节是 JPEG 却声称 `image/png` 的文件会被重编码成真正的 PNG,声明类型从此骗不了人。声明类型为空的原生文件会按嗅探结果重新包一层 `File`(不重编码);`image/jpg`、`image/x-png`、`image/x-bmp`、`image/x-xpixmap`、`image/svg+xml` 等常见别名视为一致。
|
|
51
|
+
|
|
52
|
+
一个文件会被纳入处理,条件是 MIME 类型以 `image/` 开头,或文件名以 `.png`、`.jpg`、`.jpeg`、`.jfif`、`.gif`、`.webp`、`.bmp`、`.dib`、`.tif`、`.tiff`、`.xpm`、`.avif`、`.ico`、`.cur`、`.svg`、`.heic`、`.heif` 结尾。同一次粘贴里的非图片文件会原样转发。
|
|
53
|
+
|
|
54
|
+
## 安装
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
dsh plugin --profile <profile> add dsh-paste-image
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
包里是预构建产物,安装只从 npm 拉 8 个小文件。也可以直接从 GitHub 装、绕开 registry:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
dsh plugin --profile <profile> add github:pengtang1024/dsh-paste-image
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
本地开发时指向你的工作副本:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
dsh plugin --profile <profile> add file:/absolute/path/to/dsh-paste-image
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
把 `<profile>` 换成应用实际使用的 profile。**装完必须重载页面**(`Ctrl+R`,macOS 上是 `Cmd+R`):客户端模块只在渲染进程加载时注入,没重载之前插件不会拦截任何东西。
|
|
73
|
+
|
|
74
|
+
## 验证
|
|
75
|
+
|
|
76
|
+
先跑仓库自带检查:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
cd dsh-paste-image
|
|
80
|
+
npm test
|
|
81
|
+
npm run check
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`npm test` 跑 Node 测试(0.2.3 为 21 个用例):魔数嗅探、候选判定、各个 BMP 变体、BMP 冒充 PNG 的转码、无头 DIB、原生 PNG 放行。`npm run check` 对 `client.js` 和 `lib/index.js` 做语法检查。
|
|
85
|
+
|
|
86
|
+
再验证真实链路:
|
|
87
|
+
|
|
88
|
+
1. 重载 DSH 窗口。
|
|
89
|
+
2. 用平时的截图工具截图后粘进输入框,或把图片文件拖进去。
|
|
90
|
+
3. 附件正常出现。打开开发者工具(`Ctrl+Shift+I`)→ Console,把日志级别切到 **Verbose**,应能看到:
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
[dsh-paste-image] image/xpm -> image/png 1440x900
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
如果转码失败,插件会保留原文件并打印 `keep original (...)`。
|
|
97
|
+
|
|
98
|
+
## 限制
|
|
99
|
+
|
|
100
|
+
- **XPM:** 只支持 XPM3 文本格式。解析器只读 `c` 颜色键,`m`、`g`、`s` 等键会被忽略;颜色交给浏览器的 CSS 解析器,调色板里带空格的名字(如 `light gray`)不是合法 CSS,会退化为不透明黑色。
|
|
101
|
+
- **BMP:** 支持 1/4/8/16/24/32 位、`BI_RGB`/`BI_RLE8`/`BI_RLE4`/`BI_BITFIELDS`/`BI_ALPHABITFIELDS`、core/扩展头/V4/V5/OS/2 头,以及无文件头的 DIB。2 位和 64 位像素格式不支持。32 位图若没有声明 alpha 掩码,且整幅图 alpha 全为 0,会按不透明处理,不再导出全透明 PNG。
|
|
102
|
+
- **其它编解码器:** SVG 由插件自己光栅化;ICO/CUR 会回退到 PNG 压缩条目;AVIF 依赖浏览器解码器。TIFF 和 HEIC 能被识别,但 Chromium 没有对应解码器,会原样放行、宿主仍然拒绝。插件不捆绑任何第三方解码库。
|
|
103
|
+
- **尺寸:** 转码后的图片最长边会被缩到不超过 4096 px;原生放行的文件不做任何缩放。
|
|
104
|
+
- **数量:** 一次粘贴或拖入最多 8 个文件,超过就整批交回原生路径。
|
|
105
|
+
- **扁平化:** 转码结果是一帧 PNG,动画、EXIF 元数据和内嵌色彩配置都不会保留。
|
|
106
|
+
- **范围:** 只覆盖 `paste` 与 `drop`;文件选择器等其它入口保持原生行为。
|
|
107
|
+
|
|
108
|
+
## 安全与隐私
|
|
109
|
+
|
|
110
|
+
- 全部逻辑运行在渲染进程,不发网络请求、不上传、不落盘。
|
|
111
|
+
- 没有后台进程、不轮询剪贴板、不修改 DSH 宿主代码。
|
|
112
|
+
- 图片字节只从 paste 或 drop 事件读取,本地解码后交回同一窗口。
|
|
113
|
+
- 解码器很小,但确实在渲染进程里处理不可信输入。发现崩溃或解析问题请见 [SECURITY.md](SECURITY.md)。
|
|
114
|
+
|
|
115
|
+
## 文档
|
|
116
|
+
|
|
117
|
+
- [实现原理](docs/how-it-works.md) —— 事件流、解码器内部实现、逐函数代码地图。
|
|
118
|
+
- [格式支持](docs/formats.md) —— 决策流程、格式矩阵、BMP 变体与新增格式清单。
|
|
119
|
+
- [常见问题](docs/troubleshooting.md) —— 典型故障与日志查看方法。
|
|
120
|
+
- [发版流程](docs/releasing.md) —— 版本策略与维护者发布清单。
|
|
121
|
+
|
|
122
|
+
## 社区
|
|
123
|
+
|
|
124
|
+
- [贡献指南](CONTRIBUTING.md) —— 开发环境、测试、代码风格与格式贡献要求。
|
|
125
|
+
- [行为准则](CODE_OF_CONDUCT.md) —— Contributor Covenant 2.1。
|
|
126
|
+
- [安全策略](SECURITY.md) —— 私下报告、影响范围与支持版本。
|
|
127
|
+
- [更新日志](CHANGELOG.md) 与[发布记录](https://github.com/pengtang1024/dsh-paste-image/releases) —— 改了什么、为什么改。
|
|
128
|
+
- [Issues](https://github.com/pengtang1024/dsh-paste-image/issues) —— 缺陷报告与格式需求。
|
|
129
|
+
|
|
130
|
+
## 许可证
|
|
131
|
+
|
|
132
|
+
MIT © 2026 pengtang1024,见 [LICENSE](LICENSE)。
|
|
Binary file
|
package/client.js
ADDED
|
@@ -0,0 +1,747 @@
|
|
|
1
|
+
;(function () {
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
var NATIVE_TYPES = ['image/png', 'image/jpeg', 'image/webp', 'image/gif']
|
|
5
|
+
var NATIVE_SET = new Set(NATIVE_TYPES)
|
|
6
|
+
var NATIVE_EXT = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/webp': 'webp', 'image/gif': 'gif' }
|
|
7
|
+
var MIME_ALIAS = {
|
|
8
|
+
'image/jpg': 'image/jpeg',
|
|
9
|
+
'image/pjpeg': 'image/jpeg',
|
|
10
|
+
'image/x-png': 'image/png',
|
|
11
|
+
'image/x-bmp': 'image/bmp',
|
|
12
|
+
'image/x-ms-bmp': 'image/bmp',
|
|
13
|
+
'image/x-win-bitmap': 'image/bmp',
|
|
14
|
+
'image/vnd.microsoft.icon': 'image/x-icon',
|
|
15
|
+
'image/x-xpixmap': 'image/xpm',
|
|
16
|
+
'image/x-xpm': 'image/xpm',
|
|
17
|
+
'image/svg+xml': 'image/svg',
|
|
18
|
+
}
|
|
19
|
+
var IMAGE_EXT = /\.(png|jpe?g|jfif|gif|webp|bmp|dib|tiff?|xpm|avif|ico|cur|svg|heic|heif)$/i
|
|
20
|
+
var MAX_SIDE = 4096
|
|
21
|
+
var MAX_FILES = 8
|
|
22
|
+
var MAX_PIXELS = 64 * 1024 * 1024
|
|
23
|
+
var REROUTED = '__dshPasteImageRerouted'
|
|
24
|
+
var HANDLED = '__dshPasteImageHandled'
|
|
25
|
+
var INSTALLED = '__dshPasteImageInstalled'
|
|
26
|
+
var BMP_HEADERS = { 12: true, 40: true, 52: true, 56: true, 64: true, 108: true, 124: true }
|
|
27
|
+
var HEIC_BRANDS = { heic: true, heix: true, heis: true, heim: true, hevc: true, hevx: true, mif1: true, msf1: true }
|
|
28
|
+
var BMP_BPPS = { 1: true, 4: true, 8: true, 16: true, 24: true, 32: true }
|
|
29
|
+
var BMP_COMPRESSION = { 0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true }
|
|
30
|
+
|
|
31
|
+
function ascii(bytes, start, len) {
|
|
32
|
+
var out = ''
|
|
33
|
+
for (var i = start; i < start + len && i < bytes.length; i++) out += String.fromCharCode(bytes[i])
|
|
34
|
+
return out
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function unit32(bytes, offset) {
|
|
38
|
+
return (bytes[offset] | (bytes[offset + 1] << 8) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 24)) >>> 0
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function aliasMime(type) {
|
|
42
|
+
return MIME_ALIAS[type] || type
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 无文件头的 DIB(BITMAPINFOHEADER 起步,Windows CF_DIB 形状)。
|
|
46
|
+
function dibHeaderSize(bytes) {
|
|
47
|
+
if (bytes.length < 16) return 0
|
|
48
|
+
var size = unit32(bytes, 0)
|
|
49
|
+
if (!BMP_HEADERS[size]) return 0
|
|
50
|
+
if (size === 12) {
|
|
51
|
+
var width = bytes[4] | (bytes[5] << 8)
|
|
52
|
+
var height = bytes[6] | (bytes[7] << 8)
|
|
53
|
+
var planes = bytes[8] | (bytes[9] << 8)
|
|
54
|
+
var bpp = bytes[10] | (bytes[11] << 8)
|
|
55
|
+
return width > 0 && height > 0 && planes === 1 && (bpp === 1 || bpp === 4 || bpp === 8 || bpp === 24) ? size : 0
|
|
56
|
+
}
|
|
57
|
+
if (bytes.length < 20) return 0
|
|
58
|
+
var w = unit32(bytes, 4) | 0
|
|
59
|
+
var h = unit32(bytes, 8) | 0
|
|
60
|
+
var pl = bytes[12] | (bytes[13] << 8)
|
|
61
|
+
var bits = bytes[14] | (bytes[15] << 8)
|
|
62
|
+
var compression = unit32(bytes, 16)
|
|
63
|
+
if (!(w > 0) || h === 0 || pl !== 1 || !BMP_BPPS[bits]) return 0
|
|
64
|
+
if (compression !== 0 && compression !== 1 && compression !== 2 && compression !== 3 && compression !== 6) return 0
|
|
65
|
+
return w * Math.abs(h) > MAX_PIXELS ? 0 : size
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function looksLikeSvg(bytes) {
|
|
69
|
+
var head = new TextDecoder('utf-8', { fatal: false }).decode(bytes.subarray(0, Math.min(bytes.length, 1024)))
|
|
70
|
+
head = head.replace(/^\uFEFF/, '').replace(/^\s+/, '')
|
|
71
|
+
if (head.charAt(0) !== '<') return false
|
|
72
|
+
return /<svg[\s>]/i.test(head)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function sniff(bytes) {
|
|
76
|
+
if (
|
|
77
|
+
bytes.length >= 8 &&
|
|
78
|
+
bytes[0] === 0x89 &&
|
|
79
|
+
bytes[1] === 0x50 &&
|
|
80
|
+
bytes[2] === 0x4e &&
|
|
81
|
+
bytes[3] === 0x47 &&
|
|
82
|
+
bytes[4] === 0x0d &&
|
|
83
|
+
bytes[5] === 0x0a &&
|
|
84
|
+
bytes[6] === 0x1a &&
|
|
85
|
+
bytes[7] === 0x0a
|
|
86
|
+
)
|
|
87
|
+
return 'image/png'
|
|
88
|
+
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return 'image/jpeg'
|
|
89
|
+
if (bytes.length >= 6 && ascii(bytes, 0, 3) === 'GIF') return 'image/gif'
|
|
90
|
+
if (bytes.length >= 12 && ascii(bytes, 0, 4) === 'RIFF' && ascii(bytes, 8, 4) === 'WEBP') return 'image/webp'
|
|
91
|
+
if (bytes.length >= 2 && bytes[0] === 0x42 && bytes[1] === 0x4d) return 'image/bmp'
|
|
92
|
+
if (
|
|
93
|
+
bytes.length >= 4 &&
|
|
94
|
+
((bytes[0] === 0x49 && bytes[1] === 0x49 && bytes[2] === 0x2a && bytes[3] === 0) ||
|
|
95
|
+
(bytes[0] === 0x4d && bytes[1] === 0x4d && bytes[2] === 0 && bytes[3] === 0x2a))
|
|
96
|
+
)
|
|
97
|
+
return 'image/tiff'
|
|
98
|
+
if (bytes.length >= 9 && ascii(bytes, 0, 9).toLowerCase() === '/* xpm */') return 'image/xpm'
|
|
99
|
+
if (bytes.length >= 12 && ascii(bytes, 4, 4) === 'ftyp') {
|
|
100
|
+
var brand = ascii(bytes, 8, 4).toLowerCase()
|
|
101
|
+
if (brand === 'avif' || brand === 'avis') return 'image/avif'
|
|
102
|
+
if (HEIC_BRANDS[brand]) return 'image/heic'
|
|
103
|
+
}
|
|
104
|
+
if (bytes.length >= 4 && bytes[0] === 0 && bytes[1] === 0 && (bytes[2] === 1 || bytes[2] === 2) && bytes[3] === 0)
|
|
105
|
+
return 'image/x-icon'
|
|
106
|
+
if (looksLikeSvg(bytes)) return 'image/svg'
|
|
107
|
+
if (dibHeaderSize(bytes)) return 'image/bmp'
|
|
108
|
+
return null
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ---------- XPM ----------
|
|
112
|
+
|
|
113
|
+
function xpmStrings(text) {
|
|
114
|
+
var out = []
|
|
115
|
+
var re = /"((?:[^"\\]|\\.)*)"/g
|
|
116
|
+
var m
|
|
117
|
+
while ((m = re.exec(text)) !== null) {
|
|
118
|
+
out.push(m[1].replace(/\\"/g, '"').replace(/\\\\/g, '\\').replace(/\\n/g, '\n').replace(/\\t/g, '\t'))
|
|
119
|
+
}
|
|
120
|
+
return out
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 用 canvas 把任意 CSS 颜色解析成 RGBA,顺带支持命名色与 4/8/12 位十六进制。
|
|
124
|
+
function resolveColor(color) {
|
|
125
|
+
if (!color || color === 'None') return [0, 0, 0, 0]
|
|
126
|
+
var c = document.createElement('canvas')
|
|
127
|
+
c.width = 1
|
|
128
|
+
c.height = 1
|
|
129
|
+
var ctx = c.getContext('2d')
|
|
130
|
+
ctx.clearRect(0, 0, 1, 1)
|
|
131
|
+
ctx.fillStyle = '#000000'
|
|
132
|
+
ctx.fillStyle = color
|
|
133
|
+
var filled = ctx.fillStyle
|
|
134
|
+
if (
|
|
135
|
+
typeof filled === 'string' &&
|
|
136
|
+
filled === '#000000' &&
|
|
137
|
+
color.toLowerCase() !== '#000000' &&
|
|
138
|
+
color.toLowerCase() !== 'black'
|
|
139
|
+
) {
|
|
140
|
+
return null
|
|
141
|
+
}
|
|
142
|
+
ctx.fillRect(0, 0, 1, 1)
|
|
143
|
+
var d = ctx.getImageData(0, 0, 1, 1).data
|
|
144
|
+
return [d[0], d[1], d[2], d[3]]
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function decodeXpm(text) {
|
|
148
|
+
var strings = xpmStrings(text.replace(/^\uFEFF/, ''))
|
|
149
|
+
if (strings.length < 2) throw new Error('xpm: no strings')
|
|
150
|
+
var head = strings[0].trim().split(/\s+/).map(Number)
|
|
151
|
+
var width = head[0]
|
|
152
|
+
var height = head[1]
|
|
153
|
+
var colors = head[2]
|
|
154
|
+
var cpp = head[3] || 1
|
|
155
|
+
if (!(width > 0 && height > 0 && colors > 0)) throw new Error('xpm: bad header ' + strings[0])
|
|
156
|
+
if (width * height > MAX_PIXELS) throw new Error('xpm: too large')
|
|
157
|
+
|
|
158
|
+
var palette = new Map()
|
|
159
|
+
for (var i = 1; i <= colors; i++) {
|
|
160
|
+
var line = strings[i] || ''
|
|
161
|
+
var key = line.slice(0, cpp)
|
|
162
|
+
var rest = line.slice(cpp)
|
|
163
|
+
var m = /\bc\s+([^\s]+(?:\s+[^\s]+)?)/.exec(rest)
|
|
164
|
+
var raw = m ? m[1].trim() : '#000000'
|
|
165
|
+
palette.set(key, resolveColor(raw) || [0, 0, 0, 255])
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
var canvas = document.createElement('canvas')
|
|
169
|
+
canvas.width = width
|
|
170
|
+
canvas.height = height
|
|
171
|
+
var ctx = canvas.getContext('2d')
|
|
172
|
+
var image = ctx.createImageData(width, height)
|
|
173
|
+
var data = image.data
|
|
174
|
+
for (var y = 0; y < height; y++) {
|
|
175
|
+
var row = strings[1 + colors + y] || ''
|
|
176
|
+
for (var x = 0; x < width; x++) {
|
|
177
|
+
var k = row.slice(x * cpp, (x + 1) * cpp)
|
|
178
|
+
var rgba = palette.get(k) || [0, 0, 0, 0]
|
|
179
|
+
var o = (y * width + x) * 4
|
|
180
|
+
data[o] = rgba[0]
|
|
181
|
+
data[o + 1] = rgba[1]
|
|
182
|
+
data[o + 2] = rgba[2]
|
|
183
|
+
data[o + 3] = rgba[3]
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
ctx.putImageData(image, 0, 0)
|
|
187
|
+
return canvas
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// ---------- BMP / DIB ----------
|
|
191
|
+
|
|
192
|
+
function parseBmp(bytes) {
|
|
193
|
+
var dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
194
|
+
var file = bytes.length >= 18 && dv.getUint16(0, true) === 0x4d42
|
|
195
|
+
var p = file ? 14 : 0
|
|
196
|
+
if (bytes.length < p + 16) throw new Error('bmp: truncated header')
|
|
197
|
+
var headerSize = dv.getUint32(p, true)
|
|
198
|
+
if (!BMP_HEADERS[headerSize]) throw new Error('bmp: unsupported header ' + headerSize)
|
|
199
|
+
var info = {
|
|
200
|
+
headerSize: headerSize,
|
|
201
|
+
width: 0,
|
|
202
|
+
height: 0,
|
|
203
|
+
bpp: 0,
|
|
204
|
+
compression: 0,
|
|
205
|
+
clrUsed: 0,
|
|
206
|
+
imageSize: 0,
|
|
207
|
+
paletteStride: headerSize === 12 ? 3 : 4,
|
|
208
|
+
paletteOffset: p + headerSize,
|
|
209
|
+
masks: null,
|
|
210
|
+
offset: 0,
|
|
211
|
+
}
|
|
212
|
+
if (headerSize === 12) {
|
|
213
|
+
info.width = dv.getUint16(p + 4, true)
|
|
214
|
+
info.height = dv.getInt16(p + 6, true)
|
|
215
|
+
info.bpp = dv.getUint16(p + 10, true)
|
|
216
|
+
if (info.bpp !== 1 && info.bpp !== 4 && info.bpp !== 8 && info.bpp !== 24)
|
|
217
|
+
throw new Error('bmp: core header bpp ' + info.bpp)
|
|
218
|
+
} else {
|
|
219
|
+
info.width = dv.getInt32(p + 4, true)
|
|
220
|
+
info.height = dv.getInt32(p + 8, true)
|
|
221
|
+
info.bpp = dv.getUint16(p + 14, true)
|
|
222
|
+
info.compression = dv.getUint32(p + 16, true)
|
|
223
|
+
info.imageSize = dv.getUint32(p + 20, true)
|
|
224
|
+
info.clrUsed = dv.getUint32(p + 32, true)
|
|
225
|
+
// BI_CMYK and friends (11/12/13) are not decoded; refuse instead of guessing channel order.
|
|
226
|
+
if (!BMP_COMPRESSION[info.compression]) throw new Error('bmp: unsupported compression ' + info.compression)
|
|
227
|
+
}
|
|
228
|
+
if (!(info.width > 0) || info.height === 0) throw new Error('bmp: bad dimensions')
|
|
229
|
+
if (info.width * Math.abs(info.height) > MAX_PIXELS) throw new Error('bmp: too large')
|
|
230
|
+
if (!BMP_BPPS[info.bpp]) throw new Error('bmp: unsupported bpp ' + info.bpp)
|
|
231
|
+
|
|
232
|
+
var maskBytes = 0
|
|
233
|
+
if (info.compression === 3 || info.compression === 6) {
|
|
234
|
+
var mo = p + 40
|
|
235
|
+
if (mo + 12 > bytes.length) throw new Error('bmp: truncated masks')
|
|
236
|
+
info.masks = [dv.getUint32(mo, true), dv.getUint32(mo + 4, true), dv.getUint32(mo + 8, true)]
|
|
237
|
+
if (info.compression === 6 && mo + 16 <= bytes.length) info.masks.push(dv.getUint32(mo + 12, true))
|
|
238
|
+
if (headerSize === 40) maskBytes = info.compression === 6 ? 16 : 12
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (file) {
|
|
242
|
+
info.offset = dv.getUint32(10, true)
|
|
243
|
+
} else {
|
|
244
|
+
var paletteBytes = info.bpp <= 8 ? (info.clrUsed || 1 << info.bpp) * info.paletteStride : 0
|
|
245
|
+
info.offset = p + headerSize + maskBytes + paletteBytes
|
|
246
|
+
}
|
|
247
|
+
if (info.offset > bytes.length) throw new Error('bmp: bad pixel offset')
|
|
248
|
+
return info
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function maskChannel(mask) {
|
|
252
|
+
if (!mask) return null
|
|
253
|
+
var m = mask >>> 0
|
|
254
|
+
var shift = 0
|
|
255
|
+
while (shift < 32 && ((m >>> shift) & 1) === 0) shift++
|
|
256
|
+
if (shift === 32) return null
|
|
257
|
+
return { shift: shift, max: m >>> shift, mask: m }
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function maskValue(value, channel) {
|
|
261
|
+
if (!channel) return 0
|
|
262
|
+
var v = (value & channel.mask) >>> channel.shift
|
|
263
|
+
return channel.max === 255 ? v : Math.round((v * 255) / channel.max)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function bmpPalette(bytes, info) {
|
|
267
|
+
var palette = new Uint8Array(256 * 4)
|
|
268
|
+
for (var a = 0; a < 256; a++) palette[a * 4 + 3] = 255
|
|
269
|
+
var count = Math.min(info.bpp <= 8 ? info.clrUsed || 1 << info.bpp : 0, 256)
|
|
270
|
+
for (var i = 0; i < count; i++) {
|
|
271
|
+
var po = info.paletteOffset + i * info.paletteStride
|
|
272
|
+
if (po + 3 > bytes.length) break
|
|
273
|
+
palette[i * 4] = bytes[po + 2]
|
|
274
|
+
palette[i * 4 + 1] = bytes[po + 1]
|
|
275
|
+
palette[i * 4 + 2] = bytes[po]
|
|
276
|
+
if (info.paletteStride === 4 && bytes[po + 3]) palette[i * 4 + 3] = bytes[po + 3]
|
|
277
|
+
}
|
|
278
|
+
return palette
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function putIndexed(data, width, x, y, index, palette) {
|
|
282
|
+
var o = (y * width + x) * 4
|
|
283
|
+
data[o] = palette[index * 4]
|
|
284
|
+
data[o + 1] = palette[index * 4 + 1]
|
|
285
|
+
data[o + 2] = palette[index * 4 + 2]
|
|
286
|
+
data[o + 3] = palette[index * 4 + 3]
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function decodeRleBmp(bytes, info, data, width, height, topDown, palette) {
|
|
290
|
+
var rle4 = info.compression === 2
|
|
291
|
+
var x = 0
|
|
292
|
+
var y = 0
|
|
293
|
+
var i = info.offset
|
|
294
|
+
while (i + 1 < bytes.length) {
|
|
295
|
+
var count = bytes[i++]
|
|
296
|
+
var value = bytes[i++]
|
|
297
|
+
if (count > 0) {
|
|
298
|
+
for (var k = 0; k < count; k++) {
|
|
299
|
+
if (x < width && y < height) {
|
|
300
|
+
var idx = rle4 ? (k % 2 === 0 ? value >> 4 : value & 0x0f) : value
|
|
301
|
+
putIndexed(data, width, x, topDown ? y : height - 1 - y, idx, palette)
|
|
302
|
+
}
|
|
303
|
+
x++
|
|
304
|
+
}
|
|
305
|
+
} else if (value === 0) {
|
|
306
|
+
x = 0
|
|
307
|
+
y++
|
|
308
|
+
if (y >= height) break
|
|
309
|
+
} else if (value === 1) {
|
|
310
|
+
break
|
|
311
|
+
} else if (value === 2) {
|
|
312
|
+
if (i + 1 >= bytes.length) break
|
|
313
|
+
x += bytes[i++]
|
|
314
|
+
y += bytes[i++]
|
|
315
|
+
if (y >= height) break
|
|
316
|
+
} else {
|
|
317
|
+
var n = value
|
|
318
|
+
var used = 0
|
|
319
|
+
if (rle4) {
|
|
320
|
+
var pairs = (n + 1) >> 1
|
|
321
|
+
for (var j = 0; j < pairs && i < bytes.length; j++) {
|
|
322
|
+
var b = bytes[i++]
|
|
323
|
+
used++
|
|
324
|
+
if (x < width && y < height) {
|
|
325
|
+
putIndexed(data, width, x, topDown ? y : height - 1 - y, b >> 4, palette)
|
|
326
|
+
}
|
|
327
|
+
x++
|
|
328
|
+
if (j * 2 + 1 < n) {
|
|
329
|
+
if (x < width && y < height) {
|
|
330
|
+
putIndexed(data, width, x, topDown ? y : height - 1 - y, b & 0x0f, palette)
|
|
331
|
+
}
|
|
332
|
+
x++
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
for (var m = 0; m < n && i < bytes.length; m++) {
|
|
337
|
+
var v = bytes[i++]
|
|
338
|
+
used++
|
|
339
|
+
if (x < width && y < height) {
|
|
340
|
+
putIndexed(data, width, x, topDown ? y : height - 1 - y, v, palette)
|
|
341
|
+
}
|
|
342
|
+
x++
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (used & 1) i++
|
|
346
|
+
}
|
|
347
|
+
if (y >= height) break
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function decodeBmp(bytes) {
|
|
352
|
+
var info = parseBmp(bytes)
|
|
353
|
+
var dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
354
|
+
var width = info.width
|
|
355
|
+
var topDown = info.height < 0
|
|
356
|
+
var height = topDown ? -info.height : info.height
|
|
357
|
+
var bpp = info.bpp
|
|
358
|
+
if (info.compression === 4 || info.compression === 5) throw new Error('bmp: embedded payload')
|
|
359
|
+
|
|
360
|
+
var canvas = document.createElement('canvas')
|
|
361
|
+
canvas.width = width
|
|
362
|
+
canvas.height = height
|
|
363
|
+
var ctx = canvas.getContext('2d')
|
|
364
|
+
var image = ctx.createImageData(width, height)
|
|
365
|
+
var data = image.data
|
|
366
|
+
|
|
367
|
+
if (info.compression === 1 || info.compression === 2) {
|
|
368
|
+
decodeRleBmp(bytes, info, data, width, height, topDown, bmpPalette(bytes, info))
|
|
369
|
+
} else if (bpp <= 8) {
|
|
370
|
+
var palette = bmpPalette(bytes, info)
|
|
371
|
+
var stride = ((bpp * width + 31) >> 5) << 2
|
|
372
|
+
for (var y = 0; y < height; y++) {
|
|
373
|
+
var srcRow = info.offset + (topDown ? y : height - 1 - y) * stride
|
|
374
|
+
if (srcRow + stride > bytes.length) throw new Error('bmp: truncated pixels')
|
|
375
|
+
for (var x = 0; x < width; x++) {
|
|
376
|
+
var idx
|
|
377
|
+
if (bpp === 8) idx = bytes[srcRow + x]
|
|
378
|
+
else if (bpp === 4) idx = (bytes[srcRow + (x >> 1)] >> (x & 1 ? 0 : 4)) & 0x0f
|
|
379
|
+
else idx = (bytes[srcRow + (x >> 3)] >> (7 - (x & 7))) & 1
|
|
380
|
+
putIndexed(data, width, x, y, idx, palette)
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
} else if (bpp === 16) {
|
|
384
|
+
var masks16 = info.masks && info.masks[0] | info.masks[1] | info.masks[2] ? info.masks : [0x7c00, 0x03e0, 0x001f]
|
|
385
|
+
var rc = maskChannel(masks16[0])
|
|
386
|
+
var gc = maskChannel(masks16[1])
|
|
387
|
+
var bc = maskChannel(masks16[2])
|
|
388
|
+
var stride16 = ((16 * width + 31) >> 5) << 2
|
|
389
|
+
for (var y16 = 0; y16 < height; y16++) {
|
|
390
|
+
var row16 = info.offset + (topDown ? y16 : height - 1 - y16) * stride16
|
|
391
|
+
if (row16 + stride16 > bytes.length) throw new Error('bmp: truncated pixels')
|
|
392
|
+
for (var x16 = 0; x16 < width; x16++) {
|
|
393
|
+
var v16 = dv.getUint16(row16 + x16 * 2, true)
|
|
394
|
+
var o16 = (y16 * width + x16) * 4
|
|
395
|
+
data[o16] = maskValue(v16, rc)
|
|
396
|
+
data[o16 + 1] = maskValue(v16, gc)
|
|
397
|
+
data[o16 + 2] = maskValue(v16, bc)
|
|
398
|
+
data[o16 + 3] = 255
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
} else if (bpp === 24) {
|
|
402
|
+
var stride24 = ((24 * width + 31) >> 5) << 2
|
|
403
|
+
for (var y24 = 0; y24 < height; y24++) {
|
|
404
|
+
var row24 = info.offset + (topDown ? y24 : height - 1 - y24) * stride24
|
|
405
|
+
if (row24 + stride24 > bytes.length) throw new Error('bmp: truncated pixels')
|
|
406
|
+
for (var x24 = 0; x24 < width; x24++) {
|
|
407
|
+
var s24 = row24 + x24 * 3
|
|
408
|
+
var o24 = (y24 * width + x24) * 4
|
|
409
|
+
data[o24] = bytes[s24 + 2]
|
|
410
|
+
data[o24 + 1] = bytes[s24 + 1]
|
|
411
|
+
data[o24 + 2] = bytes[s24]
|
|
412
|
+
data[o24 + 3] = 255
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
} else {
|
|
416
|
+
var masks32 = info.masks && info.masks[0] | info.masks[1] | info.masks[2] ? info.masks : null
|
|
417
|
+
var r32 = masks32 ? maskChannel(masks32[0]) : null
|
|
418
|
+
var g32 = masks32 ? maskChannel(masks32[1]) : null
|
|
419
|
+
var b32 = masks32 ? maskChannel(masks32[2]) : null
|
|
420
|
+
var a32 = masks32 && masks32[3] ? maskChannel(masks32[3]) : null
|
|
421
|
+
var anyAlpha = false
|
|
422
|
+
for (var y32 = 0; y32 < height; y32++) {
|
|
423
|
+
var row32 = info.offset + (topDown ? y32 : height - 1 - y32) * width * 4
|
|
424
|
+
if (row32 + width * 4 > bytes.length) throw new Error('bmp: truncated pixels')
|
|
425
|
+
for (var x32 = 0; x32 < width; x32++) {
|
|
426
|
+
var s32 = row32 + x32 * 4
|
|
427
|
+
var o32 = (y32 * width + x32) * 4
|
|
428
|
+
if (masks32) {
|
|
429
|
+
var v32 = dv.getUint32(s32, true)
|
|
430
|
+
data[o32] = maskValue(v32, r32)
|
|
431
|
+
data[o32 + 1] = maskValue(v32, g32)
|
|
432
|
+
data[o32 + 2] = maskValue(v32, b32)
|
|
433
|
+
data[o32 + 3] = a32 ? maskValue(v32, a32) : 255
|
|
434
|
+
} else {
|
|
435
|
+
var a = bytes[s32 + 3]
|
|
436
|
+
data[o32] = bytes[s32 + 2]
|
|
437
|
+
data[o32 + 1] = bytes[s32 + 1]
|
|
438
|
+
data[o32 + 2] = bytes[s32]
|
|
439
|
+
data[o32 + 3] = a
|
|
440
|
+
if (a) anyAlpha = true
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
// 很多编码器把 32 位 BMP 的 alpha 全留成 0;整幅图都没有非零 alpha 时按不透明处理,
|
|
445
|
+
// 否则粘贴出来的 PNG 会整张透明。
|
|
446
|
+
if (!masks32 && !anyAlpha) for (var q = 3; q < data.length; q += 4) data[q] = 255
|
|
447
|
+
}
|
|
448
|
+
ctx.putImageData(image, 0, 0)
|
|
449
|
+
return canvas
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function bmpEmbedded(bytes) {
|
|
453
|
+
var info
|
|
454
|
+
try {
|
|
455
|
+
info = parseBmp(bytes)
|
|
456
|
+
} catch (error) {
|
|
457
|
+
return null
|
|
458
|
+
}
|
|
459
|
+
if (info.compression !== 4 && info.compression !== 5) return null
|
|
460
|
+
var start = info.offset
|
|
461
|
+
var length = info.imageSize || bytes.length - start
|
|
462
|
+
if (start + length > bytes.length) length = bytes.length - start
|
|
463
|
+
var payload = bytes.subarray(start, start + length)
|
|
464
|
+
var kind = sniff(payload)
|
|
465
|
+
return kind === 'image/png' || kind === 'image/jpeg' ? { type: kind, data: payload } : null
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// ---------- SVG / ICO ----------
|
|
469
|
+
|
|
470
|
+
function svgSize(text) {
|
|
471
|
+
var w = /<svg[^>]*\bwidth\s*=\s*["']\s*([\d.]+)/i.exec(text)
|
|
472
|
+
var h = /<svg[^>]*\bheight\s*=\s*["']\s*([\d.]+)/i.exec(text)
|
|
473
|
+
var box = /viewBox\s*=\s*["']\s*[-\d.]+[\s,]+[-\d.]+[\s,]+([\d.]+)[\s,]+([\d.]+)/i.exec(text)
|
|
474
|
+
return {
|
|
475
|
+
width: w ? Number(w[1]) : box ? Number(box[1]) : 0,
|
|
476
|
+
height: h ? Number(h[1]) : box ? Number(box[2]) : 0,
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function loadSvg(bytes) {
|
|
481
|
+
return new Promise(function (resolve, reject) {
|
|
482
|
+
if (typeof Image === 'undefined') throw new Error('svg: no Image')
|
|
483
|
+
var text = new TextDecoder('utf-8', { fatal: false }).decode(bytes)
|
|
484
|
+
var url = URL.createObjectURL(new Blob([text], { type: 'image/svg+xml' }))
|
|
485
|
+
var size = svgSize(text)
|
|
486
|
+
var img = new Image()
|
|
487
|
+
var finish = function (fn, value) {
|
|
488
|
+
URL.revokeObjectURL(url)
|
|
489
|
+
fn(value)
|
|
490
|
+
}
|
|
491
|
+
img.onload = function () {
|
|
492
|
+
finish(resolve, {
|
|
493
|
+
image: img,
|
|
494
|
+
width: img.naturalWidth || size.width || 300,
|
|
495
|
+
height: img.naturalHeight || size.height || 150,
|
|
496
|
+
})
|
|
497
|
+
}
|
|
498
|
+
img.onerror = function () {
|
|
499
|
+
finish(reject, new Error('svg: decode failed'))
|
|
500
|
+
}
|
|
501
|
+
img.src = url
|
|
502
|
+
})
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function icoPngEntry(bytes) {
|
|
506
|
+
if (bytes.length < 6) return null
|
|
507
|
+
var dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
508
|
+
if (dv.getUint16(0, true) !== 0 || dv.getUint16(2, true) !== 1) return null
|
|
509
|
+
var count = dv.getUint16(4, true)
|
|
510
|
+
var best = null
|
|
511
|
+
var bestArea = -1
|
|
512
|
+
for (var i = 0; i < count; i++) {
|
|
513
|
+
var e = 6 + i * 16
|
|
514
|
+
if (e + 16 > bytes.length) break
|
|
515
|
+
var w = bytes[e] || 256
|
|
516
|
+
var h = bytes[e + 1] || 256
|
|
517
|
+
var size = dv.getUint32(e + 8, true)
|
|
518
|
+
var off = dv.getUint32(e + 12, true)
|
|
519
|
+
if (!size || off + 8 > bytes.length || off + size > bytes.length) continue
|
|
520
|
+
if (sniff(bytes.subarray(off, off + 8)) !== 'image/png') continue
|
|
521
|
+
if (w * h > bestArea) {
|
|
522
|
+
bestArea = w * h
|
|
523
|
+
best = bytes.subarray(off, off + size)
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return best
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// ---------- 规范化 ----------
|
|
530
|
+
|
|
531
|
+
function canvasOf(source, width, height) {
|
|
532
|
+
var w = width || source.width || source.naturalWidth || 1
|
|
533
|
+
var h = height || source.height || source.naturalHeight || 1
|
|
534
|
+
var scale = Math.min(1, MAX_SIDE / Math.max(w, h))
|
|
535
|
+
var canvas = document.createElement('canvas')
|
|
536
|
+
canvas.width = Math.max(1, Math.round(w * scale))
|
|
537
|
+
canvas.height = Math.max(1, Math.round(h * scale))
|
|
538
|
+
canvas.getContext('2d').drawImage(source, 0, 0, canvas.width, canvas.height)
|
|
539
|
+
return canvas
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function toPngBlob(canvas) {
|
|
543
|
+
return new Promise(function (resolve, reject) {
|
|
544
|
+
canvas.toBlob(function (blob) {
|
|
545
|
+
blob ? resolve(blob) : reject(new Error('canvas.toBlob failed'))
|
|
546
|
+
}, 'image/png')
|
|
547
|
+
})
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
async function toCanvas(file, bytes, kind) {
|
|
551
|
+
if (kind === 'image/xpm') {
|
|
552
|
+
return canvasOf(decodeXpm(new TextDecoder('utf-8', { fatal: false }).decode(bytes)))
|
|
553
|
+
}
|
|
554
|
+
if (kind === 'image/svg') {
|
|
555
|
+
var svg = await loadSvg(bytes)
|
|
556
|
+
return canvasOf(svg.image, svg.width, svg.height)
|
|
557
|
+
}
|
|
558
|
+
var type = kind || aliasMime((file.type || '').toLowerCase()) || 'application/octet-stream'
|
|
559
|
+
try {
|
|
560
|
+
var bitmap = await createImageBitmap(new Blob([bytes], { type: type }))
|
|
561
|
+
return canvasOf(bitmap)
|
|
562
|
+
} catch (error) {
|
|
563
|
+
if (kind === 'image/bmp') {
|
|
564
|
+
var embedded = bmpEmbedded(bytes)
|
|
565
|
+
if (embedded) {
|
|
566
|
+
var inner = await createImageBitmap(new Blob([embedded.data], { type: embedded.type }))
|
|
567
|
+
return canvasOf(inner)
|
|
568
|
+
}
|
|
569
|
+
return canvasOf(decodeBmp(bytes))
|
|
570
|
+
}
|
|
571
|
+
if (kind === 'image/x-icon') {
|
|
572
|
+
var png = icoPngEntry(bytes)
|
|
573
|
+
if (!png) throw error
|
|
574
|
+
var icon = await createImageBitmap(new Blob([png], { type: 'image/png' }))
|
|
575
|
+
return canvasOf(icon)
|
|
576
|
+
}
|
|
577
|
+
throw error
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function fileName(file, ext) {
|
|
582
|
+
var base = (file.name || 'image').replace(/\.[^.]+$/, '')
|
|
583
|
+
return base + '.' + ext
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
async function normalize(file) {
|
|
587
|
+
var bytes = new Uint8Array(await file.arrayBuffer())
|
|
588
|
+
var kind = sniff(bytes)
|
|
589
|
+
var declared = aliasMime((file.type || '').toLowerCase())
|
|
590
|
+
if (kind && NATIVE_SET.has(kind)) {
|
|
591
|
+
if (declared === kind) return null
|
|
592
|
+
if (!declared) {
|
|
593
|
+
return {
|
|
594
|
+
file: new File([bytes], fileName(file, NATIVE_EXT[kind]), { type: kind }),
|
|
595
|
+
from: '(no type)',
|
|
596
|
+
to: kind,
|
|
597
|
+
width: 0,
|
|
598
|
+
height: 0,
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
var canvas = await toCanvas(file, bytes, kind)
|
|
603
|
+
var blob = await toPngBlob(canvas)
|
|
604
|
+
return {
|
|
605
|
+
file: new File([blob], fileName(file, 'png'), { type: 'image/png' }),
|
|
606
|
+
from: declared || '(no type)',
|
|
607
|
+
to: 'image/png',
|
|
608
|
+
width: canvas.width,
|
|
609
|
+
height: canvas.height,
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// ---------- 事件桥 ----------
|
|
614
|
+
|
|
615
|
+
function isImageCandidate(file) {
|
|
616
|
+
if (!file) return false
|
|
617
|
+
var type = (file.type || '').toLowerCase()
|
|
618
|
+
if (type.indexOf('image/') === 0) return true
|
|
619
|
+
return IMAGE_EXT.test(file.name || '')
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
function composer() {
|
|
623
|
+
return document.querySelector('[data-composer-card]') || document.body
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function reroute(files) {
|
|
627
|
+
var dt = new DataTransfer()
|
|
628
|
+
files.forEach(function (file) {
|
|
629
|
+
dt.items.add(file)
|
|
630
|
+
})
|
|
631
|
+
var event = new DragEvent('drop', { bubbles: true, cancelable: true, composed: true, dataTransfer: dt })
|
|
632
|
+
Object.defineProperty(event, REROUTED, { value: true })
|
|
633
|
+
composer().dispatchEvent(event)
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function claim(event) {
|
|
637
|
+
if (event[HANDLED]) return false
|
|
638
|
+
try {
|
|
639
|
+
Object.defineProperty(event, HANDLED, { value: true })
|
|
640
|
+
} catch (error) {
|
|
641
|
+
event[HANDLED] = true
|
|
642
|
+
}
|
|
643
|
+
return true
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
async function handle(event, data) {
|
|
647
|
+
if (!data || event[REROUTED]) return
|
|
648
|
+
var files = Array.prototype.slice.call(data.files || [])
|
|
649
|
+
if (files.length === 0 || files.length > MAX_FILES) return
|
|
650
|
+
if (!files.some(isImageCandidate)) return
|
|
651
|
+
if (!claim(event)) return
|
|
652
|
+
event.preventDefault()
|
|
653
|
+
event.stopPropagation()
|
|
654
|
+
var out = []
|
|
655
|
+
for (var i = 0; i < files.length; i++) {
|
|
656
|
+
var file = files[i]
|
|
657
|
+
if (!isImageCandidate(file)) {
|
|
658
|
+
out.push(file)
|
|
659
|
+
continue
|
|
660
|
+
}
|
|
661
|
+
try {
|
|
662
|
+
var converted = await normalize(file)
|
|
663
|
+
out.push(converted ? converted.file : file)
|
|
664
|
+
if (converted)
|
|
665
|
+
console.debug(
|
|
666
|
+
'[dsh-paste-image] ' +
|
|
667
|
+
converted.from +
|
|
668
|
+
' -> ' +
|
|
669
|
+
converted.to +
|
|
670
|
+
(converted.width ? ' ' + converted.width + 'x' + converted.height : ''),
|
|
671
|
+
)
|
|
672
|
+
} catch (error) {
|
|
673
|
+
out.push(file)
|
|
674
|
+
console.debug(
|
|
675
|
+
'[dsh-paste-image] keep original (' +
|
|
676
|
+
(file.type || 'no type') +
|
|
677
|
+
'): ' +
|
|
678
|
+
(error && error.message ? error.message : String(error)),
|
|
679
|
+
)
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
reroute(out)
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
function install() {
|
|
686
|
+
if (typeof document === 'undefined') return function () {}
|
|
687
|
+
var record = document[INSTALLED]
|
|
688
|
+
if (!record) {
|
|
689
|
+
var controller = new AbortController()
|
|
690
|
+
var options = { capture: true, signal: controller.signal }
|
|
691
|
+
document.addEventListener(
|
|
692
|
+
'paste',
|
|
693
|
+
function (event) {
|
|
694
|
+
void handle(event, event.clipboardData)
|
|
695
|
+
},
|
|
696
|
+
options,
|
|
697
|
+
)
|
|
698
|
+
document.addEventListener(
|
|
699
|
+
'drop',
|
|
700
|
+
function (event) {
|
|
701
|
+
void handle(event, event.dataTransfer)
|
|
702
|
+
},
|
|
703
|
+
options,
|
|
704
|
+
)
|
|
705
|
+
record = {
|
|
706
|
+
refs: 0,
|
|
707
|
+
release: function () {
|
|
708
|
+
record.refs -= 1
|
|
709
|
+
if (record.refs > 0) return
|
|
710
|
+
controller.abort()
|
|
711
|
+
delete document[INSTALLED]
|
|
712
|
+
},
|
|
713
|
+
}
|
|
714
|
+
Object.defineProperty(document, INSTALLED, { value: record, configurable: true })
|
|
715
|
+
}
|
|
716
|
+
record.refs += 1
|
|
717
|
+
return record.release
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
var api = {
|
|
721
|
+
sniff: sniff,
|
|
722
|
+
decodeXpm: decodeXpm,
|
|
723
|
+
decodeBmp: decodeBmp,
|
|
724
|
+
normalize: normalize,
|
|
725
|
+
isImageCandidate: isImageCandidate,
|
|
726
|
+
install: install,
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
if (typeof window !== 'undefined' && window.__ModuleLoader__ && typeof window.__ModuleLoader__.load === 'function') {
|
|
730
|
+
window.__ModuleLoader__.load({
|
|
731
|
+
id: 'dsh-paste-image',
|
|
732
|
+
factory: function () {
|
|
733
|
+
return {
|
|
734
|
+
apply: function (ctx) {
|
|
735
|
+
if (ctx && typeof ctx.effect === 'function') {
|
|
736
|
+
ctx.effect(install, 'paste-image')
|
|
737
|
+
return
|
|
738
|
+
}
|
|
739
|
+
install()
|
|
740
|
+
},
|
|
741
|
+
}
|
|
742
|
+
},
|
|
743
|
+
})
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
if (typeof module !== 'undefined' && module.exports) module.exports = api
|
|
747
|
+
})()
|
package/cordis.patch.yml
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host half of dsh-paste-image.
|
|
3
|
+
*
|
|
4
|
+
* The plugin works entirely inside the browser renderer: it intercepts pasted and
|
|
5
|
+
* dropped images, decodes the ones the Host refuses (BMP / XPM / TIFF / lying
|
|
6
|
+
* MIME types), re-encodes them as PNG and hands them back through a synthetic
|
|
7
|
+
* drop event. Nothing runs on the Host, so there is no polling and no child
|
|
8
|
+
* process.
|
|
9
|
+
*/
|
|
10
|
+
export function apply() {}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Paste any image",
|
|
4
|
+
"description": "Paste or drop an image whose declared type is XPM, BMP or DIB, SVG, ICO, or simply wrong: the renderer sniffs the real bytes and hands the composer a PNG. Native PNG, JPEG, WebP and GIF pass through untouched. One paste takes up to 8 files, and converted images keep their aspect ratio at 4096 px on the long side. HEIC and TIFF are recognised but left as they are, because the plugin ships no decoder for them."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-paste-image",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.3",
|
|
4
|
+
"description": "Paste any image into DeepSeek Harness: converts XPM/BMP/DIB/SVG/TIFF/etc. and MIME-lying images to real PNG in the renderer.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"dsh",
|
|
7
|
+
"dsh-plugin",
|
|
8
|
+
"deepseek-harness",
|
|
9
|
+
"clipboard",
|
|
10
|
+
"paste",
|
|
11
|
+
"image",
|
|
12
|
+
"xpm",
|
|
13
|
+
"bmp",
|
|
14
|
+
"dib",
|
|
15
|
+
"svg",
|
|
16
|
+
"png",
|
|
17
|
+
"wayland"
|
|
18
|
+
],
|
|
19
|
+
"private": false,
|
|
20
|
+
"icon": "./assets/icon-180.png",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "./lib/index.js",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": "./lib/index.js",
|
|
25
|
+
"./client": "./client.js",
|
|
26
|
+
"./locale/*.json": "./locale/*.json",
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"client.js",
|
|
31
|
+
"lib",
|
|
32
|
+
"locale/*.json",
|
|
33
|
+
"assets/icon-180.png",
|
|
34
|
+
"cordis.patch.yml",
|
|
35
|
+
"README.md",
|
|
36
|
+
"README.zh.md",
|
|
37
|
+
"LICENSE",
|
|
38
|
+
"CHANGELOG.md"
|
|
39
|
+
],
|
|
40
|
+
"dsh": {
|
|
41
|
+
"bundle": {
|
|
42
|
+
"patch": "./cordis.patch.yml"
|
|
43
|
+
},
|
|
44
|
+
"client": {
|
|
45
|
+
"platform": "web",
|
|
46
|
+
"inject": []
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "node --test tests/*.test.mjs",
|
|
51
|
+
"check": "node --check client.js && node --check lib/index.js",
|
|
52
|
+
"format": "npx --yes prettier@3 --write .",
|
|
53
|
+
"format:check": "npx --yes prettier@3 --check .",
|
|
54
|
+
"prepublishOnly": "npm test && npm run check"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
},
|
|
59
|
+
"repository": {
|
|
60
|
+
"type": "git",
|
|
61
|
+
"url": "git+https://github.com/pengtang1024/dsh-paste-image.git"
|
|
62
|
+
},
|
|
63
|
+
"bugs": {
|
|
64
|
+
"url": "https://github.com/pengtang1024/dsh-paste-image/issues"
|
|
65
|
+
},
|
|
66
|
+
"homepage": "https://github.com/pengtang1024/dsh-paste-image#readme",
|
|
67
|
+
"author": "pengtang1024",
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=20"
|
|
70
|
+
},
|
|
71
|
+
"license": "MIT"
|
|
72
|
+
}
|