-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfetchAnnotations.js
46 lines (40 loc) · 1.29 KB
/
fetchAnnotations.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
// this web worker fetches annotations for urls of zotero items
// use hlib, not hlib2, because no web components in this environment
self.importScripts('https://jonudell.info/hlib/hlib.bundle.js')
// listen for a request to query hypothesis for annotations on a zotero item
self.addEventListener('message', e => {
const url = e.data.zoteroItem.url
const key = e.data.zoteroItem.key
const token = e.data.token
const hypothesisQuery = `https://hypothes.is/api/search?uri=${url}`
const opts = {
method: 'get',
url: hypothesisQuery
}
if (token) {
opts.headers = {
Authorization: 'Bearer ' + e.data.token,
'Content-Type': 'application/json;charset=utf-8'
}
}
// find hypothesis annotations for a zotero item
hlib
.httpRequest(opts)
.then( data => {
const hypothesisInfo = JSON.parse(data.response)
// message the caller with zotero item info plus hypothesis search results
self.postMessage({
key: key,
version: e.data.zoteroItem.version,
url: e.data.zoteroItem.url,
title: e.data.zoteroItem.title,
doi: e.data.zoteroItem.doi,
hypothesisAnnos: hypothesisInfo.rows,
hypothesisTotal: hypothesisInfo.total
})
})
.catch( e => {
const msg = `fetchAnnotations failed: ${opts.url}, ${data.response}, ${JSON.stringify(e)}`
self.postMessage(msg)
})
})