Fix copying on IE8

This commit is contained in:
ReimarPB 2022-11-03 21:05:54 +01:00
parent eb21cd3496
commit 8aee61c01c
2 changed files with 32 additions and 19 deletions

View File

@ -37,6 +37,10 @@ if (!document.head) {
document.head = document.getElementsByTagName("head")[0];
}
if (!window.HTMLPictureElement) {
document.createElement("picture");
}
function toArray(iter) {
var arr = [];
for (var i = 0; i < iter.length; i++) arr.push(iter[i]);

View File

@ -5,12 +5,13 @@ window.addEventListener("load", function() {
for (var i = 0; i < codes.length; i++) {
var code = codes[i];
var input = code.children[1].children[0];
var input = code.getElementsByTagName("input")[0];
// Select all in input when clicked
input.onclick = function(event) {
console.log(event);
inputSelectAll(event.target);
input.onclick = function(e) {
var event = e || window.event;
var target = event.target || event.srcElement;
inputSelectAll(target);
}
// Add clipboard icon
@ -33,11 +34,14 @@ window.addEventListener("load", function() {
img.src = base + "copy.png";
img.alt = "Clipboard Icon";
img.title = "Copy to clipboard";
img.onclick = function(event) {
img.onclick = function(e) {
var event = e || window.event;
var target = event.target || event.srcElement;
copyInputText(
event.target.parentElement.parentElement.children[1].children[0],
event.target,
event.target.getAttribute("data-theme")
target.parentElement.parentElement.getElementsByTagName('input')[0],
target,
target.getAttribute("data-theme")
);
};
@ -54,23 +58,23 @@ window.addEventListener("load", function() {
function copyInputText(input, clipboardIcon, theme) {
inputSelectAll(input);
inputSelectAll(input);
var path;
if (document.execCommand("copy")) {
path = "/assets/icons/" + theme + "-mode/";
} else {
console.log("no"); // TODO X icon
}
var path;
if (document.execCommand("copy")) {
path = "/assets/icons/" + theme + "-mode/";
} else {
console.log("no"); // TODO X icon
}
clipboardIcon.src = path + "check.svg";
clipboardIcon.previousElementSibling.srcset = path + "check.png";
clipboardIcon.previousSibling.srcset = path + "check.png";
input.setSelectionRange(0, 0);
inputDeselect(input);
setTimeout(function() {
setTimeout(function() {
clipboardIcon.src = path + "copy.svg";
clipboardIcon.previousElementSibling.srcset = path + "copy.png";
clipboardIcon.previousSibling.srcset = path + "copy.png";
}, 1000);
}
@ -80,3 +84,8 @@ function inputSelectAll(input) {
if (input.select) input.select();
else input.setSelectionRange(0, this.value.length);
}
function inputDeselect(input) {
if (input.blur) input.blur();
else input.setSelectionRange(0, 0);
}