excalidraw.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // ==UserScript==
  2. // @name T-excalidraw-远程同步
  3. // @author tianyunperfect
  4. // @description 简介
  5. // @version 1.0.0
  6. // @update 2021-07-08 16:16:14
  7. // @include https://excalidraw.tianyunperfect.cn/?a=*
  8. // @require https://git.tianyunperfect.cn/tianyunperfect/web-base/raw/master/tmp/monkey/util.js?a=2022年06月26日21:06:37
  9. // @require https://cdn.bootcdn.net/ajax/libs/js-cookie/3.0.1/js.cookie.min.js
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js
  11. // ==/UserScript==
  12. (async () => {
  13. if (window.top !== window.self) {
  14. return;
  15. }
  16. let key = "excalidraw";
  17. let id = getQueryString("a");
  18. axios.post(`https://web_history.tianyunperfect.cn/excalidraw/get`, {
  19. "id": id,
  20. "json_str": "[]"
  21. }).then(async res => {
  22. // 远程数据
  23. let data = res.data;
  24. if (data.message) {
  25. if (data.message != localStorage.getItem(key)) {
  26. localStorage.setItem(key, data.message);
  27. location.reload();
  28. }
  29. } else {
  30. await axios.post(`https://web_history.tianyunperfect.cn/excalidraw/insert`, {
  31. "id": id,
  32. "json_str": "[]"
  33. })
  34. localStorage.setItem(key, "[]");
  35. location.reload();
  36. }
  37. let json_str = localStorage.getItem(key);
  38. // 同步数据
  39. while (true) {
  40. await sleep(1000);
  41. let json_new = localStorage.getItem(key);
  42. if (json_str != json_new) {
  43. json_str = json_new;
  44. await axios.post(`https://web_history.tianyunperfect.cn/excalidraw/update`, {
  45. "id": id,
  46. "json_str": json_new
  47. });
  48. }
  49. }
  50. })
  51. })();