EN
JavaScript - paste any data form system clipboard (e.g. image, text, HTML, etc.)
10 points
In this short article we would like to show how to paste any data from system clipboard using JavaScript.
Presented solution lets to paste any content from clipboard, e.g. image, text, HTML, etc. We can limit content type by setting MIME types in the types
array. Clipboard content can be read as plain text (string
), Blob URL (string
), Blob Object, Data URL (string
) or Buffer Object (ArrayBuffer
).
xxxxxxxxxx
1
var types = [ // types order is verry important providing at first top type
2
'image/png',
3
'image/jpeg',
4
'text/plain',
5
'text/html',
6
// etc.
7
];
8
9
var reader = new ClipboardReader(types); // here we expect for clipboard content only according to `types` variable or permit to all when array is empty
10
11
12
// Somewhere in the source code ...
13
14
reader.readContent(function(blob, error) {
15
if (error) {
16
console.error(error);
17
return;
18
}
19
if (blob) {
20
console.log('Clipboard content: ' + blob);
21
return;
22
}
23
console.log('Content is not available - copy something to clipboard.');
24
});
Note: empty
types
array means: we accept all files types.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div>Copy to system clipboard some content and click some bellow button to paste it here.</div>
5
<br />
6
<button onclick="handleClick()">Paste content</button>
7
<script>
8
9
// Main logic:
10
11
function ClipboardReader(types) {
12
var _types = types || [];
13
var findType = function(types) {
14
if (_types.length > 0) {
15
for (var i = 0; i < _types.length; ++i) {
16
var type = _types[i];
17
if (types.includes(type)) {
18
return type;
19
}
20
}
21
return null;
22
} else {
23
return types[0] || null;
24
}
25
};
26
var findItem = function(items) {
27
for (var i = 0; i < items.length; ++i) {
28
var item = items[i];
29
if (item) {
30
var type = findType(item.types);
31
if (type) {
32
return item.getType(type);
33
}
34
}
35
}
36
return null;
37
};
38
this.readContent = function(callback) {
39
if (navigator.clipboard) {
40
var promise = navigator.clipboard.read();
41
promise
42
.then(function(items) {
43
var promise = findItem(items);
44
if (promise == null) { // null or undefined
45
callback(null, null);
46
} else {
47
promise
48
.then(function(blob) {
49
callback(blob);
50
})
51
.catch(function(error) {
52
callback(null, error || 'Clipboard reading error.');
53
});
54
}
55
})
56
.catch(function(error) {
57
callback(null, error || 'Clipboard reading error.');
58
});
59
} else {
60
callback(null, 'Clipboard API is not supported (or check for permisions).');
61
}
62
};
63
};
64
65
66
67
// Helper logic:
68
69
function createReader(callback) {
70
var reader = new FileReader();
71
reader.onload = function() {
72
callback(reader.result, null);
73
};
74
reader.onerror = function() {
75
callback(null, 'Incorrect file.');
76
};
77
return reader;
78
}
79
80
function toPlainText(blob, callback) {
81
var reader = createReader(callback);
82
reader.readAsText(blob);
83
}
84
85
function toDataURL(blob, callback) {
86
var reader = createReader(callback);
87
reader.readAsDataURL(blob);
88
}
89
90
function toArrayBuffer(blob, callback) {
91
var reader = createReader(callback);
92
reader.readAsArrayBuffer(blob);
93
}
94
95
96
97
// Usage example:
98
99
var types = [ // types order is verry important providing at first top type
100
'image/png',
101
'image/jpeg',
102
'text/plain',
103
'text/html',
104
// etc.
105
];
106
107
var reader = new ClipboardReader(types); // here we expect for clipboard content only according to `types` variable or permit to all when array is empty
108
109
function handleClick() {
110
reader.readContent(function(blob, error) {
111
if (error) {
112
console.error(error);
113
return;
114
}
115
if (blob) {
116
toPlainText(blob, function(text, error) {
117
console.log('Clipboard content as plain text:\n' + text);
118
});
119
return;
120
}
121
console.log('Content is not available - copy something to clipboard.');
122
});
123
}
124
125
</script>
126
</body>
127
</html>
According to the above logic it is possible to convert Blob
object to:
- Internal URL (Object URL):
xxxxxxxxxx
1const url = URL.createObjectURL(blob);
2try {
3// e.g. image.src = url;
4} finally {
5URL.revokeObjectURL(url); // releases memory
6}
- Plain text
xxxxxxxxxx
1toPlainText(blob, function(text, error) {
2console.log('[plain text]: ' + text);
3});
- DataURL
xxxxxxxxxx
1toDataURL(blob, function(data, error) {
2console.log('[DataURL]: ' + data);
3});
- ArrayBuffer
xxxxxxxxxx
1toArrayBuffer(blob, function(buffer, error) {
2console.log('[ArrayBuffer]: ' + buffer);
3});