-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight_share.js
61 lines (45 loc) · 1.52 KB
/
highlight_share.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
var savedText = null; // Variable to save the text
function saveSelection() {
if (window.getSelection) {
var sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
return sel.getRangeAt(0);
}
} else if (document.selection && document.selection.createRange) {
return document.selection.createRange();
}
return null;
}
function restoreSelection(range) {
if (range) {
if (window.getSelection) {
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (ddocument.selection && range.select) {
range.select();
}
}
}
var btnWrap = document.getElementById('share-button'),
btnShare = btnWrap.children[0];
document.onmouseup = function(e) {
savedText = saveSelection(); // Save selection on mouse-up
setTimeout(function() {
var isEmpty = savedText.toString().length === 0; // Check selection text length
// set sharing button position
btnWrap.style.top = (isEmpty ? -9999 : e.pageY) + 'px';
btnWrap.style.left = (isEmpty ? -9999 : e.pageX) + 'px';
}, 10);
};
btnShare.onmousedown = function(e) {
if (!savedText) return;
window.open('https://twitter.com/intent/tweet?text=' + savedText, 'shareWindow', 'width=300,height=150,top=50,left=50'); // Insert the selected text into sharing URL
restoreSelection(savedText); // select back the old selected text
// hide if we are done
setTimeout(function() {
btnWrap.style.top = '-9999px';
btnWrap.style.left = '-9999px';
}, 1000);
return false;
};