I made a javascript tool which converts images into base64 tags:
function readFile() {
if (this.files && this.files[0]) {
var FR= new FileReader();
FR.addEventListener("load", function(e) {
document.getElementById("img").src = e.target.result;
document.getElementById("b64").innerHTML = e.target.result;
});
FR.readAsDataURL( this.files[0] );
}
}
window.addEventListener('paste', e => {
document.getElementById("inp").files = e.clipboardData.files;
});
document.getElementById("inp").addEventListener("change", readFile);
function copyData() {
var copyText = document.getElementById("b64");
copyText.select();
copyText.setSelectionRange(0, 9999999999)
document.execCommand("copy");
}
function generateTag() {
document.getElementById("tag").innerHTML = "<img src='" + document.getElementById('b64').value + "'/>";
}
function copyTag() {
var copyText = document.getElementById("tag");
copyText.select();
copyText.setSelectionRange(0, 9999999999)
document.execCommand("copy");
}
and the html body:
<input id="inp" type='file'><br>
<button onclick="copyData()">Copy data</button>
<button onclick="generateTag(); copyTag()">Generate image tag and copy</button>
<br>
<textarea readonly placeholder="Base64 data" id="b64" rows="6"></textarea>
<textarea readonly placeholder="HTML image tag" id="tag" rows="6"></textarea>
<img id="img">
There is a problem, reading and converting images to code by clicking Select file
works fine, but through function copyData()
pasting a copied image from clipboard can be read, but it does not get converted. How can I fix this?