吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2369|回复: 6
收起左侧

[其他转载] 【js】webdav坚果云

 关闭 [复制链接]
梦汐 发表于 2023-4-2 06:53
const webdav = {
  Account: "坚果云账号",
  Password: "坚果云密匙",
  NewFolder: function (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: function (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,
                  timeout: 3000,
                  data: FileData,
                  headers: header,
                  url: url,
                  dataType: DataType,
                  onload: complete,
                  onerror: error,
                  ontimeout: error
              })
          }
      )
  },
  DownloadAFile: function (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: complete,
                  onerror: error,
                  ontimeout: error
              })
          }
      )
  },
  GetAllFile: function (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: function (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(false)
                      reject("权限不足,拒绝访问")
                  }
                  else {
                      reject("The status code is " + status + " and the status text is " + response.statusText)
                  }
              }
          });
      }
      )
  }
}
webdav.ExistsFile('debug').then(
  async r => {
      if (!r) {//不存在
          await webdav.NewFolder('debug')
      }
      console.log(await webdav.UploadFiles('debug', 'hi.js', "{js:'hi'}", "json"));
  }
)

免费评分

参与人数 3吾爱币 +3 热心值 +3 收起 理由
Coeic + 1 + 1 用心讨论,共获提升!
gorkys + 1 + 1 鼓励转贴优秀软件安全工具和文档!
windtrace + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

feiyu361 发表于 2023-4-2 09:33
弱弱的问下,这个怎么用?
 楼主| 梦汐 发表于 2023-4-2 07:07

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,
                    timeout: 3000,
                    data: FileData,
                    headers: header,
                    url: url,
                    dataType: DataType,
                    onload: complete,
                    onerror: error,
                    ontimeout: 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: complete,
                    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) => {
            console.log(this);
            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(false)
                        reject("权限不足,拒绝访问")
                    }
                    else {
                        reject("The status code is " + status + " and the status text is " + response.statusText)
                    }
                }
            });
        }
        )
    }
}
tt9527 发表于 2023-4-2 10:31
954995880 发表于 2024-2-29 11:32
点赞, 找了好就没找到, 感谢分享
Coeic 发表于 2024-3-1 17:29
楼主有详细教程吗?
alex701 发表于 2024-4-18 09:28
不会用,求下教程
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-1 02:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表