From ee3bc784f35faf342df44ab570b98271481680f8 Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Thu, 24 Oct 2024 12:41:55 +0200 Subject: Update chat.v1.js --- g4f/gui/client/static/js/chat.v1.js | 54 ++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 9790b261..8402744e 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -57,6 +57,27 @@ function filter_message(text) { ) } +function fallback_copy (text) { + console.warn("Entering fallback_copy...") + var textBox = document.createElement("textbox"); + textBox.value = text; // Avoid scrolling to bottom + textBox.style.top = "0"; + textBox.style.left = "0"; + textBox.style.position = "fixed"; + document.body.appendChild(textBox); + textBox.focus(); + textBox.select(); + try { + var success = document.execCommand('copy'); + var msg = success ? 'succeeded' : 'failed'; + console.log('Fallback: Copying text command ' + msg); + } catch (err) { + console.error('Fallback: Unable to copy', err); + }; + document.body.removeChild(textBox); + +} + hljs.addPlugin(new CopyButtonPlugin()); let typesetPromise = Promise.resolve(); const highlight = (container) => { @@ -100,6 +121,37 @@ const register_message_buttons = async () => { }) } }); + + + document.querySelectorAll(".message .fa-clipboard").forEach(async (el) => { + if (!("click" in el.dataset)) { + el.dataset.click = "true"; + el.addEventListener("click", async () => { + const message_el = el.parentElement.parentElement.parentElement; + try { + const copyText = await get_message(window.conversation_id, message_el.dataset.index); + } catch(e) { + console.error(e); + }; + + try { + console.warn("copyText type: ", typeof copyText) + console.warn("Current Text: " + copyText); + await navigator.clipboard.writeText(copyText); + } catch (e) { + console.error("Clipboard API failed!") + console.error(e); + fallback_copy(copyText); + }; + + el.classList.add("clicked"); + setTimeout(() => el.classList.remove("clicked"), 1000); + }) + } + }); + + + document.querySelectorAll(".message .fa-volume-high").forEach(async (el) => { if (!("click" in el.dataset)) { el.dataset.click = "true"; @@ -1424,4 +1476,4 @@ if (SpeechRecognition) { recognition.start(); } }); -} \ No newline at end of file +} -- cgit v1.2.3 From 89b5a6353ed7a83bd388b01da523b52774cb7cc5 Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Thu, 24 Oct 2024 12:47:07 +0200 Subject: Update chat.v1.js --- g4f/gui/client/static/js/chat.v1.js | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 8402744e..da990024 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -109,19 +109,6 @@ const register_message_buttons = async () => { }) } }); - document.querySelectorAll(".message .fa-clipboard").forEach(async (el) => { - if (!("click" in el.dataset)) { - el.dataset.click = "true"; - el.addEventListener("click", async () => { - const message_el = el.parentElement.parentElement.parentElement; - const copyText = await get_message(window.conversation_id, message_el.dataset.index); - navigator.clipboard.writeText(copyText); - el.classList.add("clicked"); - setTimeout(() => el.classList.remove("clicked"), 1000); - }) - } - }); - document.querySelectorAll(".message .fa-clipboard").forEach(async (el) => { if (!("click" in el.dataset)) { -- cgit v1.2.3 From c5e356a3219ae86b18bb366c47ccb0a36f8a651a Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Thu, 24 Oct 2024 13:40:23 +0200 Subject: Update chat.v1.js --- g4f/gui/client/static/js/chat.v1.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index da990024..5309a04d 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -59,7 +59,7 @@ function filter_message(text) { function fallback_copy (text) { console.warn("Entering fallback_copy...") - var textBox = document.createElement("textbox"); + var textBox = document.createElement("textarea"); textBox.value = text; // Avoid scrolling to bottom textBox.style.top = "0"; textBox.style.left = "0"; @@ -115,11 +115,7 @@ const register_message_buttons = async () => { el.dataset.click = "true"; el.addEventListener("click", async () => { const message_el = el.parentElement.parentElement.parentElement; - try { const copyText = await get_message(window.conversation_id, message_el.dataset.index); - } catch(e) { - console.error(e); - }; try { console.warn("copyText type: ", typeof copyText) -- cgit v1.2.3 From 51d6f285117753f556bf9c1f2072bac8719f626e Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Fri, 25 Oct 2024 14:10:56 +0200 Subject: Undo minify highlightjs-copy.min.js --- g4f/gui/client/static/js/highlightjs-copy.min.js | 45 +++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/highlightjs-copy.min.js b/g4f/gui/client/static/js/highlightjs-copy.min.js index ac11d33e..f74a6172 100644 --- a/g4f/gui/client/static/js/highlightjs-copy.min.js +++ b/g4f/gui/client/static/js/highlightjs-copy.min.js @@ -1 +1,44 @@ -class CopyButtonPlugin{constructor(options={}){self.hook=options.hook;self.callback=options.callback}"after:highlightElement"({el,text}){let button=Object.assign(document.createElement("button"),{innerHTML:"Copy",className:"hljs-copy-button"});button.dataset.copied=false;el.parentElement.classList.add("hljs-copy-wrapper");el.parentElement.appendChild(button);el.parentElement.style.setProperty("--hljs-theme-background",window.getComputedStyle(el).backgroundColor);button.onclick=function(){if(!navigator.clipboard)return;let newText=text;if(hook&&typeof hook==="function"){newText=hook(text,el)||text}navigator.clipboard.writeText(newText).then(function(){button.innerHTML="Copied!";button.dataset.copied=true;let alert=Object.assign(document.createElement("div"),{role:"status",className:"hljs-copy-alert",innerHTML:"Copied to clipboard"});el.parentElement.appendChild(alert);setTimeout(()=>{button.innerHTML="Copy";button.dataset.copied=false;el.parentElement.removeChild(alert);alert=null},2e3)}).then(function(){if(typeof callback==="function")return callback(newText,el)})}}} \ No newline at end of file +class CopyButtonPlugin { + constructor(options = {}) { + self.hook = options.hook; + self.callback = options.callback + } + "after:highlightElement"({ + el, + text + }) { + let button = Object.assign(document.createElement("button"), { + innerHTML: "Copy", + className: "hljs-copy-button" + }); + button.dataset.copied = false; + el.parentElement.classList.add("hljs-copy-wrapper"); + el.parentElement.appendChild(button); + el.parentElement.style.setProperty("--hljs-theme-background", window.getComputedStyle(el).backgroundColor); + button.onclick = function () { + if (!navigator.clipboard) return; + let newText = text; + if (hook && typeof hook === "function") { + newText = hook(text, el) || text + } + navigator.clipboard.writeText(newText).then(function () { + button.innerHTML = "Copied!"; + button.dataset.copied = true; + let alert = Object.assign(document.createElement("div"), { + role: "status", + className: "hljs-copy-alert", + innerHTML: "Copied to clipboard" + }); + el.parentElement.appendChild(alert); + setTimeout(() => { + button.innerHTML = "Copy"; + button.dataset.copied = false; + el.parentElement.removeChild(alert); + alert = null + }, 2e3) + }).then(function () { + if (typeof callback === "function") return callback(newText, el) + }) + } + } +} -- cgit v1.2.3 From e9ecdfe5dd9c1ee15337fc70c71ddc50ec78bb29 Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Fri, 25 Oct 2024 14:14:47 +0200 Subject: Update highlightjs-copy.min.js --- g4f/gui/client/static/js/highlightjs-copy.min.js | 28 ++++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/highlightjs-copy.min.js b/g4f/gui/client/static/js/highlightjs-copy.min.js index f74a6172..20b7d52b 100644 --- a/g4f/gui/client/static/js/highlightjs-copy.min.js +++ b/g4f/gui/client/static/js/highlightjs-copy.min.js @@ -15,13 +15,25 @@ class CopyButtonPlugin { el.parentElement.classList.add("hljs-copy-wrapper"); el.parentElement.appendChild(button); el.parentElement.style.setProperty("--hljs-theme-background", window.getComputedStyle(el).backgroundColor); - button.onclick = function () { - if (!navigator.clipboard) return; + button.onclick = async () => { + if (!navigator.clipboard) { + console.error("navigator.clipboard: Clipboard API unavailable.") + return; + } let newText = text; if (hook && typeof hook === "function") { newText = hook(text, el) || text } - navigator.clipboard.writeText(newText).then(function () { + + try { + console.warn("newText type: ", typeof newText); + console.warn("Current Text: " + newText); + await navigator.clipboard.writeText(newText); + } catch (e) { + console.error("Clipboard API writeText failed!!") + console.error(e); + fallback_copy(newText); + } button.innerHTML = "Copied!"; button.dataset.copied = true; let alert = Object.assign(document.createElement("div"), { @@ -36,9 +48,11 @@ class CopyButtonPlugin { el.parentElement.removeChild(alert); alert = null }, 2e3) - }).then(function () { - if (typeof callback === "function") return callback(newText, el) - }) + } + + + if (typeof callback === "function") return callback(newText, el); + } - } + } -- cgit v1.2.3 From 6bf6c5e5e97633050b0bdf61f0c4730b40c08851 Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Sat, 26 Oct 2024 12:29:58 +0200 Subject: Clean up chat.v1.js --- g4f/gui/client/static/js/chat.v1.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 5309a04d..2777e79a 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -57,10 +57,9 @@ function filter_message(text) { ) } -function fallback_copy (text) { - console.warn("Entering fallback_copy...") +function fallback_clipboard (text) { var textBox = document.createElement("textarea"); - textBox.value = text; // Avoid scrolling to bottom + textBox.value = text; textBox.style.top = "0"; textBox.style.left = "0"; textBox.style.position = "fixed"; @@ -70,12 +69,11 @@ function fallback_copy (text) { try { var success = document.execCommand('copy'); var msg = success ? 'succeeded' : 'failed'; - console.log('Fallback: Copying text command ' + msg); - } catch (err) { - console.error('Fallback: Unable to copy', err); - }; + console.log('Clipboard Fallback: Copying text command ' + msg); + } catch (e) { + console.error('Clipboard Fallback: Unable to copy', e); + } document.body.removeChild(textBox); - } hljs.addPlugin(new CopyButtonPlugin()); @@ -118,14 +116,12 @@ const register_message_buttons = async () => { const copyText = await get_message(window.conversation_id, message_el.dataset.index); try { - console.warn("copyText type: ", typeof copyText) - console.warn("Current Text: " + copyText); await navigator.clipboard.writeText(copyText); } catch (e) { - console.error("Clipboard API failed!") console.error(e); - fallback_copy(copyText); - }; + console.error("Clipboard API failed! Fallback to document.exec("copy")..."); + fallback_clipboard(copyText); + } el.classList.add("clicked"); setTimeout(() => el.classList.remove("clicked"), 1000); @@ -133,8 +129,6 @@ const register_message_buttons = async () => { } }); - - document.querySelectorAll(".message .fa-volume-high").forEach(async (el) => { if (!("click" in el.dataset)) { el.dataset.click = "true"; -- cgit v1.2.3 From 504b0dbf5b702532e545b8f710b7cfbb657d3200 Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Sat, 26 Oct 2024 12:39:40 +0200 Subject: Clean up highlightjs-copy.min.js --- g4f/gui/client/static/js/highlightjs-copy.min.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/highlightjs-copy.min.js b/g4f/gui/client/static/js/highlightjs-copy.min.js index 20b7d52b..0c3c1cee 100644 --- a/g4f/gui/client/static/js/highlightjs-copy.min.js +++ b/g4f/gui/client/static/js/highlightjs-copy.min.js @@ -26,13 +26,11 @@ class CopyButtonPlugin { } try { - console.warn("newText type: ", typeof newText); - console.warn("Current Text: " + newText); await navigator.clipboard.writeText(newText); } catch (e) { - console.error("Clipboard API writeText failed!!") console.error(e); - fallback_copy(newText); + console.error("Clipboard API writeText() failed! Fallback to document.exec(\"copy\")..."); + fallback_clipboard(newText); } button.innerHTML = "Copied!"; button.dataset.copied = true; -- cgit v1.2.3 From 6f0eae8a067879758bb65ce9f21e6223ba8917e5 Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Sat, 26 Oct 2024 12:42:23 +0200 Subject: Clean up chat.v1.js --- g4f/gui/client/static/js/chat.v1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 2777e79a..9682de59 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -119,7 +119,7 @@ const register_message_buttons = async () => { await navigator.clipboard.writeText(copyText); } catch (e) { console.error(e); - console.error("Clipboard API failed! Fallback to document.exec("copy")..."); + console.error("Clipboard API writeText() failed! Fallback to document.exec(\"copy\")..."); fallback_clipboard(copyText); } -- cgit v1.2.3 From 75e9a237261f7175e5add7d8ee1f81f35e095224 Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Sat, 26 Oct 2024 13:26:04 +0200 Subject: Update highlightjs-copy.min.js --- g4f/gui/client/static/js/highlightjs-copy.min.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/highlightjs-copy.min.js b/g4f/gui/client/static/js/highlightjs-copy.min.js index 0c3c1cee..cd8ae957 100644 --- a/g4f/gui/client/static/js/highlightjs-copy.min.js +++ b/g4f/gui/client/static/js/highlightjs-copy.min.js @@ -16,16 +16,14 @@ class CopyButtonPlugin { el.parentElement.appendChild(button); el.parentElement.style.setProperty("--hljs-theme-background", window.getComputedStyle(el).backgroundColor); button.onclick = async () => { - if (!navigator.clipboard) { - console.error("navigator.clipboard: Clipboard API unavailable.") - return; - } let newText = text; if (hook && typeof hook === "function") { newText = hook(text, el) || text } - - try { + try { + if (!navigator.clipboard) { + throw new Error("navigator.clipboard: Clipboard API unavailable."); + } await navigator.clipboard.writeText(newText); } catch (e) { console.error(e); -- cgit v1.2.3 From 8d010d892777ab7e191ade861ef1f357a54b3fe4 Mon Sep 17 00:00:00 2001 From: foxfire52 Date: Sat, 26 Oct 2024 13:28:08 +0200 Subject: Update chat.v1.js --- g4f/gui/client/static/js/chat.v1.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 9682de59..d5a1ffaa 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -115,7 +115,10 @@ const register_message_buttons = async () => { const message_el = el.parentElement.parentElement.parentElement; const copyText = await get_message(window.conversation_id, message_el.dataset.index); - try { + try { + if (!navigator.clipboard) { + throw new Error("navigator.clipboard: Clipboard API unavailable."); + } await navigator.clipboard.writeText(copyText); } catch (e) { console.error(e); -- cgit v1.2.3