Greasy Fork

Greasy Fork is available in English.

webdav

坚果云

当前为 2023-04-02 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/463081/1169771/webdav.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

class webdav {
    constructor(Account, Password) {
        this.Account = Account
        this.Password = Password
    }
    NewFolder(FolderName) {
        let url = `https://dav.jianguoyun.com/dav/${FolderName}/`
        let type = "MKCOL" // 新建
        let header = { "authorization": `Basic ${btoa(this.Account + ':' + this.Password)}` }
        return new Promise(
            (complete, error) => {
                GM_xmlhttpRequest({
                    method: type,
                    timeout: 3000,
                    headers: header,
                    url: url,
                    onload: complete,
                    onerror: error,
                    ontimeout: error
                })
            }
        )
    }
    UploadFiles(FolderName, FileName, FileData, DataType) {
        let url = `https://dav.jianguoyun.com/dav/${FolderName}/${FileName}`
        let type = "PUT" // 上传
        let header = { "authorization": `Basic ${btoa(this.Account + ':' + this.Password)}` }
        return new Promise(
            (complete, error) => {
                GM_xmlhttpRequest({
                    method: type,
                    data: FileData,
                    headers: header,
                    url: url,
                    dataType: DataType,
                    onload: function (response) {
                        if (response.status == 201 || response.status == 204) {
                            complete(true)
                        } else {
                            console.error(response)
                            complete(false)
                        }
                    },
                    onerror: error
                })
            }
        )
    }
    DownloadFile(FolderName, FileName) {
        let url = `https://dav.jianguoyun.com/dav/${FolderName}/${FileName}`
        let type = "GET" // 上传
        let header = { "authorization": `Basic ${btoa(this.Account + ':' + this.Password)}` }
        return new Promise(
            (complete, error) => {
                GM_xmlhttpRequest({
                    method: type,
                    timeout: 3000,
                    headers: header,
                    url: url,
                    onload: function (response) {
                        if (response.status == 200) {
                            complete(response.responseText)
                        } else {
                            console.error(response)
                            complete(false)
                        }
                    },
                    onerror: error,
                    ontimeout: error
                })
            }
        )
    }
    GetAllFile(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) {
                        var parser = new DOMParser();
                        var xmlDoc = parser.parseFromString(response.responseText, "text/xml");
                        var responses = xmlDoc.getElementsByTagNameNS("DAV:", "response");
                        var urls = [];
                        for (var i = 0; i < responses.length; i++) {
                            var href = responses[i].getElementsByTagNameNS("DAV:", "href")[0].textContent;
                            var propstat = responses[i].getElementsByTagNameNS("DAV:", "propstat")[0];
                            var status = propstat.getElementsByTagNameNS("DAV:", "status")[0].textContent;
                            if (status.includes("200 OK")) {
                                var resourcetype = propstat.getElementsByTagNameNS("DAV:", "resourcetype")[0];
                                if (resourcetype.getElementsByTagNameNS("DAV:", "collection").length > 0) {
                                    href += "/";
                                }
                                urls.push(href);
                            }
                        }
                        resolve(urls);
                    }
                    else {
                        console.error(response);
                        reject(new Error("The request failed with status code " + response.status));
                    }
                }
            });
        });
    }
    ExistsFile(path) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "HEAD",
                url: "https://dav.jianguoyun.com/dav/" + path,
                headers: {
                    "Authorization": `Basic ${btoa(this.Account + ':' + this.Password)}`
                },
                onload: function (response) {
                    var status = response.status;
                    // 如果状态码是200,表示文件夹存在
                    if (status == 200) {
                        resolve(true)
                    }
                    // 如果状态码是404,表示文件夹不存在
                    else if (status == 404) {
                        resolve(false)
                    } else if (status == 403) {
                        resolve(true)//reject("权限不足,拒绝访问")
                    }
                    else {
                        reject("The status code is " + status + " and the status text is " + response.statusText)
                    }
                }
            });
        }
        )
    }
}
function AutoUploadFiles(Account, Password, FilePath, FileName, FileData = undefined, Exclude = []) {
    if (!FileData) {
        FileData = {}
        GM_listValues().forEach(function (value) {
            //console.log(Exclude.indexOf(value), value);
            if (Exclude.indexOf(value) == -1) {
                FileData[value] = GM_getValue(value)
            }
        })
        FileData = JSON.stringify(FileData)
    }
    return new Promise(
        complete => {
            let LBackupTimestamp = GM_getValue("LBackupTimestamp", 0)
            let NBackupTimestamp = Date.now()
            if (NBackupTimestamp - LBackupTimestamp > 24 * 60 * 60 * 1000) {
                console.log("FileData:", FileData)
                let dav = new webdav(Account, Password)
                dav.ExistsFile(FilePath).then(
                    async response => {
                        if (!response) {//not exist
                            await dav.NewFolder(FilePath)
                        }
                        let status = await dav.UploadFiles(FilePath, FileName, FileData, "json")
                        if (status) {
                            GM_setValue("LBackupTimestamp", NBackupTimestamp)
                        }else{
                            console.log("备份失败");
                        }
                        complete(status)
                    }
                )
            } else {
                complete("备份间隔不能小于24小时")
            }
        }
    )
}