-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
100 lines (96 loc) · 3.34 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require('dotenv').config();
let id = "";
let notepad = {};
let notepadContent = [];
let notepadToPost = {};
const notifier = require('node-notifier');
const path = require('path');
const clipboard = nw.Clipboard.get();
import {info} from "./index.js";
export async function update(text) {
//获取词书id
try {
const response = await fetch(
`https://open.maimemo.com/open/api/v1/notepads?limit=${1}&offset=${1}`,
{
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: process.env.TOKEN
}
});
const data = await response.json();
id = data.data.notepads[0].id;
} catch (error) {
console.error(error);
}
//根据id获取词书内容
try {
const response = await fetch(
`https://open.maimemo.com/open/api/v1/notepads/${id}`,
{
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: process.env.TOKEN
}
});
const data = await response.json();
notepad = data;
data.data.notepad.list.forEach ((item) => {
if (item.type === "CHAPTER") {
} else {
notepadContent.push(item.word);
}
});
} catch (error) {
console.error(error);
}
//读取剪贴板,获取要添加的词语,并清空剪贴板
notepadContent.push(text);
console.log(notepadContent);
notepadToPost = JSON.stringify({
notepad: {
status: notepad.data.notepad.status,
content: `//\n${notepadContent.join("\n")}`,
title: notepad.data.notepad.title,
brief: notepad.data.notepad.brief,
tags: notepad.data.notepad.tags
}
});
clipboard.clear(); //清空剪贴板
notepadContent = [];//清空存储的剪贴板内容
// 更新云词本
try {
const response = await fetch(
`https://open.maimemo.com/open/api/v1/notepads/${id}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: process.env.TOKEN
},
body: notepadToPost
});
const data = await response.json();
if (data) {
info.innerHTML = `${text} uploaded!`;
notifier.notify(
{
title: text,
message: `${text} uploaded!`,
icon: path.join(`${nw.App.startPath.replace(/\\/g, '/')}/img/icon.png`), // Absolute path
sound: true, // Only Notification Center or Windows Toasters
wait: true // Wait with callback, until user action is taken against notification, does not apply to Windows Toasters as they always wait or notify-send as it does not support the wait option
},
function (err, response, metadata) {
// Response is response from notification
// Metadata contains activationType, activationAt, deliveredAt
}
);
}
} catch (error) {
console.error(error);
}
}