写了一段 JS 代码,获取配置文件的 UserDate,并写入到指定的内容块(示例是:20250510110212-edgmnmn),用 runJS 插件能跑通,想让它定时运行,粘贴到代码片段中好像不会自动运行,请问是哪里不对嘛?有没有办法实现这个功能?
const fs = require('fs');
const path = require('path');
const Dir= window.siyuan.config.system.confDir + '\\conf.json'
// 构建完整文件路径
const filePath = path.resolve(__dirname, 'C:/Users/Chanson/Downloads/SiYuan/conf/conf.json'); //自己的备注,将路径转成win格式
const absolutePath = 'C:\Users\/Chanson\/Downloads\/SiYuan\/conf\/conf.json'; // 双反斜杠转义 这里应该写错了,应该是\\,因为单反斜杠(\)是转义 'C:\Users\\Chanson\\Downloads\\SiYuan\\conf\\conf.json'
const absolutePaths = 'C:/Users/Chanson/Downloads/SiYuan/conf/conf.json';
// 异步读取并解析JSON文件
fs.readFile(Dir, 'utf8', (err, content) => { //自己的备注,既能读取win格式路径又能linux格式路径
if (err) {
console.error('文件读取失败:', err);
return;
}
try {
const config = JSON.parse(content);
const { clipboard } = require('electron')
clipboard.writeText("userDate="+config.userData+"\n")
runJs.siyuan.showMessage("已复制userData脚本到剪切板,请自行粘贴!!");
//console.log('用户数据:', config.userData); // 输出: 1234
// 获取块内容示例
siyuan.fetchPost("/api/block/getBlockKramdown", {
id: "20250510110212-edgmnmn"
}, (response) => {
console.log("块内容:",response.data.kramdown);
});
// 更新块内容示例
siyuan.fetchPost("/api/block/updateBlock", {
id: "20250510110212-edgmnmn",
dataType: "dom",
data: config.userData
}, (response) => {
if (response.code === 0) {
console.log("更新成功");
}
});
} catch (parseError) {
console.error('JSON解析失败:', parseError);
}
});
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于