async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
var notyf = new Notyf({
types: [
{
type: 'info',
background: 'blue',
icon: {
className: 'material-icons',
tagName: 'i',
text: 'info'
}
},
{
type: 'warning',
background: 'orange',
icon: {
className: 'material-icons',
tagName: 'i',
text: 'warning'
}
}
]
});
// Function to show the success message
function showMessage(msgtype, msg) {
var showMsg = document.getElementById("showMsg");
// Check if the success message should be displayed
if (showMsg.dataset.display !== 'false') {
// Use Notyf for displaying success message
if (msgtype == 'success') {
notyf.success({
message: msg,
duration: 9500,
dismissible: true
});
} else if (msgtype == 'warn') {
notyf.open({
type: "warning",
message: msg,
duration: 9500,
dismissible: true
});
} else if (msgtype == 'info') {
notyf.open({
type: "info",
message: msg,
duration: 9500,
dismissible: true
});
} else if (msgtype == 'error') {
notyf.error({
message: msg,
duration: 9500,
dismissible: true
});
}
}
}
function copyToClipboard(text) {
// Create a temporary input element
const tempInput = document.createElement('input');
tempInput.value = text;
// Append the input element to the DOM
document.body.appendChild(tempInput);
// Select the text inside the input element
tempInput.select();
tempInput.setSelectionRange(0, 99999); // For mobile devices
// Copy the selected text to the clipboard
document.execCommand('copy');
// Remove the temporary input element from the DOM
document.body.removeChild(tempInput);
// Show a notification or perform any other action if needed
showMessage('success', 'Joke have been automatically copied to your clipboard!');
//showMessage('warn', 'Joke copied to clipboard!');
//showMessage('info', 'Joke copied to clipboard!');
//showMessage('error', 'Joke copied to clipboard!');
}
async function showJoke(joke) {
console.log(joke);
var jokeElement = document.getElementById('joke');
jokeElement.innerHTML = joke;
}
// Add this function to your existing JavaScript file
async function handleButtonClick(type, message, joke) {
function showMsg() {
showMessage(type, message);
}
showMsg();
//showJoke(joke);
try {
// Fetch a new joke from the server
const response = await fetch('/joke');
if (!response.ok) {
throw new Error('Failed to fetch new joke');
}
// Parse the response text (assuming it's a plain string)
const newJoke = await response.text();
copyToClipboard(newJoke);
// Run showJoke() with the new joke
showJoke(newJoke);
} catch (error) {
console.error('Error fetching or processing new joke:', error);
}
}
Full Error:
I have it included in the html file:
Javascript: