Skip to content

Commit 1d31aa5

Browse files
committed
init: 🎉 添加资源下载器封装工具
1 parent 902d5cc commit 1d31aa5

13 files changed

Lines changed: 6827 additions & 12 deletions

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
| [Common Right Click Tab](./apps/common/right-click-tab/README.md) | 右键超链接快速打开新标签页 | [🐒Greasy Fork](https://greasyfork.org/zh-CN/scripts/528494-common-right-click-tab) \| [🐱Script Cat](https://scriptcat.org/zh-CN/script-show-page/2869) |
1616
| [Common Right Click Copy](./apps/common/right-click-copy/README.md) | 右键快速复制/粘贴文本 | [🐒Greasy Fork](https://greasyfork.org/zh-CN/scripts/532794) \| [🐱Script Cat](https://scriptcat.org/zh-CN/script-show-page/3186) |
1717
| [Common QR Code](./apps/common/qr-code/README.md) | 图片二维码识别/生成工具 | [🐒Greasy Fork](https://greasyfork.org/zh-CN/scripts/538539) \| [🐱Script Cat](https://scriptcat.org/zh-CN/script-show-page/3559) |
18+
19+
## 📦 封装工具
20+
21+
| 名称 | 功能 | 使用 |
22+
| ---------------------------------------------- | -------------------------------------------- | ---------------------------------------------------------------- |
23+
| [downloader](./apps/libs/downloader/README.md) | 资源下载器(下载资源、Zip 压缩、下载到本地) | [🐱Script Cat](https://scriptcat.org/zh-CN/script-show-page/4893) |

apps/libs/downloader/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# downloader
2+
3+
## 1.0.1
4+
5+
### Patch Changes
6+
7+
- 添加进度回调
8+
9+
## 1.0.0
10+
11+
### Major Changes
12+
13+
- init

apps/libs/downloader/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Downloader
2+
3+
**资源下载器(下载资源、Zip 压缩、下载到本地)**
4+
5+
<a href="https://github.com/xiaohuohumax/userscripts/tree/main/apps/libs/downloader">
6+
<img src="https://img.shields.io/badge/GITHUB-项目地址-brightgreen?style=for-the-badge&logo=github" alt="项目地址" />
7+
</a>
8+
<a href="https://github.com/xiaohuohumax/userscripts/blob/main/LICENSE">
9+
<img src="https://img.shields.io/badge/MIT-开源协议-orange?style=for-the-badge&logo=github" alt="开源协议" />
10+
</a>
11+
<a href="https://github.com/xiaohuohumax/userscripts/blob/main/apps/libs/downloader/CHANGELOG.md">
12+
<img src="https://img.shields.io/badge/CHANGELOG-更新日志-blue?style=for-the-badge&logo=github" alt="更新日志" />
13+
</a>
14+
<a href="https://github.com/xiaohuohumax/userscripts/issues">
15+
<img src="https://img.shields.io/badge/issues-问题反馈-yellow?style=for-the-badge&logo=github" alt="问题反馈" />
16+
</a>
17+
18+
## 📖 使用方式
19+
20+
1. 添加 `// @require https://***/downloader.js` 库引用
21+
2. 添加 `// @grant GM_download` 下载权限
22+
3. 使用 `downloader` 方法下载资源
23+
24+
```typescript
25+
// ==UserScript==
26+
// ***
27+
// @require https://***/downloader.js?sha384-***
28+
// @grant GM_download
29+
// ==/UserScript==
30+
31+
(async () => {
32+
'use strict'
33+
// 异步调用
34+
await downloader({
35+
filename: 'index.zip', // 文件名
36+
resources: [ // 资源列表
37+
{ name: 'index.txt', url: location.href },
38+
],
39+
concurrency: 10, // 并发数
40+
onProgress(index) { // 下载进度回调
41+
console.log(`正在下载第 ${index + 1} 个资源`)
42+
},
43+
})
44+
})()
45+
```
46+
47+
## 🚨 免责声明
48+
49+
- 本脚本仅供学习交流使用
50+
- 请勿用于任何商业用途
51+
- 使用本脚本产生的任何后果由用户自行承担
52+
53+
## ♻ 其他说明
54+
55+
GreasyFork 或者 ScriptCat 回复不及时,问题反馈推荐直接在 Github 提 Issue。
56+
57+
**如果觉得本脚本对你有帮助,欢迎点个 ⭐ Star 支持一下!**

apps/libs/downloader/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "downloader",
3+
"type": "module",
4+
"version": "1.0.1",
5+
"private": true,
6+
"description": "资源下载器(下载资源、Zip 压缩、下载到本地)",
7+
"author": {
8+
"name": "xiaohuohumax",
9+
"url": "https://github.com/xiaohuohumax"
10+
},
11+
"license": "MIT",
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/xiaohuohumax/userscripts.git"
15+
},
16+
"keywords": [
17+
"typescript",
18+
"tampermonkey",
19+
"downloader"
20+
],
21+
"scripts": {
22+
"build": "vite build --emptyOutDir=false"
23+
},
24+
"dependencies": {
25+
"@zip.js/zip.js": "^2.8.11",
26+
"p-limit": "^7.2.0"
27+
},
28+
"devDependencies": {
29+
"@types/tampermonkey": "^5.0.5"
30+
}
31+
}

apps/libs/downloader/src/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { BlobWriter, HttpReader, ZipWriter } from '@zip.js/zip.js'
2+
import pLimit from 'p-limit'
3+
4+
export interface Resource {
5+
name: string
6+
url: string | URL
7+
}
8+
9+
export interface Options {
10+
filename: string
11+
resources: Resource[]
12+
concurrency?: number
13+
onProgress?: (index: number) => void
14+
}
15+
16+
export default async function downloader(options: Options): Promise<void> {
17+
const writer = new ZipWriter(new BlobWriter('application/zip'))
18+
const limit = pLimit(options.concurrency || 10)
19+
await Promise.all(options.resources.map((resource, index) => limit(() => {
20+
options.onProgress?.(index)
21+
return writer.add(resource.name, new HttpReader(resource.url))
22+
})))
23+
const blob = await writer.close()
24+
const url = URL.createObjectURL(blob)
25+
GM_download(url, options.filename)
26+
}

apps/libs/downloader/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../tsconfig.base.json"
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from 'vite'
2+
3+
export default defineConfig({
4+
build: {
5+
outDir: '../../../dist/',
6+
minify: false,
7+
lib: {
8+
entry: 'src/index.ts',
9+
formats: ['iife'],
10+
fileName: () => 'downloader.js',
11+
name: 'downloader',
12+
},
13+
},
14+
})

dist/bilibili-dynamic-block.user.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name Bilibili 动态卡片拦截(Bilibili Dynamic Block)
33
// @namespace xiaohuohumax/userscripts/bilibili-dynamic-block
4-
// @version 1.0.3
4+
// @version 1.0.4
55
// @author xiaohuohumax
66
// @description 此脚本可以依据规则拦截 Bilibili 动态卡片。
77
// @license MIT
@@ -56,7 +56,7 @@
5656
return debounced;
5757
};
5858
const ID = "bilibili-dynamic-block";
59-
const VERSION = "1.0.3";
59+
const VERSION = "1.0.4";
6060
const LAST_VERSION = 1;
6161
class Store {
6262
constructor() {

dist/common-right-click-copy.user.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name 右键快速复制/粘贴文本(Common Right Click Copy)
33
// @namespace xiaohuohumax/userscripts/common-right-click-copy
4-
// @version 1.1.0
4+
// @version 1.1.1
55
// @author xiaohuohumax
66
// @description 用户可以通过右键点击选中的文本,快速复制到剪贴板,然后在输入框中右键即可快速粘贴剪贴板的文本(PS:对应复制限制的网站暂不支持)。
77
// @license MIT
@@ -32,7 +32,7 @@
3232
var _GM_setClipboard = /* @__PURE__ */ (() => typeof GM_setClipboard != "undefined" ? GM_setClipboard : void 0)();
3333
var _GM_setValue = /* @__PURE__ */ (() => typeof GM_setValue != "undefined" ? GM_setValue : void 0)();
3434
const ID = "common-right-click-copy";
35-
const VERSION = "1.1.0";
35+
const VERSION = "1.1.1";
3636
const LAST_VERSION = 1;
3737
class Store {
3838
constructor() {

dist/common-right-click-tab.user.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name 右键超链接快速打开新标签页(Common Right Click Tab)
33
// @namespace xiaohuohumax/userscripts/common-right-click-tab
4-
// @version 1.3.0
4+
// @version 1.3.1
55
// @author xiaohuohumax
66
// @description 用户可以通过右键点击【普通链接、鼠标选中带链接的文字】等方式快速打开新标签页。效果类似于【Ctrl+左键】点击链接。
77
// @license MIT
@@ -489,7 +489,7 @@
489489
return returnValue;
490490
}
491491
const ID = "common-right-click-tab";
492-
const VERSION = "1.3.0";
492+
const VERSION = "1.3.1";
493493
const LAST_VERSION = 2;
494494
class Store {
495495
constructor() {

0 commit comments

Comments
 (0)