-
Notifications
You must be signed in to change notification settings - Fork 1
/
CopyClipboard.js
296 lines (296 loc) · 5.47 KB
/
CopyClipboard.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
*
* @Name : CopyClipboard.js
* @Version : 1.0
* @Programmer : Max
* @Date : 2018-06-23
* @Released under : https://github.com/BaseMax/CopyClipboardJs/blob/master/LICENSE
* @Repository : https://github.com/BaseMax/CopyClipboardJs
*
**/
;(function(window,document)
{
"use strict";
var text_default="Text!"
/**
* @variable systemPasteReady
*
* @goal : true | false
*
**/
var systemPasteReady = false;
/**
* @variable systemPasteContent
*
* @goal : pasted string for cache
*
**/
var systemPasteContent;
/**
* @variable textarea
*
* @goal : temp textarea
*
**/
var textarea;
/**
* @function send
*
* @goal : Send GET request to a link.
*
* @return void
**/
var send=function(link)
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange=function()
{
if(this.readyState == 4 && this.status == 200)
{
//return this.responseText;
}
};
xhttp.open("GET",link,true);
xhttp.send();
};
/**
* @function copy_selected
*
* @goal : Copy selected text to clipboard.
*
* @return void
**/
var copy_selected=function()
{
//soon
};
/**
* @function copy_text
*
* @goal : Copy text to clipboard.
*
* @return void
**/
var copy_text=function(text)
{
/*
var textarea = document.createElement("textarea");
textarea.setAttribute("style","width:0px;border:0;display:none;opacity:0;");
document.body.appendChild(textarea);
textarea.value = text;
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
*/
function selectElementText(element)
{
if (document.selection)
{
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
}
else if (window.getSelection)
{
var range = document.createRange();
range.selectNode(element);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
}
var element = document.createElement('div');
element.textContent = text;
document.body.appendChild(element);
selectElementText(element);
document.execCommand('copy');
element.remove();
};
/**
* @function copy_target
*
* @goal : Copy value of a element to clipboard.
*
* @return void
**/
var copy_target=function(target)
{
var text=text_default;
if(target.val !== undefined)
{
text=target.val;
}
else if(target.innerHTML !== undefined)
{
text=target.innerHTML;
}
else if(target.value !== undefined)
{
text=target.value;
}
copy_text(text);
};
/**
* @function get
*
* @goal : Get text from clipboard.
*
* @return void
**/
var get=function(target)
{
if(window.clipboardData)
{
return window.clipboardData.getData("Text");
}
function waitForPaste()
{
if(systemPasteReady==false)
{
setTimeout(waitForPaste,250);
}
else
{
var text=systemPasteContent;
systemPasteReady = false;
document.body.removeChild(textarea);
textarea = null;
return text;
}
}
textarea = document.createElement("textarea");
textarea.setAttribute("style","width:0px;border:0;display:none;opacity:0;");
document.body.appendChild(textarea);
textarea.select();
return waitForPaste();
};
/**
* @function paste
*
* @goal : Paste text from clipboard.
*
* @return string
**/
var paste=function(target)
{
var text=get();
if(target.val !== undefined)
{
target.val=text;
}
else if(target.innerHTML !== undefined)
{
target.innerHTML=text;
}
else if(target.value !== undefined)
{
target.value=text;
}
};
/**
* @function text
*
* @goal : Get Text from argument and Copy to clipboard.
*
* @return void
**/
var text=function(from)
{
var text=text_default;
if(from.hasAttribute("data-copy-text"))
{
text=from.getAttribute("data-copy-text");
}
copy_text(text);
};
/**
* @function from
*
* @goal : Get Text from a element and Copy to clipboard.
*
* @return void
**/
var from=function(from)
{
var text=text_default;
var target=undefined;
if(from.hasAttribute("data-copy-from"))
{
target=document.querySelector(from.getAttribute("data-copy-from"));
}
if(target === undefined)
{
copy_text(text);
}
else
{
copy_target(target);
}
};
/**
* @struct systemPasteListener
*
* @goal : Listener key press whool of web page.
*
* @return function
**/
function systemPasteListener(evt)
{
systemPasteContent = evt.clipboardData.getData("text/plain");
systemPasteReady = true;
evt.preventDefault();
}
window.addEventListener('paste',systemPasteListener);
/**
* @struct goscroll
*
* @goal : access to public functions
*
* @return struct
**/
window.copy=
{
//manual using
copy_selected:copy_selected,
copy_text:copy_text,
//use
paste:paste,
text:text,
from:from,
send:send,
};
/**
* @struct onload
*
* @goal : set onclick and events after page load...
*
* @return void
**/
window.addEventListener("load",function()
{
var data_copys;
data_copys = document.querySelectorAll("[data-copy-text]");
data_copys.forEach(function(item)
{
if(item.onclick === null)//onclick not exists
{
item.onclick=function()
{
window.copy.text(this);
};
}
});
//////////////////////////////////////////////////////////////////
data_copys = document.querySelectorAll("[data-copy-from]");
data_copys.forEach(function(item)
{
if(item.onclick === null)//onclick not exists
{
item.onclick=function()
{
window.copy.from(this);
};
}
});
//////////////////////////////////////////////////////////////////
},false);
}(window,document));