-
Notifications
You must be signed in to change notification settings - Fork 463
/
clipboard.js
46 lines (38 loc) · 1.19 KB
/
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
function createClipboard() {
var self = {};
var holder = document.createElement('textarea');
holder.contentEditable = true;
holder.enableAutoFocus = true;
holder.id = 'sk_clipboard';
function clipboardActionWithSelectionPreserved(cb) {
actionWithSelectionPreserved(function(selection) {
// avoid editable body
document.documentElement.appendChild(holder);
cb(selection);
holder.remove();
});
}
self.read = function(onReady) {
clipboardActionWithSelectionPreserved(function() {
holder.value = '';
setInnerHTML(holder, '');
holder.focus();
document.execCommand("Paste");
});
var data = holder.value;
if (data === "") {
data = holder.innerHTML.replace(/<br>/gi,"\n");
}
onReady({data: data});
};
self.write = function(text) {
clipboardActionWithSelectionPreserved(function() {
holder.value = text;
holder.select();
document.execCommand('copy');
holder.value = '';
});
Front.showBanner("Copied: " + text);
};
return self;
}