-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.js
56 lines (46 loc) · 1.31 KB
/
selection.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
sel = window.getSelection();
// github.com/aavshr/anchor-backend
root = "https://kyofeo.deta.dev/v1";
uuid = () => {
var dt = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (dt + Math.random()*16)%16 | 0;
dt = Math.floor(dt/16);
return (c=='x' ? r :(r&0x3|0x8)).toString(16);
});
return uuid;
}
createAnchor = (id) => {
var invisibleAnchor = document.createElement("a");
invisibleAnchor.setAttribute("id", id);
return invisibleAnchor;
}
createLink = () => {
var range, node;
var anchor_id = uuid();
var anchor = createAnchor(anchor_id);
range = window.getSelection().getRangeAt(0);
range.collapse(true);
range.insertNode(anchor);
postHtml(anchor_id, document.all[0].outerHTML);
// only get the url without a hash
var url = location.href.split("#")[0];
// anchor deliberately misspelled here
url = `${url}?anchorrrr_id=${anchor_id}`;
alert(url);
};
postHtml = (id, html) => {
var data = {
anchor_id: id,
html: html
};
var endpoint = `${root}/anchors`;
fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
}
createLink();