File size: 2,566 Bytes
3ec47db
b9c61c0
 
 
3ec47db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9c61c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

// 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);
        });
    });
}