From 8aee61c01c903229078133a74776c2be73fb568f Mon Sep 17 00:00:00 2001 From: ReimarPB Date: Thu, 3 Nov 2022 21:05:54 +0100 Subject: [PATCH] Fix copying on IE8 --- assets/script/compatibility.js | 4 +++ assets/script/inline-code.js | 47 ++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/assets/script/compatibility.js b/assets/script/compatibility.js index 6216159..2c141f9 100644 --- a/assets/script/compatibility.js +++ b/assets/script/compatibility.js @@ -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]); diff --git a/assets/script/inline-code.js b/assets/script/inline-code.js index 61849ad..e5d1cc3 100644 --- a/assets/script/inline-code.js +++ b/assets/script/inline-code.js @@ -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); +}