plaster/public/client.js

92 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2024-12-20 00:15:55 +00:00
document.addEventListener('DOMContentLoaded', async () => {
const fetchBtn = document.getElementById('fetchBtn');
const pasteUrlInput = document.getElementById('pasteUrl');
const autoCopyCheckbox = document.getElementById('autoCopyCheckbox');
// Load default state for the checkbox
const defaultAutoCopy = await getDefaultAutoCopy();
const userPreference = getCookie('autoCopyEnabled');
autoCopyCheckbox.checked = userPreference !== null ? userPreference === 'true' : defaultAutoCopy;
// Save user preference to a cookie when the checkbox changes
autoCopyCheckbox.addEventListener('change', () => {
setCookie('autoCopyEnabled', autoCopyCheckbox.checked, 365);
});
2024-12-20 04:46:42 +00:00
// Fetch ASCII art and display it
try {
const response = await fetch('/ascii');
const data = await response.json();
if (data.enabled) {
const asciiArtContainer = document.getElementById('asciiArtContainer');
asciiArtContainer.textContent = data.art;
}
} catch (error) {
console.error('Failed to fetch ASCII art:', error);
}
2024-12-20 00:15:55 +00:00
// Handle "Go to Raw Paste" button click
fetchBtn.addEventListener('click', async () => {
const urlInput = pasteUrlInput.value.trim();
const pasteId = getPasteId(urlInput);
if (!pasteId) {
alert('Please enter a valid Pastebin URL.');
return;
}
2024-12-20 00:39:15 +00:00
const frontendUrl = `/${pasteId}`;
// If auto-copy is enabled, fetch and copy the paste contents
2024-12-20 00:15:55 +00:00
if (autoCopyCheckbox.checked) {
try {
2024-12-20 00:39:15 +00:00
const response = await fetch(frontendUrl);
2024-12-20 00:15:55 +00:00
if (response.ok) {
const pasteContents = await response.text();
await navigator.clipboard.writeText(pasteContents);
alert('Raw paste contents copied to clipboard!');
} else {
alert('Failed to fetch paste contents.');
}
} catch (error) {
alert('Error copying to clipboard: ' + error.message);
}
}
2024-12-20 00:39:15 +00:00
// Redirect to the frontend paste URL
window.location.href = frontendUrl;
2024-12-20 00:15:55 +00:00
});
2024-12-18 21:21:37 +00:00
});
2024-12-18 22:01:02 +00:00
2024-12-20 00:15:55 +00:00
// Utility function to get the default auto-copy value from the server
async function getDefaultAutoCopy() {
try {
const response = await fetch('/auto-copy-default');
if (response.ok) {
const data = await response.json();
return data.autoCopyDefault;
}
} catch (error) {
console.error('Error fetching auto-copy default:', error);
}
return false; // Default fallback
}
// Extract Paste ID from the given Pastebin URL
2024-12-18 22:01:02 +00:00
function getPasteId(url) {
const regex = /pastebin\.com\/(?:raw\/)?([a-zA-Z0-9]+)/;
const match = url.match(regex);
2024-12-20 00:15:55 +00:00
return match ? match[1] : null;
2024-12-18 22:01:02 +00:00
}
2024-12-20 00:15:55 +00:00
// Utility functions to manage cookies
function setCookie(name, value, days) {
const expires = new Date(Date.now() + days * 86400000).toUTCString();
document.cookie = `${name}=${value}; expires=${expires}; path=/`;
}
function getCookie(name) {
const match = document.cookie.match(new RegExp(`(?:^|; )${name}=([^;]*)`));
return match ? decodeURIComponent(match[1]) : null;
2024-12-20 00:39:15 +00:00
}