-
Notifications
You must be signed in to change notification settings - Fork 3
/
context_menu_sample_code.txt
55 lines (43 loc) · 1.33 KB
/
context_menu_sample_code.txt
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
// variables
var helper = null
, selection = null
, range = null
, text = ''
, trim = true
, deselect = false;
document.body.addEventListener("keydown", function (event) {
// only proceed if crtl + c is pressed
if (event.ctrlKey !== true || event.keyCode !== 67) return true;
// get selected text without dom info
selection = window.getSelection();
text = selection.toString();
// get old range
if (selection.getRangeAt && selection.rangeCount && !deselect)
range = selection.getRangeAt(0);
else range = null;
// trim whitespace if chosen
if (trim === true) text = text.replace(/^\s+|\s+$/g, '');
// create a helper textarea
if (helper === null) {
helper = document.createElement('textarea');
helper.style.position = "absolute";
helper.style.fontSize = "1pt";
helper.style.border = "none";
helper.style.margin = "-100";
document.body.appendChild(helper);
}
// change text of helper, make it visible and select contents
helper.value = text;
helper.style.display = "block";
helper.select();
// hide the element after 100ms
window.setTimeout(function () {
// hide helper element
helper.style.display = "none";
// restore selection if one exists
if (range === null && !deselect) return;
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
},100);
}, false);