-
Notifications
You must be signed in to change notification settings - Fork 52
/
trilium_server_facade.js
225 lines (179 loc) · 5.25 KB
/
trilium_server_facade.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const PROTOCOL_VERSION_MAJOR = 1;
function isDevEnv() {
const manifest = browser.runtime.getManifest();
return manifest.name.endsWith('(dev)');
}
class TriliumServerFacade {
constructor() {
this.triggerSearchForTrilium();
// continually scan for changes (if e.g. desktop app is started after browser)
setInterval(() => this.triggerSearchForTrilium(), 60 * 1000);
}
async sendTriliumSearchStatusToPopup() {
try {
await browser.runtime.sendMessage({
name: "trilium-search-status",
triliumSearch: this.triliumSearch
});
}
catch (e) {} // nothing might be listening
}
async sendTriliumSearchNoteToPopup(){
try{
await browser.runtime.sendMessage({
name: "trilium-previously-visited",
searchNote: this.triliumSearchNote
})
}
catch (e) {} // nothing might be listening
}
setTriliumSearchNote(st){
this.triliumSearchNote = st;
this.sendTriliumSearchNoteToPopup();
}
setTriliumSearch(ts) {
this.triliumSearch = ts;
this.sendTriliumSearchStatusToPopup();
}
setTriliumSearchWithVersionCheck(json, resp) {
const [major, minor] = json.protocolVersion
.split(".")
.map(chunk => parseInt(chunk));
// minor version is intended to be used to dynamically limit features provided by extension
// if some specific Trilium API is not supported. So far not needed.
if (major !== PROTOCOL_VERSION_MAJOR) {
this.setTriliumSearch({
status: 'version-mismatch',
extensionMajor: PROTOCOL_VERSION_MAJOR,
triliumMajor: major
});
}
else {
this.setTriliumSearch(resp);
}
}
async triggerSearchForTrilium() {
this.setTriliumSearch({ status: 'searching' });
try {
const port = await this.getPort();
console.debug('Trying port ' + port);
const resp = await fetch(`http://127.0.0.1:${port}/api/clipper/handshake`);
const text = await resp.text();
console.log("Received response:", text);
const json = JSON.parse(text);
if (json.appName === 'trilium') {
this.setTriliumSearchWithVersionCheck(json, {
status: 'found-desktop',
port: port,
url: 'http://127.0.0.1:' + port
});
return;
}
}
catch (error) {
// continue
}
const {triliumServerUrl} = await browser.storage.sync.get("triliumServerUrl");
const {authToken} = await browser.storage.sync.get("authToken");
if (triliumServerUrl && authToken) {
try {
const resp = await fetch(triliumServerUrl + '/api/clipper/handshake', {
headers: {
Authorization: authToken
}
});
const text = await resp.text();
console.log("Received response:", text);
const json = JSON.parse(text);
if (json.appName === 'trilium') {
this.setTriliumSearchWithVersionCheck(json, {
status: 'found-server',
url: triliumServerUrl,
token: authToken
});
return;
}
}
catch (e) {
console.log("Request to the configured server instance failed with:", e);
}
}
// if all above fails it's not found
this.setTriliumSearch({ status: 'not-found' });
}
async triggerSearchNoteByUrl(noteUrl) {
const resp = await triliumServerFacade.callService('GET', 'notes-by-url/' + encodeURIComponent(noteUrl))
let newStatus = {
status: 'not-found',
noteId: null
}
if (resp && resp.noteId) {
newStatus.noteId = resp.noteId;
newStatus.status = 'found';
}
this.setTriliumSearchNote(newStatus);
}
async waitForTriliumSearch() {
return new Promise((res, rej) => {
const checkStatus = () => {
if (this.triliumSearch.status === "searching") {
setTimeout(checkStatus, 500);
}
else if (this.triliumSearch.status === 'not-found') {
rej(new Error("Trilium instance has not been found."));
}
else {
res();
}
};
checkStatus();
});
}
async getPort() {
const {triliumDesktopPort} = await browser.storage.sync.get("triliumDesktopPort");
if (triliumDesktopPort) {
return parseInt(triliumDesktopPort);
}
else {
return isDevEnv() ? 37740 : 37840;
}
}
async callService(method, path, body) {
const fetchOptions = {
method: method,
headers: {
'Content-Type': 'application/json'
},
};
if (body) {
fetchOptions.body = typeof body === 'string' ? body : JSON.stringify(body);
}
try {
await this.waitForTriliumSearch();
fetchOptions.headers.Authorization = this.triliumSearch.token || "";
fetchOptions.headers['trilium-local-now-datetime'] = this.localNowDateTime();
const url = this.triliumSearch.url + "/api/clipper/" + path;
console.log(`Sending ${method} request to ${url}`);
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(await response.text());
}
return await response.json();
}
catch (e) {
console.log("Sending request to trilium failed", e);
toast('Your request failed because we could not contact Trilium instance. Please make sure Trilium is running and is accessible.');
return null;
}
}
localNowDateTime() {
const date = new Date();
const off = date.getTimezoneOffset();
const absoff = Math.abs(off);
return (new Date(date.getTime() - off * 60 * 1000).toISOString().substr(0,23).replace("T", " ") +
(off > 0 ? '-' : '+') +
(absoff / 60).toFixed(0).padStart(2,'0') + ':' +
(absoff % 60).toString().padStart(2,'0'));
}
}
window.triliumServerFacade = new TriliumServerFacade();