CKWebDavStorage

Simple WebDAV storage provider

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/440068/1018737/CKWebDavStorage.js

  1. // ==UserScript==
  2. // @name CKWebDavStorage
  3. // @namespace ckylin-script-lib-webdav-storage
  4. // @version 1.0
  5. // @description Simple WebDAV storage provider
  6. // @match http://*/*
  7. // @match https://*/*
  8. // @require https://cdn.jsdelivr.net/npm/webdav@4.8.0/dist/web/webdav.js
  9. // @author CKylinMC
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant unsafeWindow
  13. // @license GPLv3 License
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. class CKWebDAVStorage {
  18. static fromConfig(options = {}) {
  19. const option = Object.assign({
  20. url: null,
  21. username: null,
  22. password: null,
  23. namespace: null,
  24. syncOnChange: true,
  25. pathByDots: true
  26. }, options);
  27. if (Object.values(option).includes(null)) {
  28. return false;
  29. }
  30. return new CKWebDAVStorage(...Object.values(option));
  31. }
  32. constructor(url, username, password, namespace, syncOnChange = true, pathByDots = true) {
  33. this.client = new window.WebDAV.createClient(url, {
  34. username,
  35. password
  36. });
  37. this.namespace = namespace || 'customdata' + Math.random().toString(36).substring(2, 15);
  38. this.namespace = this.namespace.replace(/[^a-zA-Z0-9]/g, '');
  39. console.warn("WEBDAV", "This caller not give a namespace, so use random string as namespace:", this.namespace);
  40. this.doc = null;
  41. this.syncOnChange = syncOnChange;
  42. this.pathByDots = pathByDots;
  43. }
  44. setSyncOnChange(syncOnChange) {
  45. this.syncOnChange = !!syncOnChange;
  46. }
  47. getSyncOnChange() {
  48. return this.syncOnChange;
  49. }
  50. getNamespace() {
  51. return this.namespace;
  52. }
  53. getFileName() {
  54. return this.getNamespace() + '.json';
  55. }
  56. async exists() {
  57. return await this.client.exists(this.getFileName());
  58. }
  59. async markmodified() {
  60. this.doc.timestamp = (new Date()).getTime();
  61. }
  62. async download() {
  63. try {
  64. return JSON.parse(await this.client.getFileContents("/" + this.getFileName(), {
  65. format: "text"
  66. }));
  67. } catch (e) {
  68. return {};
  69. }
  70. }
  71. async upload(doc) {
  72. await this.client.putFileContents("/" + this.getFileName(), JSON.stringify(doc), {
  73. overwrite: true
  74. });
  75. }
  76. async sync() {
  77. if (await this.exists()) {
  78. if (this.doc.timestamp) {
  79. let clouddoc = await this.download();
  80. if (clouddoc.timestamp && this.clouddoc.timestamp > this.doc.timestamp) {
  81. this.doc = clouddoc;
  82. } else {
  83. await this.upload(this.doc);
  84. }
  85. } else {
  86. this.doc = await this.download();
  87. }
  88. } else {
  89. if (this.doc) {
  90. this.markmodified();
  91. await this.upload(this.doc);
  92. } else {
  93. this.doc = {};
  94. this.markmodified();
  95. await this.upload(this.doc);
  96. }
  97. }
  98. }
  99. parseKey(key) {
  100. const keys = key.split(".").filter(it => it != null && (typeof it != "undefined") && it.length > 0);
  101. if (keys.length === 0) {
  102. throw new Error("Invalid key: " + key);
  103. }
  104. return keys;
  105. }
  106. set(key, value = null) {
  107. if (this.pathByDots && key.indexOf(".") > -1) {
  108. let keys = this.parseKey(key);
  109. let obj = this.doc;
  110. let length = keys.length;
  111. let i = 0;
  112. for (; i < length - 1; i++) {
  113. let k = keys[i];
  114. if (!obj.hasOwnProperty(k)) {
  115. obj[k] = {};
  116. obj = obj[k];
  117. } else {
  118. obj = obj[k];
  119. }
  120. }
  121. obj[keys[length - 1]] = value;
  122. } else this.doc[key] = value;
  123. this.markmodified();
  124. this.autoSync();
  125. }
  126. get(key) {
  127. if (this.pathByDots && key.indexOf(".") > -1) {
  128. let keys = this.parseKey(key);
  129. let obj = this.doc;
  130. for (let key of keys) {
  131. if (!obj.hasOwnProperty(key)) {
  132. return false;
  133. } else {
  134. obj = obj[key];
  135. }
  136. }
  137. return obj;
  138. }
  139. return this.doc[key];
  140. }
  141. remove(key) {
  142. if (this.pathByDots && key.indexOf(".") > -1) {
  143. let keys = this.parseKey(key);
  144. let obj = this.doc;
  145. let length = keys.length;
  146. let i = 0;
  147. for (; i < length - 1; i++) {
  148. let k = keys[i];
  149. if (!obj.hasOwnProperty(k)) {
  150. obj[k] = {};
  151. obj = obj[k];
  152. } else {
  153. obj = obj[k];
  154. }
  155. }
  156. delete obj[keys[length - 1]];
  157. } else delete this.doc[key];
  158. this.markmodified();
  159. this.autoSync();
  160. }
  161. exists(key) {
  162. return typeof this.get(key) != "undefined";
  163. }
  164. clear() {
  165. this.doc = {};
  166. this.markmodified();
  167. this.autoSync();
  168. }
  169. getAll() {
  170. return this.doc();
  171. }
  172. setAll(obj) {
  173. this.doc = {};
  174. for (let keyname of Object.keys(obj)) {
  175. if (obj.hasOwnProperty(keyname)) {
  176. this.doc[keyname] = obj[keyname];
  177. }
  178. }
  179. this.markmodified();
  180. this.autoSync();
  181. }
  182. async autoSync() {
  183. if (this.syncOnChange) {
  184. await this.sync();
  185. }
  186. }
  187. }
  188. window.CKWebDAVStorage = CKWebDAVStorage;
  189. if (typeof unsafeWindow != "undefined") unsafeWindow.CKWebDAVStorage = CKWebDAVStorage;
  190. })();