cw332's picture
PERFORM THE FOLLOWING OPERATIONS:
3ec47db verified
// Shared functionality across pages
console.log('GhostSurf proxy service initialized');
// Proxy URL validator
function isValidProxyUrl(url) {
try {
const urlObj = new URL(url);
return ['http:', 'https:'].includes(urlObj.protocol);
} catch {
return false;
}
}
// Initialize proxy form
function initProxyForm() {
const form = document.getElementById('proxyForm');
if (form) {
form.addEventListener('submit', function(e) {
e.preventDefault();
const urlInput = document.getElementById('url');
const url = urlInput.value.trim();
if (!isValidProxyUrl(url)) {
alert('Please enter a valid http:// or https:// URL');
return;
}
// Ensure URL has protocol
const fullUrl = url.startsWith('http') ? url : `https://${url}`;
// Show loading state
form.classList.add('submitting');
// Call proxy endpoint
fetch(`/proxy?url=${encodeURIComponent(fullUrl)}`)
.then(response => {
if (!response.ok) throw new Error('Network response was not ok');
return response.text();
})
.then(html => {
// Create a new document with the proxied content
const proxiedDoc = document.open('text/html', 'replace');
proxiedDoc.write(html);
proxiedDoc.close();
})
.catch(error => {
console.error('Proxy error:', error);
alert('Error loading the requested URL. Please try again.');
})
.finally(() => {
form.classList.remove('submitting');
});
});
}
}
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
initProxyForm();
});
// Form validation for proxy input
function validateUrl(url) {
try {
new URL(url);
return true;
} catch (e) {
return false;
}
}
// Service worker registration for offline capabilities
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js').then(registration => {
console.log('ServiceWorker registration successful');
}).catch(err => {
console.log('ServiceWorker registration failed: ', err);
});
});
}