-
Notifications
You must be signed in to change notification settings - Fork 0
/
_clipboard.js
77 lines (76 loc) · 2.91 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
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
var _clipboard = {
mode: null,
available: {
api: false,
clipboardData: false,
execCommand: false
},
init: function(){
if(window.clipboardData&&window.clipboardData.setData) _clipboard.available.clipboardData = true;
else if(document.queryCommandSupported('copy')) _clipboard.available.execCommand = true;
_clipboard.setMode();
if(_clipboard.mode!='clipboardData'){
try{
if(typeof navigator.clipboard.readText!==undefined){
navigator.permissions.query({name: 'clipboard-read'}).then(function(permissionStatus){
if(permissionStatus.state=='granted') _clipboard.available.api = true;
else if(permissionStatus.state=='denied') _clipboard.available.api = false;
_clipboard.setMode();
permissionStatus.onchange = function(){
if(permissionStatus.state=='granted') _clipboard.available.api = true;
else if(permissionStatus.state=='denied') _clipboard.available.api = false;
_clipboard.setMode();
};
});
}
}catch(e){}
}
},
setMode: function(mode){
if(mode===undefined){
if(_clipboard.available.api) _clipboard.mode = 'api';
else if(_clipboard.available.clipboardData) _clipboard.mode = 'clipboardData';
else if(_clipboard.available.execCommand) _clipboard.mode = 'execCommand';
else _clipboard.mode = 'prompt';
}else this.mode = mode;
},
text: function(text){
if(text===undefined){
switch(this.mode){
case 'api': err.log('_clipboard.text in mode api is available only async, use _clipboard.read(func) instead'); break;
case 'clipboardData': text = clipboardData.getData('Text'); break;
case 'execCommand':
var textarea = document.createElement('textarea');
textarea.style.position = 'fixed';
document.body.appendChild(textarea); textarea.focus();
try{ document.execCommand('paste'); text = textarea.value; }
catch(ex){ prompt('_clipboard.text failed, ctrl+v now for paste manually', ''); }
finally{ document.body.removeChild(textarea); }
break;
case 'prompt': prompt('ctrl+v now for paste manually', ''); break;
} return text;
}else{
switch(this.mode){
case 'api': navigator.clipboard.writeText(text); break;
case 'clipboardData': clipboardData.setData('Text', text); break;
case 'execCommand':
var textarea = document.createElement('textarea');
textarea.textContent = text; document.body.appendChild(textarea); textarea.select();
try{ document.execCommand('copy'); }
catch(ex){ prompt('clipboard.text failed, ctrl+c or ctrl+x now for copy manually', text); }
finally{ document.body.removeChild(textarea); }
break;
case 'prompt': prompt('ctrl+c or ctrl+x now for copy manually', text); break;
}
}
},
read: function(callback){
if(this.mode=='api') navigator.clipboard.readText().then(function(text){ callback(text); });
else{
var text = _clipboard.text();
callback(text);
}
},
write: function(text){ _clipboard.text(text); }
};
_clipboard.init();