-
Notifications
You must be signed in to change notification settings - Fork 3
/
annotate.js
40 lines (32 loc) · 1.11 KB
/
annotate.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
// receive action (highlight, comment, copy) request
// send window.selection object to context-menu?
// or build TextQuoteSelector here and send that?
import * as textPosition from 'dom-anchor-text-position';
import * as textQuote from 'dom-anchor-text-quote';
function getAnnotation() {
const selection = window.getSelection();
const range = selection.getRangeAt(0);
if (range.collapsed) return {};
const textPositionSelector = textPosition.fromRange(document.body, range);
Object.assign(textPositionSelector, { type: 'TextPositionSelector' });
const textQuoteSelector = textQuote.fromRange(document.body, range);
Object.assign(textQuoteSelector, { type: 'TextQuoteSelector' });
const annotation = {
'@context': 'http://www.w3.org/ns/anno.jsonld',
type: 'Annotation',
target: {
type: 'SpecificResource',
source: window.location.href,
selector: [
textPositionSelector,
textQuoteSelector,
]
}
};
return annotation;
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
sendResponse({
annotation: getAnnotation()
});
});