|
|
|
|
|
|
|
|
console.log('GhostSurf proxy service initialized'); |
|
|
|
|
|
|
|
|
function isValidProxyUrl(url) { |
|
|
try { |
|
|
const urlObj = new URL(url); |
|
|
return ['http:', 'https:'].includes(urlObj.protocol); |
|
|
} catch { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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; |
|
|
} |
|
|
|
|
|
|
|
|
const fullUrl = url.startsWith('http') ? url : `https://${url}`; |
|
|
|
|
|
|
|
|
form.classList.add('submitting'); |
|
|
|
|
|
|
|
|
fetch(`/proxy?url=${encodeURIComponent(fullUrl)}`) |
|
|
.then(response => { |
|
|
if (!response.ok) throw new Error('Network response was not ok'); |
|
|
return response.text(); |
|
|
}) |
|
|
.then(html => { |
|
|
|
|
|
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'); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
initProxyForm(); |
|
|
}); |
|
|
|
|
|
function validateUrl(url) { |
|
|
try { |
|
|
new URL(url); |
|
|
return true; |
|
|
} catch (e) { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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); |
|
|
}); |
|
|
}); |
|
|
} |