-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-copy.js
218 lines (156 loc) · 5.4 KB
/
simple-copy.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* simple-copy.js 0.5.3
*
* Copyright (c) 2021 Guilherme Nascimento (brcontainer@yahoo.com.br)
*
* Released under the MIT license
*/
(function (u) {
"use strict";
var w = typeof window !== "undefined" ? window : {},
d = w.document || {},
$ = w.jQuery,
tmpBody = d.implementation.createHTMLDocument("").body,
prefix = "data-simplecopy-",
ignore = "script,noscript,object,link,img,[simple-copy-ignore=true]";
function isInvisible(el)
{
return (
el.hasAttribute("simple-copy-ignore") !== "true" &&
!el.offsetWidth &&
!el.offsetHeight &&
!el.getClientRects().length
);
}
function copyWithoutFormat(target)
{
var els = target.getElementsByTagName("*"),
restore = [];
for (var i = els.length - 1; i >= 0; i--) {
var el = els[i];
if (isInvisible(el)) {
el.setAttribute("simple-copy-ignore", "true");
restore.push(el);
}
}
tmpBody.innerHTML = target.innerHTML;
for (var i = restore.length - 1; i >= 0; i--) {
restore[i].removeAttribute("simple-copy-ignore");
}
restore = u;
for (var j = tmpBody.querySelectorAll(ignore), i = j.length - 1; i >= 0; i--) {
var el = j[i];
if (el && el.parentNode) el.parentNode.removeChild(el);
}
return copyText(tmpBody.textContent);
}
function copyElement(target, select, text, node, multiple)
{
if (typeof target === "string") {
try {
target = d.querySelector(target);
} catch (ee) {
return false;
}
}
if (!target || target.nodeType !== 1) return false;
var isForm = typeof target.form === "object";
if (text && !isForm) return copyWithoutFormat(target);
var doc = target.ownerDocument,
selection = doc.defaultView.getSelection(),
range = doc.createRange(),
hack = !select && !isForm,
isEditable = target.isContentEditable;
selection.removeAllRanges();
if (hack) target.contentEditable = node ? "inherit" : true;
if (isForm && !node) {
if (select) return selectField(target);
if (target.nodeName === "SELECT" && multiple) {
var result, i = 0, values = [], els = target.options, j = els.length;
for (; i < j; i++) els[i].selected && values.push(els[i].value);
result = values.join(multiple);
} else {
result = target.value;
}
return copyText(result);
} else if (node) {
range.selectNode(target);
} else {
range.selectNodeContents(target);
}
selection.addRange(range);
if (select) return;
var response = doc.execCommand("copy");
if (hack) target.contentEditable = isEditable ? true : "inherit";
selection.removeAllRanges();
target.blur();
return response;
}
function selectField(target)
{
target.focus();
target.select && target.select();
}
function copyText(text)
{
var docEl = d.scrollingElement || d.body,
txt = d.createElement("textarea"),
x = docEl.scrollLeft,
y = docEl.scrollTop;
txt.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px;opacity:0";
txt.value = text;
d.body.appendChild(txt);
selectField(txt);
var response = d.execCommand("copy");
d.body.removeChild(txt);
docEl.scrollLeft = x;
docEl.scrollTop = y;
docEl = txt = u;
return response;
}
function attr(el, option, value)
{
var data = el.getAttribute(prefix + option);
return value ? data : data === "true";
}
function mainEvents(e)
{
if (e.button !== 0) return;
var target, query, el = e.target, data = attr(el, "data", true);
if (data) {
tmpBody.innerHTML = data;
copyElement(tmpBody, false, true);
} else {
query = attr(el, "target", true);
if (!query) return;
target = d.querySelector(query);
if (target) copyElement(target, attr(el, "select"), attr(el, "text"), attr(el, "node"), attr(el, "multiple", true));
}
}
d.addEventListener("click", mainEvents);
var main = {
"select": function (target, opts) {
opts = opts || {};
return copyElement(target, true, false, opts.node);
},
"copy": function (target, opts) {
opts = opts || {};
return copyElement(target, false, opts.text, opts.node, opts.multiple);
},
"data": copyText
};
if ($ && $.extend) {
$.fn.simpleCopy = function (opts) {
var target = this[0];
if (!target) return;
if (!opts) opts = {};
if (opts.type === u) action = "copy";
if (action === "copy" || action === "select") main[action](target, opts);
};
}
w.SimpleCopy = main;
// CommonJS
if (typeof module !== "undefined" && module.exports) module.exports = main;
// RequireJS
if (typeof define !== "undefined") define(function () { return main; });
})();