Greasy Fork is available in English.

webdav

坚果云

Ce script ne devrait pas être installé directement. C'est une librairie créée pour d'autres scripts. Elle doit être inclus avec la commande // @require https://update.greasyfork.org/scripts/463081/1191838/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. data: FileData,
  33. headers: header,
  34. url: url,
  35. dataType: DataType,
  36. onload: function (response) {
  37. if (response.status == 201 || response.status == 204) {
  38. complete(true)
  39. } else {
  40. console.error(response)
  41. complete(false)
  42. }
  43. },
  44. onerror: error
  45. })
  46. }
  47. )
  48. }
  49. DownloadFile(FolderName, FileName) {
  50. let url = `https://dav.jianguoyun.com/dav/${FolderName}/${FileName}`
  51. let type = "GET" // 上传
  52. let header = { "authorization": `Basic ${btoa(this.Account + ':' + this.Password)}` }
  53. return new Promise(
  54. (complete, error) => {
  55. GM_xmlhttpRequest({
  56. method: type,
  57. timeout: 3000,
  58. headers: header,
  59. url: url,
  60. onload: function (response) {
  61. if (response.status == 200) {
  62. complete(response.responseText)
  63. } else {
  64. console.error(response)
  65. complete(false)
  66. }
  67. },
  68. onerror: error,
  69. ontimeout: error
  70. })
  71. }
  72. )
  73. }
  74. GetAllFile(path, depth) {
  75. return new Promise((resolve, reject) => {
  76. GM_xmlhttpRequest({
  77. method: "PROPFIND",
  78. url: "https://dav.jianguoyun.com/dav/" + path,
  79. headers: {
  80. "Authorization": `Basic ${btoa(this.Account + ':' + this.Password)}`,
  81. "Depth": depth
  82. },
  83. onload: function (response) {
  84. if (response.status == 207) {
  85. var parser = new DOMParser();
  86. var xmlDoc = parser.parseFromString(response.responseText, "text/xml");
  87. var responses = xmlDoc.getElementsByTagNameNS("DAV:", "response");
  88. var urls = [];
  89. for (var i = 0; i < responses.length; i++) {
  90. var href = responses[i].getElementsByTagNameNS("DAV:", "href")[0].textContent;
  91. var propstat = responses[i].getElementsByTagNameNS("DAV:", "propstat")[0];
  92. var status = propstat.getElementsByTagNameNS("DAV:", "status")[0].textContent;
  93. if (status.includes("200 OK")) {
  94. var resourcetype = propstat.getElementsByTagNameNS("DAV:", "resourcetype")[0];
  95. if (resourcetype.getElementsByTagNameNS("DAV:", "collection").length > 0) {
  96. href += "/";
  97. }
  98. urls.push(href);
  99. }
  100. }
  101. resolve(urls);
  102. }
  103. else {
  104. console.error(response);
  105. reject(new Error("The request failed with status code " + response.status));
  106. }
  107. }
  108. });
  109. });
  110. }
  111. ExistsFile(path) {
  112. return new Promise((resolve, reject) => {
  113. GM_xmlhttpRequest({
  114. method: "HEAD",
  115. url: "https://dav.jianguoyun.com/dav/" + path,
  116. headers: {
  117. "Authorization": `Basic ${btoa(this.Account + ':' + this.Password)}`
  118. },
  119. onload: function (response) {
  120. var status = response.status;
  121. // 如果状态码是200,表示文件夹存在
  122. if (status == 200) {
  123. resolve(true)
  124. }
  125. // 如果状态码是404,表示文件夹不存在
  126. else if (status == 404) {
  127. resolve(false)
  128. } else if (status == 403) {
  129. resolve(true)//reject("权限不足,拒绝访问")
  130. }
  131. else {
  132. reject("The status code is " + status + " and the status text is " + response.statusText)
  133. }
  134. }
  135. });
  136. }
  137. )
  138. }
  139. AutoBack(fileName, Exclude = [], Callback) {
  140. let tim = new Date
  141. let format = `${tim.getFullYear()}-${tim.getMonth()}-${tim.getDate()}`
  142. let NewFileName = `${fileName}-${format}.json`
  143. if (GM_getValue('backup', null) != format) {
  144. var FileData = {}
  145. GM_listValues().forEach(function (value) {
  146. if (Exclude.indexOf(value) == -1) {
  147. FileData[value] = GM_getValue(value)
  148. }
  149. })
  150. FileData = JSON.stringify(FileData)
  151. let FilePath = "config/" + fileName
  152. new Promise(
  153. complete => {
  154. this.ExistsFile(FilePath).then(
  155. async response => {
  156. if (!response) {//not exist
  157. await this.NewFolder(FilePath)
  158. }
  159. console.log(FilePath, NewFileName, FileData, "json");
  160. let status = await this.UploadFiles(FilePath, NewFileName, FileData, "json")
  161. if (status) {
  162. console.log("上传备份成功");
  163. GM_setValue('backup', format)
  164. if (Callback != null) {//回调
  165. Callback.call({ webdav: this })
  166. }
  167. } else {
  168. console.log("上传备份失败");
  169. }
  170. complete(status)
  171. }
  172. )
  173. }
  174. )
  175. }
  176. }
  177. }