send_memos.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
  6. <title>memos记录</title>
  7. <!-- 引入Quill样式 -->
  8. <link rel="stylesheet" href="./js/quill.snow.css">
  9. <style>
  10. #editor {
  11. height: 150px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!-- 创建一个用于编辑的容器 -->
  17. <div id="editor"></div>
  18. <!-- 添加多选按钮 -->
  19. <input type="checkbox" id="work" name="category" value="工作" checked>
  20. <label for="work">工作</label>
  21. <input type="checkbox" id="diary" name="category" value="日记">
  22. <label for="diary">日记</label>
  23. <input type="checkbox" id="dream" name="category" value="梦记">
  24. <label for="dream">梦记</label>
  25. <input type="checkbox" id="daily" name="category" value="日常">
  26. <label for="daily">日常</label>
  27. <!-- 添加发送按钮 -->
  28. <button onclick="sendData()">发送</button>
  29. <!-- 引入Quill库 -->
  30. <script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>
  31. <script src="https://web.tianyunperfect.cn/simple/js/util.js"></script>
  32. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  33. <script>
  34. const quill = new Quill('#editor', {
  35. theme: 'snow', // 指定使用的主题
  36. modules: {
  37. toolbar: [
  38. [{'header': [1, 2, false]}, 'bold', 'italic', 'blockquote', 'code-block', 'link'],
  39. [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}, 'image', 'video'],
  40. ]
  41. }
  42. });
  43. async function convertToWebPAsync(base64Image) {
  44. // 检查图像格式是否为WebP
  45. if (base64Image.startsWith('data:image/webp')) {
  46. // 如果是WebP格式,直接返回原始的base64字符串
  47. return base64Image;
  48. } else {
  49. // 将图像转换为WebP格式
  50. const image = new Image();
  51. image.src = base64Image;
  52. await new Promise((resolve, reject) => {
  53. image.onload = resolve;
  54. image.onerror = reject;
  55. });
  56. const canvas = document.createElement('canvas');
  57. canvas.width = image.width;
  58. canvas.height = image.height;
  59. const ctx = canvas.getContext('2d');
  60. ctx.drawImage(image, 0, 0);
  61. const webpData = canvas.toDataURL('image/webp');
  62. return webpData;
  63. }
  64. }
  65. /**
  66. * 获取 markdown 格式的内容
  67. * @param delta
  68. * @returns {string}
  69. */
  70. async function deltaToMarkdown(delta) {
  71. let markdown = '';
  72. for (const op of delta['ops']) {
  73. if (op.insert) {
  74. if (typeof op.insert === 'string') {
  75. markdown += op.insert;
  76. } else if (op.insert.image) {
  77. // 如果是图片,转换为webp
  78. const webpBase64 = await convertToWebPAsync(op.insert.image);
  79. markdown += `![${op.insert.alt}](${webpBase64})`;
  80. }
  81. }
  82. }
  83. return markdown;
  84. }
  85. // 获取编辑器内容
  86. // function getContent() {
  87. // const content = quill.root.innerHTML;
  88. // console.log(content);
  89. // }
  90. // 发送数据
  91. async function sendData() {
  92. const content = await deltaToMarkdown(quill.getContents());
  93. const categories = getSelectedCategories();
  94. // 添加多选按钮选择的分类到内容前面
  95. let updatedContent = content;
  96. if (categories.length > 0) {
  97. updatedContent = `#${categories.join(' ')} ${content}`;
  98. }
  99. // 调用数据备份接口
  100. const data = {
  101. content: updatedContent
  102. };
  103. // 发送异步请求
  104. requestUtil.async('https://memos.tianyunperfect.cn/api/v1/memo', 'post', data, {
  105. 'Content-type': 'application/json',
  106. 'Authorization': 'bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNjk2Mjk3NjQwfQ.cUwdFFJp_Dv-MKy9BsrDPdjyxZrSaiPUqlMkdQEmJRE'
  107. }).then(res => {
  108. if (res['id']) {
  109. window.location.href = `https://memos.tianyunperfect.cn/m/${res['id']}`;
  110. }
  111. });
  112. }
  113. // 获取选择的分类
  114. function getSelectedCategories() {
  115. const checkboxes = document.querySelectorAll('input[name="category"]:checked');
  116. const categories = [];
  117. checkboxes.forEach(checkbox => {
  118. categories.push(checkbox.value);
  119. });
  120. return categories;
  121. }
  122. </script>
  123. </body>
  124. </html>