I try to get a readClipboard function on the same model … thanks for the correction
/**
- Attempts to read data from the users clipboard.
- @param {natural} time Maximum runtime of this asynchronous operation after which it will be canceled and rejected.
-
@return {Promise} Promise that rejects if the timeout or an error occurred. If it resolves the operation should have succeeded.
*/
function readFromClipboard(time) { return new Promise(function(resolve, reject) {
let done = false;
function onPaste(event) { try {
if (done) { return; } done = true;
document.removeEventListener(‘paste’, onPaste);
const transfer = event.clipboardData;
//transfer.clearData();
var data = transfer.getData(‘text/plain’);
console.log("found ", data);
if (typeof data === ‘string’) {
resolve(data);
} else {
//Object.keys(data).forEach(mimeType => transfer.setData(mimeType, data[mimeType]));
reject(new Error(“Can’t read the Clipboard”));
}
event.preventDefault();
//resolve();
} catch (error) { reject(error); } }
setTimeout(() => {
if (done) { return; } done = true;
document.removeEventListener(‘paste’, onPaste);
reject(new Error('Timeout after '+ (time || 1000) +‘ms’));
}, time || 1000);
document.addEventListener(‘paste’, onPaste);
document.execCommand(‘paste’, false, null);
}); }
And to call this, I tried
var p = readFromClipboard();
p.then(function(data){console.log("fetch clipboard ", data);}, function(err){console.log("Error ", err);}
);
without success…
F.