send_memos.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. <script src="js/util.js"></script>
  10. <style>
  11. #editor {
  12. height: 150px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <a href="https://memos.tianyunperfect.cn/">memos主页</a>
  18. <!--输入框,绑定回车事件,触发查询-->
  19. <input style="margin-left: 30px; width: 70vw; position: fixed;top: 10px" placeholder="搜索" type="text" id="search" onkeydown="if(event.keyCode===13) {search()}">
  20. <br><br>
  21. <div></div>
  22. <!-- 创建一个用于编辑的容器 -->
  23. <div id="editor"></div>
  24. <!-- 添加多选按钮 -->
  25. <div id="tags"></div>
  26. <br>
  27. <br>
  28. <!-- 添加发送按钮 -->
  29. <button id="log" style="float: right; height: 100px;width: 200px; font-size: 40px" onclick="sendData()">记录</button>
  30. <div id="memos_list">
  31. </div>
  32. <!-- 引入Quill库 -->
  33. <script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>
  34. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  35. <script>
  36. let authStr = "bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzA5MTc5NTUyfQ.LFxWB4efya1sL7VoJ42xpXxbAip-udT_Kx2OwZ8Y3-E";
  37. let myHeaders = {
  38. 'Content-type': 'application/json',
  39. 'Authorization': authStr
  40. };
  41. // console.log(location.href)
  42. // console.log(location.href.indexOf(tag))
  43. // 设置多选按钮 标签
  44. requestUtil.async('https://memos.tianyunperfect.cn/api/v1/tag', 'get', null, {'Authorization': authStr}).then(res => {
  45. // console.log(res); ['tag1','tag2']
  46. // 在 id tags 里面拼接res数组 ,示例todo标签 <input type="checkbox" id="todo" name="category" value="todo" checked> <label for="todo">todo</label>
  47. let tags = document.getElementById('tags');
  48. res.forEach(tag => {
  49. let input = document.createElement('input');
  50. // 如果是location.href 包含tag,则设置为选中状态, href 要反编译以识别中文
  51. if (decodeURI(location.href).indexOf(tag) !== -1) {
  52. input.checked = true;
  53. }
  54. input.type = 'checkbox';
  55. input.id = tag;
  56. input.name = 'category';
  57. input.value = tag;
  58. let label = document.createElement('label');
  59. // 设置margin-right 20
  60. label.style.marginRight = '10px';
  61. label.htmlFor = tag;
  62. label.innerText = tag;
  63. tags.appendChild(input);
  64. tags.appendChild(label);
  65. });
  66. search();
  67. // id="tags" 里面的多选按钮,点击后触发search
  68. let checkboxes = document.querySelectorAll('input[name="category"]');
  69. checkboxes.forEach(checkbox => {
  70. checkbox.onclick = function () {
  71. search();
  72. }
  73. });
  74. });
  75. const quill = new Quill('#editor', {
  76. theme: 'snow', // 指定使用的主题
  77. modules: {
  78. toolbar: [
  79. [{'header': [1, 2, false]}, 'bold', 'italic', 'blockquote', 'code-block', 'link'],
  80. [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}, 'image', 'video'],
  81. ]
  82. }
  83. });
  84. quill.focus();
  85. let contentStr = 'content';
  86. // 从localStorage中获取内容
  87. let item = localStorage.getItem(contentStr);
  88. if (item != null) {
  89. quill.root.innerHTML = item;
  90. }
  91. quill.on('text-change', function (delta, oldDelta, source) {
  92. localStorage.setItem(contentStr, quill.root.innerHTML);
  93. });
  94. async function convertToWebPAsync(base64Image) {
  95. // 检查图像格式是否为WebP
  96. if (base64Image.startsWith('data:image/webp')) {
  97. // 如果是WebP格式,直接返回原始的base64字符串
  98. return base64Image;
  99. } else {
  100. // 将图像转换为WebP格式
  101. const image = new Image();
  102. image.src = base64Image;
  103. await new Promise((resolve, reject) => {
  104. image.onload = resolve;
  105. image.onerror = reject;
  106. });
  107. const canvas = document.createElement('canvas');
  108. canvas.width = image.width;
  109. canvas.height = image.height;
  110. const ctx = canvas.getContext('2d');
  111. ctx.drawImage(image, 0, 0);
  112. const webpData = canvas.toDataURL('image/webp');
  113. return webpData;
  114. }
  115. }
  116. /**
  117. * 获取 markdown 格式的内容
  118. * @param delta
  119. * @returns {string}
  120. */
  121. async function deltaToMarkdown(delta) {
  122. let markdown = '';
  123. for (const op of delta['ops']) {
  124. if (op.insert) {
  125. if (typeof op.insert === 'string') {
  126. markdown += op.insert;
  127. } else if (op.insert.image) {
  128. // 如果是图片,转换为webp
  129. const webpBase64 = await convertToWebPAsync(op.insert.image);
  130. markdown += `![${op.insert.alt}](${webpBase64})`;
  131. }
  132. }
  133. }
  134. return markdown;
  135. }
  136. // 获取编辑器内容
  137. // function getContent() {
  138. // const content = quill.root.innerHTML;
  139. // console.log(content);
  140. // }
  141. // 发送数据
  142. async function sendData() {
  143. // 使id=log 不可用
  144. let logBtn = document.getElementById('log');
  145. logBtn.disabled = true;
  146. logBtn.innerText = '正在发送...';
  147. const content = await deltaToMarkdown(quill.getContents());
  148. const categories = getSelectedCategories();
  149. // 添加多选按钮选择的分类到内容前面
  150. let updatedContent = content;
  151. if (categories.length > 0) {
  152. updatedContent = `#${categories.join(' ')} ${content}`;
  153. }
  154. // 调用数据备份接口
  155. const data = {
  156. content: updatedContent
  157. };
  158. // 发送异步请求
  159. requestUtil.async('https://memos.tianyunperfect.cn/api/v1/memo', 'post', data, myHeaders).then(res => {
  160. // alert(JSON.stringify(res));
  161. if (res['name']) {
  162. // window.location.href = `https://memos.tianyunperfect.cn/m/${res['name']}`;
  163. showMsg('记录成功', 1);
  164. }
  165. logBtn.disabled = false;
  166. logBtn.innerText = '记录';
  167. quill.root.innerHTML = '';
  168. localStorage.removeItem(contentStr);
  169. });
  170. }
  171. // 获取选择的分类
  172. function getSelectedCategories() {
  173. const checkboxes = document.querySelectorAll('input[name="category"]:checked');
  174. const categories = [];
  175. checkboxes.forEach(checkbox => {
  176. categories.push(checkbox.value);
  177. });
  178. return categories;
  179. }
  180. function search() {
  181. let search = document.getElementById('search').value;
  182. // 如果标签不为空,则 拼接成 tag_str ,逗号分隔
  183. let tag_str = '';
  184. let checkboxes = document.querySelectorAll('input[name="category"]:checked');
  185. checkboxes.forEach(checkbox => {
  186. tag_str += checkbox.value + ',';
  187. });
  188. // https://web_history.tianyunperfect.cn/api/memos/list
  189. // get
  190. // 入参:search tag_str
  191. //
  192. // {'code': 200, 'res': [{'id': 11, 'resource_name': 'g2cyE6nXNCdqsm4ZH2R3Bs', 'creator_id': 1, 'created_ts': '2023-07-04T13:35:18', 'updated_ts': '2024-04-22T13:55:25', 'row_status': 'NORMAL', 'content': '康宁的女儿 易安 \n小名:气气\n#备份', 'visibility': 'PRIVATE'}]}
  193. let params = {'search': search, tag_str: tag_str}
  194. let url = 'https://web_history.tianyunperfect.cn/memos/list';
  195. // 拼接url
  196. let urlWithParams = requestUtil.buildUrl(url, params);
  197. requestUtil.async(urlWithParams, 'get', null, null).then(res => {
  198. // console.log(res);
  199. res = res['res'];
  200. let div = document.querySelector('#memos_list');
  201. div.innerHTML = '';
  202. res.forEach(item => {
  203. let tmpDiv = document.createElement('div');
  204. tmpDiv.innerText = item['content'];
  205. // 样式加上border,点击跳转到对应的url https://memos.tianyunperfect.cn/m/ + item['resource_name']
  206. tmpDiv.style.border = '1px solid #000';
  207. tmpDiv.style.margin = '10px';
  208. tmpDiv.style.padding = '10px';
  209. tmpDiv.style.cursor = 'pointer';
  210. tmpDiv.onclick = function () {
  211. window.open(`https://memos.tianyunperfect.cn/m/${item['resource_name']}`);
  212. };
  213. div.appendChild(tmpDiv);
  214. });
  215. });
  216. }
  217. </script>
  218. </body>
  219. </html>