🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

坚果云上传接口

2.0

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/552701/1678021/%E5%9D%9A%E6%9E%9C%E4%BA%91%E4%B8%8A%E4%BC%A0%E6%8E%A5%E5%8F%A3.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

class webdav {
    constructor(account, password) {
        this.account = account;
        this.password = password;
        this.headers = { "authorization": `Basic ${btoa(this.account + ':' + this.password)}` };
    }

    create_folder(path) {
        let url = `https://dav.jianguoyun.com/dav/${path}/`
        let type = "MKCOL"
        return new Promise(
            (complete, error) => {
                GM_xmlhttpRequest({
                    method: type,
                    timeout: 3000,
                    headers: this.headers,
                    url: url,
                    onload: complete,
                    onerror: error,
                    ontimeout: error
                })
            }
        )
    }

    upload_file(path, name, data) {
        let url = `https://dav.jianguoyun.com/dav/${path}/${name}`
        let type = "PUT" // 上传
        return new Promise(
            (complete, error) => {
                GM_xmlhttpRequest({
                    method: type,
                    data: data,
                    headers: this.headers,
                    url: url,
                    onload: function (response) {
                        if (response.status == 201 || response.status == 204) {
                            complete(true);
                        } else {
                            console.error(response);
                            complete(false);
                        }
                    },
                    onerror: error,
                    ontimeout: error
                })
            }
        )

    }

    download_file(path, name) {
        let url = `https://dav.jianguoyun.com/dav/${path}/${name}`
        let type = "GET" // 下载
        return new Promise(
            (complete, error) => {
                GM_xmlhttpRequest({
                    method: type,
                    timeout: 3000,
                    headers: this.headers,
                    url: url,
                    onload: function (response) {
                        if (response.status == 200) {
                            complete(response.responseText)
                        } else {
                            console.error(response)
                            complete(false)
                        }
                    },
                    onerror: error,
                    ontimeout: error
                })
            }
        )

    }

    get_all_files(path, depth) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "PROPFIND",
                url: "https://dav.jianguoyun.com/dav/" + path,
                headers: {
                    "Authorization": `Basic ${btoa(this.account + ':' + this.password)}`,
                    "Depth": depth
                },
                onload: function (response) {
                    if (response.status == 207) {
                        const parser = new DOMParser();
                        const xml_doc = parser.parseFromString(response.responseText, "text/xml");
                        const responses = Array.from(xml_doc.getElementsByTagNameNS("DAV:", "response"));
                        const urls = responses
                            .map(res => {
                                const href = res.getElementsByTagNameNS("DAV:", "href")[0]?.textContent;
                                const propstat = res.getElementsByTagNameNS("DAV:", "propstat")[0];
                                const status = propstat?.getElementsByTagNameNS("DAV:", "status")[0]?.textContent || "";
                                if (!status.includes("200 OK")) return null;

                                const resourcetype = propstat.getElementsByTagNameNS("DAV:", "resourcetype")[0];
                                const is_collection = resourcetype?.getElementsByTagNameNS("DAV:", "collection").length > 0;
                                return href + (is_collection ? "/" : "");
                            }).filter(Boolean);
                        resolve(urls);
                    }
                    else {
                        console.error(response);
                        reject(new Error("The request failed with status code " + response.status));
                    }
                }
            });
        });
    }

    exists(path) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "HEAD",
                url: "https://dav.jianguoyun.com/dav/" + path,
                headers: this.headers,
                onload: function (response) {
                    const status = response.status;
                    if (status == 200) {
                        resolve(true);
                    }
                    else if (status == 404) {
                        resolve(false);
                    } else if (status == 403) {
                        resolve(false);
                    } else {
                        reject("The status code is " + status + " and the status text is " + response.statusText);
                    }
                }
            })
        })
    }

    run(file_name, filters, callback) {
        const time = `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()}`;
        const update_name = `${file_name}-${time}.json`;
        if (GM_getValue('UploadTiem', null) != time) {
            const ScriptData = {};
            GM_listValues().forEach((key) => {
                if (filters.indexOf(key) == -1) {
                    ScriptData[key] = GM_getValue(key);
                }
            })
            const update_data = JSON.stringify(ScriptData);
            const update_path = "application/" + file_name;
            this.exists(update_path).then(async r => {
                if (!r) { // 文件夹不存在则创建文件夹
                    await this.create_folder(update_path)
                }
                console.log(file_name, update_path, update_name, update_data);
                const status = await this.upload_file(update_path, update_name, update_data);
                if (status) {
                    console.log(file_name, "上传成功");
                    GM_setValue('UploadTiem', time);
                    if (callback != null) {//回调
                        callback.call(this);
                    }
                    return true;
                }
                return false;
            })
        }
    }
}