“你的日记“备份教程
- 使用chrome或者edge进入网页版你的日记(网址为 https://ohshenghuo.com/diary/ 或者 https://nideriji.cn/diary/ ),并登录。
- 按F12打开开发者工具,选择Console(控制台)标签页。
- 复制以下代码到 Console(控制台) 中,并按回车执行。
/**
* 从Cookies中获取token
* */
function getToken() {
var strcookie = document.cookie;//获取cookie字符串
var arrcookie = strcookie.split("; ");//分割
//遍历匹配
for (var i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].split("=");
if (arr[0] == "token") {
return arr[1];
}
}
return "";
}
auth_token = "token " + getToken();
//获取第一篇日记。
diary_0 = await(await fetch(`https://ohshenghuo.com/api/diary/latest/`, {
headers: new Headers({
'auth': auth_token // 指定token
}),
})).json();
var diaries = [diary_0.diary,];
has_more = false;
do {
latest_date = diaries[diaries.length - 1].createddate;
temp = await(await fetch("https://ohshenghuo.com/api/diary/all/?latest_date=" + latest_date + "&count=20", {
headers: new Headers({
'auth': auth_token // 指定token
}),
})).json();
has_more = temp.has_more;
for (var i = 0; i < temp.diaries.length; ++i) {
// console.log(temp.diaries[i].id);
diaries.push(temp.diaries[i]);
}
} while (has_more);
count_character = 0;
for (var i = 0; i < diaries.length; ++i) {
count_character += diaries[i].content.length;
count_character += diaries[i].title.length;
}
if (diaries.length < temp.user_config.diary_count || count_character < temp.user_config.word_count) {
console.error("备份出错,获取数据不完整,请重新备份")
}
function export2file(name, data) {
var urlObject = window.URL || window.webkitURL || window
var export_blob = new Blob([data])
var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
save_link.href = urlObject.createObjectURL(export_blob)
save_link.download = name
fakeClick(save_link)
}
function fakeClick(obj) {
var ev = document.createEvent('MouseEvents')
ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
obj.dispatchEvent(ev)
}
//export2file("test.txt", "hello world");
file_str = "";
for(var i=0; i<diaries.length; ++i){
file_str += diaries[i].createddate;
file_str += "\n";
file_str += diaries[i].title;
file_str += "\n";
file_str += diaries[i].content;
file_str += "\n\n\n";
}
export2file("你的日记.txt", file_str);
浏览器便会自动下载一个名为"你的日记.txt"的文件。
此文件即为日记备份,包含所有日记内容。
若提示备份失败,重新执行 第3步即可。
注:
只能备份文字,不能备份图片。