-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhtml-to-clipboard.js
163 lines (136 loc) · 5.13 KB
/
html-to-clipboard.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function HtmlToClipboard(options) {
// default settings
var settings = {
limitTarget: null,
listenOnCopy: false,
raw: false
};
// use this flag to avoid recursively copying
var justCopied = false;
// actual settings
if (typeof options === "object") {
for (option in options) {
settings[option] = options[option];
}
}
if (typeof settings.raw !== "boolean")
throw "'raw' option must be boolean";
if (settings.limitTarget && (typeof settings.limitTarget !== "string" && !(settings.limitTarget instanceof HTMLElement)))
throw "'target' option must be either a selector or a DOM node";
// find target element either from selector string or from element
var getTarget = function(limitTarget) {
var target = limitTarget;
if (typeof target === "string") {
var el = document.querySelector(target);
if (el === null) {
throw "Element with selector '" + target + "' does not exist in the DOM";
} else {
return el;
}
} else {
if (document.contains(target)) {
return target;
} else {
throw "Target element is not present in the DOM";
}
}
};
// get the current selection, possibly limited to target element
var getSelection = function (limitTarget) {
var selection = window.getSelection();
// limit selection to target
if (limitTarget) {
var target = getTarget(limitTarget);
// don't limit the selection in standard inputs
if (typeof target.selectionStart !== "undefined") {
return target.value.substring(target.selectionStart, target.selectionEnd);
}
for (var i = 0; i < selection.rangeCount; i++) {
var range = selection.getRangeAt(i);
var sliceStart = 0;
var sliceEnd = null;
if (range.intersectsNode(target)) {
var lengthBefore = range.toString().length;
range.setStart(target, 0);
var lengthAfter = range.toString().length;
if (lengthAfter > lengthBefore)
sliceStart = lengthAfter - lengthBefore;
lengthBefore = range.toString().length;
range.setEnd(target, target.childNodes.length);
lengthAfter = range.toString().length;
if (lengthAfter > lengthBefore)
sliceEnd = lengthBefore;
else
sliceEnd = lengthAfter;
return range.toString().substr(sliceStart, sliceEnd);
}
}
return "";
} else {
// textarea
if (typeof document.activeElement.selectionStart !== "undefined") {
return document.activeElement.value.substring(document.activeElement.selectionStart, document.activeElement.selectionEnd);
// everything else
} else {
return selection.toString();
}
}
};
var copyToClipboard = function (data, raw) {
if (typeof raw !== "boolean")
raw = true;
var tmpEl = document.createElement(raw ? "textarea" : "div");
tmpEl.style.opacity = 0;
tmpEl.style.position = "absolute";
tmpEl.style.pointerEvents = "none";
tmpEl.style.zIndex = -1;
tmpEl.setAttribute('tabindex', '-1'); // so it can be focused
tmpEl.innerHTML = data;
document.body.appendChild(tmpEl);
var focused = document.activeElement;
tmpEl.focus();
if (raw) {
tmpEl.select();
} else {
window.getSelection().removeAllRanges();
var range = document.createRange();
range.setStartBefore(tmpEl.firstChild);
range.setEndAfter(tmpEl.lastChild);
window.getSelection().addRange(range);
}
var success = false;
try {
justCopied = true;
setTimeout(function () {
justCopied = false;
});
success = document.execCommand('copy');
} catch (err) {
console.error(err);
}
if (!success)
console.error('execCommand failed!');
window.getSelection().removeAllRanges();
document.body.removeChild(tmpEl);
focused.focus();
}
// listen on copy event
if (settings.listenOnCopy) {
var eventTarget = document;
if (settings.limitTarget)
eventTarget = getTarget(settings.limitTarget);
var self = this;
eventTarget.addEventListener('copy', function (e) {
if (!justCopied) {
e.preventDefault();
self.copy();
}
});
}
this.copy = function(data) {
if (typeof data !== "string")
var data = getSelection(settings.limitTarget);
if (data !== "")
copyToClipboard(data, settings.raw);
};
}