-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
34 lines (31 loc) · 1.21 KB
/
script.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
document.addEventListener('mousedown', onMouseDown);
document.addEventListener('mouseup', onMouseUp);
var temp = document.querySelector('#shareBoxTemplate');
function onMouseDown() {
document.getSelection().removeAllRanges();
var shareBox = document.querySelector('#shareBox');
if (shareBox !== null)
shareBox.remove();
}
function onMouseUp() {
var sel = document.getSelection(),
txt = sel.toString();
if (txt !== "") {
var range = sel.getRangeAt(0);
if (range.startContainer.parentElement.parentElement.localName === "article" || range.startContainer.parentElement.localName === "article") {
document.body.insertBefore(document.importNode(temp.content, true), temp);
var rect = range.getBoundingClientRect();
var shareBox = document.querySelector('#shareBox');
shareBox.style.top = `calc(${rect.top}px - 38px)`;
shareBox.style.left = `calc(${rect.left}px + calc(${rect.width}px / 2) - 30px)`;
var shareBtn = shareBox.querySelector('button');
shareBtn['shareTxt'] = txt;
shareBtn.addEventListener('mousedown', onShareClick, true);
}
}
}
function onShareClick() {
window.open(`https://twitter.com/intent/tweet?text=${this.shareTxt}`);
this.remove();
document.getSelection().removeAllRanges()
}