Wilson@思源

目 录

用SQL查询数据并写入到剪切板

see https://ld246.com/article/1726473563881
js
//!js return (async () => { /////// 主逻辑区 /////// // sql查询 const sql = `select markdown from blocks where type='p' and markdown like '%工具%'`; const result = await query(sql); // 无数据提示 if(result.length === 0) return error('未找到任何数据'); // 过滤数据 const markdowns = result.map(row => row.markdown); // 写到剪切板 const clipboard = markdowns.join("\n\n"); await navigator.clipboard.writeText(clipboard); // 渲染结果 return render('查询结果已复制到剪切板'); /////// 功能函数区 /////// // 查询SQL函数 async function query(sql) { const result = await fetchSyncPost('/api/query/sql', { "stmt": sql }); if (result.code !== 0) { console.error("查询数据库出错", result.msg); return []; } return result.data; } // 渲染结果函数 function render(html, style) { onRender('.b3-form__space--small').then((container) => { container.style = style || 'color:var(--b3-card-info-color);'; container.innerHTML = html; }); return []; } // 渲染错误输出 function error(html, style) { return render(html, style || 'color:red;'); } // 监听dom渲染 function onRender(selector) { return new Promise(resolve => { const check = () => { let el = item.querySelector(selector); if (el) resolve(el); else requestAnimationFrame(check); }; check(); }); } })();