Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新API CAT_fileStorage 提案 #127

Closed
CodFrm opened this issue Dec 15, 2022 · 1 comment
Closed

新API CAT_fileStorage 提案 #127

CodFrm opened this issue Dec 15, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@CodFrm
Copy link
Member

CodFrm commented Dec 15, 2022

调用此 API,可以操控脚本同步配置的文件储存源,将会在设定目录下创建一个app目录供此 API 使用.

API 定义如下

/**
 * 操控脚本同步配置的文件储存源,将会在同步目录下创建一个app/(uuid或者storageName)目录供此 API 使用
 * 当声明了@storageName时,将会将文件上传至指定的@storageName目录,多个脚本也可用此声明公用一个文件夹
 * 上传时默认覆盖同名文件
 * @param action 操作类型 list 列出指定目录所有文件, upload 上传文件, download 下载文件, delete 删除文件
 */
declare function CAT_fileStorage(
  action: "list",
  details: {
    path?: string;
    onload?: (files: CATType.FileStorageFileInfo[]) => void;
    onerror?: (error: FileStorageError) => void;
  }
): void;
declare function CAT_fileStorage(
  action: "download",
  details: {
    filename: string;
    onload: (data: Blob) => void;
    onprogress?: (progress: number) => void;
    onerror?: (error: FileStorageError) => void;
  }
): void;

declare function CAT_fileStorage(
  action: "delete",
  details: {
    filename: string;
    onload?: () => void;
    onerror?: (error: FileStorageError) => void;
  }
): void;

declare function CAT_fileStorage(
  action: "upload",
  details: {
    filename: string;
    data: Blob;
    onload?: () => void;
    onprogress?: (progress: number) => void;
    onerror?: (error: FileStorageError) => void;
  }
): void;

  interface FileStorageError {
    // 错误码 1 用户未配置文件储存源 2 文件储存源配置错误 3 路径不存在
    // 4 文件不存在 5 文件已存在 6 上传失败 7 下载失败 8 删除失败
    code: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
    msg: string;
  }
  interface FileStorageFileInfo {
    path: string;
    name: string;
    size: number;
    isDir: boolean;
  }

论坛帖子:
脚本猫新API CAT_fileStorage 提案
https://bbs.tampermonkey.net.cn/thread-3881-1-1.html
(出处: 油猴中文网)

@CodFrm CodFrm added the enhancement New feature or request label Dec 15, 2022
CodFrm added a commit that referenced this issue Dec 17, 2022
@CodFrm
Copy link
Member Author

CodFrm commented Dec 17, 2022

一个例子

// ==UserScript==
// @name         cat file storage
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.0
// @description  脚本同步储存空间操作
// @author       You
// @match        https://bbs.tampermonkey.net.cn/
// @grant        CAT_fileStorage
// ==/UserScript==

CAT_fileStorage("upload", {
	path: "test.txt",
	data: new Blob(["Hello World"]),
	onload() {
		CAT_fileStorage("list", {
			onload(list) {
				console.log(list);
				list.forEach(value => {
					if (value.name === "test.txt") {
						CAT_fileStorage("download", {
							file: value,
							async onload(data) {
								console.log(await data.text());
								CAT_fileStorage("delete", {
									path: value.name,
									onload() {
										console.log('ok');
									}
								});
							}
						});
					}
				});
			}
		})
	}
})

@CodFrm CodFrm closed this as completed Dec 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

1 participant