webdav

坚果云

目前为 2023-04-02 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/463081/1169729/webdav.js

  1. class webdav {
  2. constructor(Account, Password) {
  3. this.Account = Account
  4. this.Password = Password
  5. }
  6. NewFolder(FolderName) {
  7. let url = `https://dav.jianguoyun.com/dav/${FolderName}/`
  8. let type = "MKCOL" // 新建
  9. let header = { "authorization": `Basic ${btoa(this.Account + ':' + this.Password)}` }
  10. return new Promise(
  11. (complete, error) => {
  12. GM_xmlhttpRequest({
  13. method: type,
  14. timeout: 3000,
  15. headers: header,
  16. url: url,
  17. onload: complete,
  18. onerror: error,
  19. ontimeout: error
  20. })
  21. }
  22. )
  23. }
  24. UploadFiles(FolderName, FileName, FileData, DataType) {
  25. let url = `https://dav.jianguoyun.com/dav/${FolderName}/${FileName}`
  26. let type = "PUT" // 上传
  27. let header = { "authorization": `Basic ${btoa(this.Account + ':' + this.Password)}` }
  28. return new Promise(
  29. (complete, error) => {
  30. GM_xmlhttpRequest({
  31. method: type,
  32. timeout: 3000,
  33. data: FileData,
  34. headers: header,
  35. url: url,
  36. dataType: DataType,
  37. onload: function (response) {
  38. if (response.status == 201 || response.status == 204) {
  39. complete(true)
  40. } else {
  41. console.error(response)
  42. complete(false)
  43. }
  44. },
  45. onerror: error,
  46. ontimeout: error
  47. })
  48. }
  49. )
  50. }
  51. DownloadFile(FolderName, FileName) {
  52. let url = `https://dav.jianguoyun.com/dav/${FolderName}/${FileName}`
  53. let type = "GET" // 上传
  54. let header = { "authorization": `Basic ${btoa(this.Account + ':' + this.Password)}` }
  55. return new Promise(
  56. (complete, error) => {
  57. GM_xmlhttpRequest({
  58. method: type,
  59. timeout: 3000,
  60. headers: header,
  61. url: url,
  62. onload: function (response) {
  63. if (response.status == 200) {
  64. complete(response.responseText)
  65. } else {
  66. console.error(response)
  67. complete(false)
  68. }
  69. },
  70. onerror: error,
  71. ontimeout: error
  72. })
  73. }
  74. )
  75. }
  76. GetAllFile(path, depth) {
  77. return new Promise((resolve, reject) => {
  78. GM_xmlhttpRequest({
  79. method: "PROPFIND",
  80. url: "https://dav.jianguoyun.com/dav/" + path,
  81. headers: {
  82. "Authorization": `Basic ${btoa(this.Account + ':' + this.Password)}`,
  83. "Depth": depth
  84. },
  85. onload: function (response) {
  86. if (response.status == 207) {
  87. var parser = new DOMParser();
  88. var xmlDoc = parser.parseFromString(response.responseText, "text/xml");
  89. var responses = xmlDoc.getElementsByTagNameNS("DAV:", "response");
  90. var urls = [];
  91. for (var i = 0; i < responses.length; i++) {
  92. var href = responses[i].getElementsByTagNameNS("DAV:", "href")[0].textContent;
  93. var propstat = responses[i].getElementsByTagNameNS("DAV:", "propstat")[0];
  94. var status = propstat.getElementsByTagNameNS("DAV:", "status")[0].textContent;
  95. if (status.includes("200 OK")) {
  96. var resourcetype = propstat.getElementsByTagNameNS("DAV:", "resourcetype")[0];
  97. if (resourcetype.getElementsByTagNameNS("DAV:", "collection").length > 0) {
  98. href += "/";
  99. }
  100. urls.push(href);
  101. }
  102. }
  103. resolve(urls);
  104. }
  105. else {
  106. console.error(response);
  107. reject(new Error("The request failed with status code " + response.status));
  108. }
  109. }
  110. });
  111. });
  112. }
  113. ExistsFile(path) {
  114. return new Promise((resolve, reject) => {
  115. console.log(this);
  116. GM_xmlhttpRequest({
  117. method: "HEAD",
  118. url: "https://dav.jianguoyun.com/dav/" + path,
  119. headers: {
  120. "Authorization": `Basic ${btoa(this.Account + ':' + this.Password)}`
  121. },
  122. onload: function (response) {
  123. var status = response.status;
  124. // 如果状态码是200,表示文件夹存在
  125. if (status == 200) {
  126. resolve(true)
  127. }
  128. // 如果状态码是404,表示文件夹不存在
  129. else if (status == 404) {
  130. resolve(false)
  131. } else if (status == 403) {
  132. resolve(false)
  133. reject("权限不足,拒绝访问")
  134. }
  135. else {
  136. reject("The status code is " + status + " and the status text is " + response.statusText)
  137. }
  138. }
  139. });
  140. }
  141. )
  142. }
  143. }
  144. function AutoUploadFiles(Account, Password, FilePath, FileName, FileData = undefined) {
  145. if (!FileData) {
  146. FileData = {}
  147. GM_listValues().forEach(function (value, index, array) {
  148. FileData[value] = GM_getValue(value)
  149. })
  150. FileData = JSON.stringify(FileData)
  151. }
  152. return new Promise(
  153. complete => {
  154. let LBackupTimestamp = GM_getValue("LBackupTimestamp", 0)
  155. let NBackupTimestamp = Date.now()
  156. if (NBackupTimestamp - LBackupTimestamp > 24 * 60 * 60 * 1000) {
  157. console.log("FileData |", FileData)
  158. let dav = new webdav(Account, Password)
  159. dav.ExistsFile(FilePath).then(
  160. async response => {
  161. if (!response) {//not exist
  162. await dav.NewFolder(FilePath)
  163. }
  164. complete(await dav.UploadFiles(FilePath, FileName, FileData, "json"))
  165. GM_setValue("LBackupTimestamp", NBackupTimestamp)
  166. }
  167. )
  168. } else {
  169. complete("The backup interval cannot be less than 24 hours")
  170. }
  171. }
  172. )
  173. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址