<html>
<head>
<title>Clipboard API Usage - RJM Programming - July, 2022</title>
<style>
#destination {
width:70%;
height:200px;
}

td {
vertical-align: top;
}
</style>
<script type='text/javascript'>
var aminterested='';
</script>
</head>
<body ontouchstart='onc(event);' onmousedown='onc(event);' onload='onl();' style='background-color:transparent;'>
<div id=ourldiv></div>
<h1>Clipboard API Usage</h1>
<h3>RJM Programming - July, 2022 <span id=status></span></h3>
<h4>Thanks to <a target=_blank title='https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API' href='//developer.mozilla.org/en-US/docs/Web/API/Clipboard_API'>https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API</a></h4>

<table border=10 cellpadding=5>
<tr><th colspan=5>Clipboard Text ...</tr>
<tr><td><button onclick="pasteinto('after');">Append Below ...</button></td><td><button onclick="pasteinto('before');">Prepend Below ...</button><td><button onclick="pasteinto('cursor');">At Cursor ...</button></td><td><button onclick="pasteinto('cutpaste');">Cut (Highlight Below) and Paste ...</button></td><td><button onclick="pasteinto('setclipboard');">Set Clipboard (to Highlight Below) ...</button></td></tr>
</table><br><br>
<table><tr><td><textarea id=mytao class=editor rows=7 cols=120 onmousedown="mustnot=true; setTimeout(mnoff, 2000);" ontouchstart="mustnot=true; setTimeout(mnoff, 2000);"></textarea></td></TR><TR><td id=emailsms style=display:none;vertical-align:top;>&nbsp;<a target=_blank href='mailto:?subject=My%20Selection%20...&body=' id=aemail title=Email>&#128231;</a>&nbsp;<a target=_blank onmouseover="if (smsee.length == 0 && origsmsurl.indexOf('sms:&') != -1) { smsee=prompt('Please enter SMS number to send to.', ''); if (smsee == null) { smsee=''; } else { origsmsurl=origsmsurl.replace('sms:&','sms:' + smsee + '&'); asmsurl=asmsurl.replace('sms:&','sms:' + smsee + '&'); this.href=asmsurl; } }" href='sms:&body=My%20Selection%20...' id=asms title=SMS>&#128223;</a></td></tr></table><br><br>
<table border=10 cellpadding=5>
<tr><th colspan=5>Clipboard Image ...</tr>
<tr><td><button onclick="pasteinto('mafter');">Append Below ...</button></td><td><button onclick="pasteinto('mbefore');">Prepend Below ...</button><td><button onclick="mustnot=true; pasteinto('mcursor'); setTimeout(mnoff, 2000);" ontouchstart='mustnot=true; setTimeout(mnoff, 2000);' onmousedown='mustnot=true; setTimeout(mnoff, 2000);'>At Click ...</button></td><td><button onclick="pasteinto('mcutpaste');">Cut (Highlight Below) and Paste ...</button></td><td><button onclick="pasteinto('msetclipboard');">Set Clipboard (to Highlight Below) ...</button></td></tr>
</table><br><br>
<details id=mydetails><summary id=mysummary></summary>
<table id=ctable>
<tbody id=ctbody><tr id=trcanvas><td id=tdcanvas1>
<canvas onclick='setib(this);' width=0 height=0 id=mycanvas1 class=meditor style="width:0px;height:0px;border:2px solid red;display:none;" title='Click here to paste image clipboard data'></canvas>
</td></tr></tbody></table></details>
<br><img id=destination title='Click here to paste image clipboard data'></img><br><img src='./clipboard_api_test.jpg' style=display:none;></img>

<table><tr><td><textarea id=mytao class=editor rows=10 cols=80></textarea></td><td id=emailsms style=display:none;vertical-align:top;>&nbsp;<a target=_blank href='mailto:?subject=My%20Selection%20...&body=' id=aemail title=Email>&#128231;</a>&nbsp;<a target=_blank onmouseover="if (smsee.length == 0 && origsmsurl.indexOf('sms:&') != -1) { smsee=prompt('Please enter SMS number to send to.', ''); if (smsee == null) { smsee=''; } else { origsmsurl=origsmsurl.replace('sms:&','sms:' + smsee + '&'); asmsurl=asmsurl.replace('sms:&','sms:' + smsee + '&'); this.href=asmsurl; } }" ontouchstart="if (smsee.length == 0 && origsmsurl.indexOf('sms:&') != -1) { smsee=prompt('Please enter SMS number to send to.', ''); if (smsee == null) { smsee=''; } else { origsmsurl=origsmsurl.replace('sms:&','sms:' + smsee + '&'); asmsurl=asmsurl.replace('sms:&','sms:' + smsee + '&'); this.href=asmsurl; } }" href='sms:&body=My%20Selection%20...' id=asms title=SMS>&#128223;</a></td></tr></table>

<script type='text/javascript'>
// var aminterested=false;
var origemailurl='mailto:?subject=My%20Selection%20...&body=';
var origsmsurl='sms:&body=My%20Selection%20...';
var smsee='';
var mustnot=false;
var xis=-1, yis=-1;
var pos3=-1, pos4=-1;
var thiscellis=-1;
var sofar = '';
var sofarbefore='', sofarafter='', sofarmid='';
var msofar = '';
var msofarbefore='', msofarafter='', msofarmid='';
var ctx=null, cnv=null;
var filem=null;
var destinationImage=null;
var mmode='mbefore', prevmmode='mbefore';
var imgz=null;
var prevw=0, prevh=0;
var wo=null;
var curtr=0;
var tdtemplate='';

function pasteinto(inmode) {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of pasteinto(inmode) at ' + (new Date()) + ' ... ';
}
//event.stopPropagation();
consolelog('here at pasteinto');
sofar = document.querySelector(".editor").value;
consolelog('here at pasteinto sofar=' + sofar + ' with sofarbefore=*' + sofarbefore + '* and sofarafter=*' + sofarafter + '*');

//if (('' + cnv.style.width + ' ').substring(0,1) == '0') {
//mmode='mbefore';
//} else {
mmode=inmode;
//}
consolelog('mmode=' + mmode);

switch (inmode) {
case 'before':
navigator.clipboard.readText().then(
(clipText) => document.querySelector(".editor").value = clipText + sofar);
break;

case 'after':
navigator.clipboard.readText().then(
(clipText) => document.querySelector(".editor").value += clipText);
break;

case 'setclipboard':
if (document.querySelector(".editor").selectionStart == document.querySelector(".editor").selectionEnd) {
sofarmid='';
} else {
sofarmid=document.querySelector(".editor").value.substring(document.querySelector(".editor").selectionStart, document.querySelector(".editor").selectionEnd);
}
navigator.clipboard.writeText(sofarmid).then(function() {
/* clipboard successfully set */
}, function() {
/* clipboard write failed */
});
break;

case 'cutpaste':
if (document.querySelector(".editor").selectionStart == document.querySelector(".editor").selectionEnd) {
sofarmid='';
} else {
sofarmid=document.querySelector(".editor").value.substring(document.querySelector(".editor").selectionStart, document.querySelector(".editor").selectionEnd);
}
navigator.clipboard.readText().then(
(clipText) => document.querySelector(".editor").value = sofarbefore + clipText + sofarafter.substring(sofarmid.length));
break;

case 'cursor':
consolelog('at cursor');
navigator.clipboard.readText().then(
(clipText) => document.querySelector(".editor").value = sofarbefore + clipText + sofarafter);
break;

case 'mbefore':
destinationImage.click();
break;

case 'mafter':
destinationImage.click();
break;

case 'msetclipboard':
document.getElementById('mydetails').setAttribute('open',true); //destinationImage.click();
if (document.getElementById('mysummary').innerHTML.indexOf(' Images below ') == -1) {
document.getElementById('mysummary').innerHTML='Accumulated Images below ...';
}
if (document.getElementById('mysummary').innerHTML.indexOf(' click ') == -1) {
document.getElementById('mysummary').innerHTML+=' please click image below to copy to clipboard ... ';
}
break;

case 'mcutpaste':
document.getElementById('mydetails').setAttribute('open',true); //destinationImage.click();
if (document.getElementById('mysummary').innerHTML.indexOf(' Images below ') == -1) {
document.getElementById('mysummary').innerHTML='Accumulated Images below ...';
}
if (document.getElementById('mysummary').innerHTML.indexOf(' click ') == -1) {
document.getElementById('mysummary').innerHTML+=' please click image below to change for clipboard contents ... ';
}
break;

case 'mcursor':
xis=pos3;
yis=pos4;
destinationImage.click();
break;

default:
consolelog('at cursor');
navigator.clipboard.readText().then(
(clipText) => document.querySelector(".editor").value = sofarbefore + clipText + sofarafter);
break;
}

if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of pasteinto(inmode) at ' + (new Date()) + ' ... ';
}
}

//document.getElementById('mytao').addEventListener('click', showposition); // click
document.querySelector(".editor").addEventListener('click', showposition); // click
document.querySelector(".editor").addEventListener('keyup', showpositionkp); // click
//document.querySelector(".editor").addEventListener('losefocus', showpositionkp); // click

function showposition(event) { // thanks to https://stackoverflow.com/questions/62310186/how-do-you-get-the-current-cursor-position-in-a-textarea-whenever-the-cursor-o
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of showposition(event) at ' + (new Date()) + ' ... ';
}
consolelog(event.target.selectionStart);
if (document.querySelector(".editor").value != '') {
//onsole.log('yes, here');
sofarbefore=document.querySelector(".editor").value.substring(0, event.target.selectionStart);
sofarafter=document.querySelector(".editor").value.substring(event.target.selectionStart);
//consolelog('sofarbefore=' + sofarbefore + ' and sofarafter=' + sofarafter);
}
document.querySelector(".editor").focus();
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of showposition(event) at ' + (new Date()) + ' ... ';
}
}

function showpositionkp(event) { // thanks to https://stackoverflow.com/questions/62310186/how-do-you-get-the-current-cursor-position-in-a-textarea-whenever-the-cursor-o
//event.stopPropagation();
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of showpositionkp(event) at ' + (new Date()) + ' ... ';
}
consolelog(event.target.selectionStart);
if (document.querySelector(".editor").value != '') {
consolelog('yes, here');
if (event.target.selectionStart == event.target.selectionEnd) {
sofarmid='';
sofarbefore=document.querySelector(".editor").value.substring(0, event.target.selectionStart);
sofarafter=document.querySelector(".editor").value.substring(event.target.selectionStart);
} else {
sofarmid=ocument.querySelector(".editor").value.substring(event.target.selectionStart, event.target.selectionEnd);
sofarbefore=document.querySelector(".editor").value.substring(0, event.target.selectionStart);
sofarafter=document.querySelector(".editor").value.substring(event.target.selectionStart);
}
consolelog('sofarbefore=' + sofarbefore + ' and sofarafter=' + sofarafter + ' and sofarmid=' + sofarmid);
}
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of showpositionkp(event) at ' + (new Date()) + ' ... ';
}
}

function onl() {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of onl() at ' + (new Date()) + ' ... ';
}
cnv=document.getElementById("mycanvas1");
destinationImage = document.querySelector('#destination')
destinationImage.addEventListener('click', pasteImage);
//document.querySelector(".meditor").addEventListener('click', pasteImage);
tdtemplate=document.getElementById("tdcanvas1").outerHTML;

ctx=document.getElementById("mycanvas1").getContext("2d");
//document.getElementById("mycanvas1").addEventListener("drop", onDrop);
//window.addEventListener("paste", onPaste);
document.getElementById("mytao").focus();
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of onl() at ' + (new Date()) + ' ... ';
}
}

function onDrop(event) {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of onDrop(event) at ' + (new Date()) + ' ... ';
}
consolelog('here od');
event.preventDefault();
const file = event.dataTransfer.files[0];
this.createRasterFromFile(file);
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of onDrop(event) at ' + (new Date()) + ' ... ';
}
}

function onPaste(event) {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of onPaste(event) at ' + (new Date()) + ' ... ';
}
mustnot=true;
consolelog('here op');
event.preventDefault();
event.stopPropagation();
setTimeout(mnoff, 2000);
consolelog('event.clipboardData=' + event.clipboardData);
consolelog('window.clipboardData=' + window.clipboardData);
consolelog('event.clipboardData.files=' + event.clipboardData.files);
//consolelog('window.clipboardData.files=' + window.clipboardData.files);
//consolelog('event.clipboardData.items=' + event.clipboardData.items);
//consolelog('window.clipboardData.items=' + window.clipboardData.items);
filem = (event.clipboardData || window.clipboardData).files;
consolelog('here op2');
//createRasterFromFile(filem[0].getAsFile());
createRasterFromFile(filem[0]);
consolelog('here op3');
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of onPaste(event) at ' + (new Date()) + ' ... ';
}
}

function createRasterFromFile(filex) {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of createRasterFromFile(filex) at ' + (new Date()) + ' ... ';
}
consolelog('here cr');
if (filex && (filex.type == 'image/png' || filex.type == 'image/jpeg')) {
consolelog('here CR');
const reader = new FileReader();
reader.onload = function () {
// Image data stored in reader.result
consolelog('here');
ctx.drawImage(reader.result, 0, 0);
}.bind(this);
reader.readAsDataURL(filex);
} else if (filex) {
consolelog('not here ' + filex.type);
} else if (filem) {
consolelog('here filem ' + filem.type);
} else {
consolelog('not here filem');
}
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of createRasterFromFile(filex) at ' + (new Date()) + ' ... ';
}
}


async function pasteImage() {
if (aminterested == '') { aminterested='just'; }
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of pasteImage() at ' + (new Date()) + ' ... ';
}
//event.stopPropagation();
try {
const permission = await navigator.permissions.query({ name: 'clipboard-read' });
if (permission.state === 'denied') {
throw new Error('Not allowed to read clipboard.');
}
const clipboardContents = await navigator.clipboard.read();
for (const item of clipboardContents) {
if (!item.types.includes('image/png')) {
throw new Error('Clipboard contains non-image data.');
}
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + " started call of const blob = await item.getType('image/png'); at " + (new Date()) + ' ... ';
}
const blob = await item.getType('image/png');
imgz = new Image();
anothercell();
imgz.onload = () => {
aminterested='Just';
document.getElementById('status').innerHTML=' <br>' + aminterested + ' started lazy evaluation of imgz.onload = () => {} at ' + (new Date()) + ' ... ';
destinationImage.style.width='' + imgz.width + 'px';
destinationImage.style.height='' + imgz.height + 'px';
prevw=eval('' + ('' + cnv.style.width).replace('px',''));
prevh=eval('' + ('' + cnv.style.height).replace('px',''));
consolelog('canvas width becomes ' + eval('' + cnv.width) + ' + ' + eval('' + imgz.width) + ' = ' + '' + eval(eval('' + cnv.width) + eval('' + imgz.width)) + 'px');
cnv.style.width='' + eval(eval('' + ('' + cnv.style.width).replace('px','')) + eval('' + imgz.width)) + 'px';
cnv.style.height='' + eval(eval('' + ('' + cnv.style.height).replace('px','')) + eval('' + imgz.height)) + 'px';
cnv.width='' + ('' + cnv.style.width).replace('px','') + 'px';
cnv.height='' + ('' + cnv.style.height).replace('px','') + 'px';
if (mmode == 'mbefore' || 1 == 1) {
ctx.drawImage(imgz, prevw, prevh);
if (wo) {
wo.close();
wo=null;
}
cnv.style.display='block';
//wo=window.open('','_blank','top=50,left=50,height=600,width=600');
//wo.document.write(cnv.toDataURL('image/png'));
}
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished lazy evaluation of imgz.onload = () => {} at ' + (new Date()) + '. ';
aminterested=aminterested.toLowerCase();
};
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + " started call of imgz.src = URL.createObjectURL(blob); at " + (new Date()) + ' ... ';
}
imgz.src = URL.createObjectURL(blob);
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + " started call of destinationImage.src = URL.createObjectURL(blob); at " + (new Date()) + ' ... ';
}
destinationImage.src = URL.createObjectURL(blob);
//cnv.style.backgroundRepeat=(('' + cnv.style.backgroundRepeat) + ',no-repeat').replace(/^\,/g,'');
//if (('' + cnv.style.background + ' ').trim() != '') { document.getElementById('mysummary').innerHTML='Accumulated Images below ...'; }
cnv.style.background=(('' + cnv.style.background) + ',url(' + destinationImage.src + ') no-repeat').replace(/^\,/g,'');
//document.querySelector(".editor").style.display='block';
//destinationImage.style.display='none';
setTimeout(anothercellz, 6000);
}
}
catch (error) {
console.error(error.message);
}
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of pasteImage() at ' + (new Date()) + ' ... ';
}
if (aminterested == aminterested.toLowerCase()) { aminterested=''; }
}

function thiscell(icurtr) {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of thiscell(icurtr) at ' + (new Date()) + ' ... ';
}
//curtr++;
//alert(tdtemplate.replace(/1/g, '' + curtr));
var wastrih=document.getElementById('trcanvas').innerHTML;
consolelog('Mmode=' + mmode);
cnv=document.getElementById('mycanvas' + icurtr);
ctx=document.getElementById("mycanvas" + icurtr).getContext("2d");
cnv.style.width='0px';
cnv.style.height='0px';
cnv.style.background='';
if (1 == 6) {
imgz.onload = () => {
destinationImage.style.width='' + imgz.width + 'px';
destinationImage.style.height='' + imgz.height + 'px';
prevw=0;
prevh=0;
consolelog('canvas width becomes ' + eval('' + cnv.width) + ' + ' + eval('' + imgz.width) + ' = ' + '' + eval(eval('' + cnv.width) + eval('' + imgz.width)) + 'px');
cnv.style.width='' + eval(0 + eval('' + imgz.width)) + 'px';
cnv.style.height='' + eval(0 + eval('' + imgz.height)) + 'px';
cnv.width='' + ('' + cnv.style.width).replace('px','') + 'px';
cnv.height='' + ('' + cnv.style.height).replace('px','') + 'px';
if (mmode == 'mbefore' || 1 == 1) {
ctx.drawImage(imgz, prevw, prevh);
if (wo) {
wo.close();
wo=null;
}
cnv.style.display='block';
//wo=window.open('','_blank','top=50,left=50,height=600,width=600');
//wo.document.write(cnv.toDataURL('image/png'));
}
};
imgz.src = URL.createObjectURL(blob);
destinationImage.src = URL.createObjectURL(blob);
//cnv.style.backgroundRepeat=(('' + cnv.style.backgroundRepeat) + ',no-repeat').replace(/^\,/g,'');
//if (('' + cnv.style.background + ' ').trim() != '') { document.getElementById('mysummary').innerHTML='Accumulated Images below ...'; }
cnv.style.background=(('') + ',url(' + destinationImage.src + ') no-repeat').replace(/^\,/g,'');
//document.querySelector(".editor").style.display='block';
//destinationImage.style.display='none';
}
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of thiscell(icurtr) at ' + (new Date()) + ' ... ';
}
}

function anothercell() {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of anothercell() at ' + (new Date()) + ' ... ';
}
var ouro=document.getElementById('trcanvas');
if (thiscellis >= 0) {
thiscell(thiscellis);
thiscellis=-1;
} else {
curtr++;
//alert(tdtemplate.replace(/1/g, '' + curtr));
var ourtdtemplate=tdtemplate;
if (('' + xis).indexOf('-') == -1) {
ourtdtemplate=ourtdtemplate.replace(' style="', ' style="position:absolute;z-index:-99;opacity:0.6;left:' + xis + 'px;top:' + yis + 'px;');
xis=-1;
yis=-1;
ouro=document.getElementById('ourldiv');
ourtdtemplate='<canvas' + ourtdtemplate.split('<canvas')[1].split('</td>')[0];
//alert(ourtdtemplate);
}
var wastrih=document.getElementById('trcanvas').innerHTML;
consolelog('Mmode=' + mmode);
if (!document.getElementById('tdcanvas' + curtr)) {
if (mmode != 'mbefore') {
ouro.innerHTML+=ourtdtemplate.replace(/1/g, '' + curtr);
} else {
ouro.innerHTML=ourtdtemplate.replace(/1/g, '' + curtr) + wastrih;
}
cnv=document.getElementById('mycanvas' + curtr);
ctx=document.getElementById("mycanvas" + curtr).getContext("2d");
}
//cnv=document.getElementById('mycanvas' + curtr);
//ctx=document.getElementById("mycanvas" + curtr).getContext("2d");
//document.getElementById('mysummary').innerHTML='Accumulated Images below ...';
//prevmmode=mmode;
}
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of anothercell() at ' + (new Date()) + ' ... ';
}
}

function anothercellz() {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of anothercellz() at ' + (new Date()) + ' ... ';
}
//cnv=document.getElementById('mycanvas' + curtr);
//ctx=document.getElementById("mycanvas" + curtr).getContext("2d");
if (document.getElementById('mysummary').innerHTML.indexOf(' Images below ') == -1) {
document.getElementById('mysummary').innerHTML='Accumulated Images below ...';
}
prevmmode=mmode;
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of anothercellz() at ' + (new Date()) + ' ... ';
}
}

function onDone() {}

function onError(therr) {}

function wassetib(canvas) { //, onDone, onError) {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of wassetib(canvas) at ' + (new Date()) + ' ... ';
}
if (mmode == 'mcutpaste') {
canvas.style.border='2px dashed yellow';
thiscell(eval(canvas.id.replace('mycanvas','')));
} else {
canvas.style.border='2px dotted blue';

var type = "image/png";
var blob = new Blob([canvas], { type });
var data = [new ClipboardItem({ [type]: blob })];


navigator.clipboard.write(data).then(function () {
onDone();
}, function (err) {
onError(err);
});
}
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of wassetib(canvas) at ' + (new Date()) + ' ... ';
}
}

function setib(inb) {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of setib(inb) at ' + (new Date()) + ' ... ';
}
var inbb=inb.outerHTML.split('url(')[1].split(')')[0].replace("'","").replace("'","").replace('"','').replace('"','').replace(/\"\;/g,'');
if (mmode == 'mcutpaste') {
inb.style.border='2px dashed yellow';
thiscellis=eval(inb.id.replace('mycanvas','')); //destinationImage.click(); //url2blob(inbb);
//thiscell(eval(inb.id.replace('mycanvas','')));
destinationImage.click();
} else {
inb.style.borderColor='blue';
consolelog(inbb);
url2blob(inbb);
// var dinbb = [new ClipboardItem({ [type]: blob })];

// navigator.clipboard.write(dinbb).then(
// function () {
// /* success */
// },
// function () {
// /* failure */
// }
// );
}
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of setib(inb) at ' + (new Date()) + ' ... ';
}
}

async function url2blob(url) { // thanks to https://stackoverflow.com/questions/44070437/how-to-get-a-file-or-blob-from-an-url-in-javascript
if (aminterested == '') { aminterested='just'; }
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of url2blob(url) at ' + (new Date()) + ' ... ';
}
try {
const data = await fetch(url);
const blob = await data.blob();
await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob
})
]);
consolelog("Success.");
} catch (err) {
console.error(err.name, err.message);
}
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of url2blob(url) at ' + (new Date()) + ' ... ';
}
}

function onc(e) {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of onc(e) at ' + (new Date()) + ' ... ';
}
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
//pos3 = e.clientX;
//pos4 = e.clientY;
if (!mustnot) {
if (e.touches) {
if (e.touches[0].pageX) {
pos3 = e.touches[0].pageX;
pos4 = e.touches[0].pageY;
} else {
pos3 = e.touches[0].clientX;
pos4 = e.touches[0].clientY;
}
consolelog('pos3=' + pos3 + ',pos4=' + pos4);
} else if (e.clientX || e.clientY) {
pos3 = e.clientX;
pos4 = e.clientY;
} else {
pos3 = e.pageX;
pos4 = e.pageY;
}
}
mustnot=false;
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of onc(e) at ' + (new Date()) + ' ... ';
}
if (aminterested == aminterested.toLowerCase()) { aminterested=''; }
}

function mnoff() {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of mnoff() at ' + (new Date()) + ' ... ';
}
mustnot=false;
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of mnoff() at ' + (new Date()) + ' ... ';
}
}

function nothing() {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of nothing() at ' + (new Date()) + ' ... ';
}
mustnot=mustnot;
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of nothing() at ' + (new Date()) + ' ... ';
}
}

function consolelog(blb) {
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' started call of consolelog("' + blb + '") at ' + (new Date()) + ' ... ';
}
console.log(blb);
if (aminterested.toLowerCase() == 'just') {
document.getElementById('status').innerHTML+=' <br>' + aminterested + ' finished call of consolelog("' + blb + '") at ' + (new Date()) + ' ... ';
}
}

// addEventListener version
document.addEventListener('selectionchange', () => {
aminterested='Just';
document.getElementById('status').innerHTML=" <br>" + aminterested + " started lazy evaluation of document.addEventListener('selectionchange', () => {} at " + (new Date()) + ' ... ';
nothing();
document.getElementById('emailsms').style.display='table-cell';
document.getElementById('mytao').title=document.getSelection().toString();
aemailurl=origemailurl + encodeURIComponent(document.getSelection().toString());
document.getElementById('aemail').href=aemailurl;
asmsurl=origsmsurl + encodeURIComponent(String.fromCharCode(10) + document.getSelection().toString());
document.getElementById('asms').href=asmsurl;
consolelog(document.getSelection().toString());
document.getElementById('status').innerHTML+=" <br>" + aminterested + " finished lazy evaluation of document.addEventListener('selectionchange', () => {} at " + (new Date()) + '. ';
aminterested=aminterested.toLowerCase();
});

// addEventListener version
document.addEventListener('selectstart', () => {
aminterested='Just';
document.getElementById('status').innerHTML=" <br>" + aminterested + " started lazy evaluation of document.addEventListener('selectstart', () => {} at " + (new Date()) + ' ... ';
nothing();
document.getElementById('mytao').title='';
aemailurl=origemailurl;
asmsurl=origsmsurl;
consolelog('Selection started');
document.getElementById('status').innerHTML+=" <br>" + aminterested + " finished lazy evaluation of document.addEventListener('selectstart', () => {} at " + (new Date()) + '. ';
aminterested=aminterested.toLowerCase();
});

</script>
</body>
</html>