mirror of
https://github.com/gigirassy/plaster.git
synced 2024-12-23 18:39:07 +00:00
Update client.js
This commit is contained in:
parent
7a1cb27f5b
commit
b5e185ab6b
@ -1,23 +1,75 @@
|
|||||||
document.getElementById('fetchBtn').addEventListener('click', () => {
|
document.addEventListener('DOMContentLoaded', async () => {
|
||||||
const urlInput = document.getElementById('pasteUrl').value;
|
const fetchBtn = document.getElementById('fetchBtn');
|
||||||
|
const pasteUrlInput = document.getElementById('pasteUrl');
|
||||||
|
const autoCopyCheckbox = document.getElementById('autoCopyCheckbox');
|
||||||
|
|
||||||
// Extract the Paste ID from the provided Pastebin URL
|
// Load default state for the checkbox
|
||||||
const pasteId = getPasteId(urlInput);
|
const defaultAutoCopy = await getDefaultAutoCopy();
|
||||||
|
const userPreference = getCookie('autoCopyEnabled');
|
||||||
|
autoCopyCheckbox.checked = userPreference !== null ? userPreference === 'true' : defaultAutoCopy;
|
||||||
|
|
||||||
if (pasteId) {
|
// Save user preference to a cookie when the checkbox changes
|
||||||
window.location.href = `/${pasteId}`;
|
autoCopyCheckbox.addEventListener('change', () => {
|
||||||
} else {
|
setCookie('autoCopyEnabled', autoCopyCheckbox.checked, 365);
|
||||||
alert('Please enter a valid Pastebin URL.');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redirect to raw paste page
|
||||||
|
const rawPasteUrl = `/${pasteId}`;
|
||||||
|
if (autoCopyCheckbox.checked) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(rawPasteUrl);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.location.href = `/${pasteId}`
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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
|
||||||
function getPasteId(url) {
|
function getPasteId(url) {
|
||||||
// Check if the URL is a valid Pastebin URL and extract the Paste ID
|
|
||||||
const regex = /pastebin\.com\/(?:raw\/)?([a-zA-Z0-9]+)/;
|
const regex = /pastebin\.com\/(?:raw\/)?([a-zA-Z0-9]+)/;
|
||||||
const match = url.match(regex);
|
const match = url.match(regex);
|
||||||
|
return match ? match[1] : null;
|
||||||
|
}
|
||||||
|
|
||||||
if (match) {
|
// Utility functions to manage cookies
|
||||||
return match[1]; // Return the Paste ID
|
function setCookie(name, value, days) {
|
||||||
}
|
const expires = new Date(Date.now() + days * 86400000).toUTCString();
|
||||||
return null; // Invalid Pastebin URL
|
document.cookie = `${name}=${value}; expires=${expires}; path=/`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCookie(name) {
|
||||||
|
const match = document.cookie.match(new RegExp(`(?:^|; )${name}=([^;]*)`));
|
||||||
|
return match ? decodeURIComponent(match[1]) : null;
|
||||||
|
Loading…
Reference in New Issue
Block a user