From 003a84bd5d046b690b386a9f716bd8f1e8c6a32f Mon Sep 17 00:00:00 2001 From: MOHAMED ALSHLKANY <120714408+ALSHLKANY@users.noreply.github.com> Date: Tue, 19 Mar 2024 07:05:58 +0200 Subject: [PATCH] FIX copy button Added fallback mechanism for browsers without clipboard API or insecure connections --- main.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index c2f90f7..84a0df3 100644 --- a/main.js +++ b/main.js @@ -490,11 +490,25 @@ function view_problems(problems) { let btn = codes_btns[i]; btn.addEventListener("click", () => { + // Check if clipboard API is supported [requires HTTPS] + if (navigator.clipboard) { navigator.clipboard.writeText(showed_codes[i]); - Swal.fire({ - title: "Code Copied", - icon: "success", - }); + } + // Fallback is the solution for browsers without clipboard API or insecure connections + else { + const textArea = document.createElement("textarea"); + textArea.value = showed_codes[i]; + document.body.appendChild(textArea); + textArea.select(); + + document.execCommand("copy"); + + document.body.removeChild(textArea); + } + Swal.fire({ + title: "Code Copied", + icon: "success", + }); }); } } @@ -523,4 +537,4 @@ document.querySelector("#github span").addEventListener("click", () => { document.querySelector("#github img").addEventListener("click", () => { window.open("https://github.com/Mohab96", "_blank"); -}); \ No newline at end of file +});