File size: 5,558 Bytes
374c4fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36715b4
 
374c4fc
36715b4
374c4fc
 
 
6ff5a44
36715b4
 
 
 
374c4fc
36715b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374c4fc
36715b4
 
374c4fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36715b4
6ff5a44
 
 
36715b4
 
 
 
6ff5a44
374c4fc
36715b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374c4fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
document.addEventListener('DOMContentLoaded', () => {
    const webview = document.getElementById('webview');
    const urlInput = document.querySelector('input[type="text"]');
    const backButton = document.querySelector('button:nth-of-type(1)');
    const forwardButton = document.querySelector('button:nth-of-type(2)');
    const refreshButton = document.querySelector('button:nth-of-type(3)');
    const statusText = document.getElementById('loading-status');
    const zoomText = document.getElementById('zoom-level');
    
    let currentZoom = 100;

    // Navigation controls
    backButton.addEventListener('click', () => {
        webview.contentWindow.history.back();
    });

    forwardButton.addEventListener('click', () => {
        webview.contentWindow.history.forward();
    });

    refreshButton.addEventListener('click', () => {
        webview.src = webview.src;
    });
    // URL navigation
    urlInput.addEventListener('keydown', (e) => {
        if (e.key === 'Enter') {
            let url = urlInput.value.trim();
            statusText.textContent = 'Loading...';
            statusText.classList.add('animate-pulse');
            
            // Add protocol if missing
            if (!url.startsWith('http://') && !url.startsWith('https://')) {
                url = 'https://' + url;
            }

            try {
                // Create URL object to validate
                new URL(url);
                webview.src = url;
            } catch (error) {
                // If URL is invalid, try with www prefix
                if (!url.includes('www.')) {
                    try {
                        url = url.replace('://', '://www.');
                        new URL(url);
                        webview.src = url;
                    } catch (e) {
                        // If still invalid, try Google search
                        if (!url.startsWith('http')) {
                            webview.src = `https://www.google.com/search?q=${encodeURIComponent(urlInput.value)}`;
                        } else {
                            statusText.textContent = 'Invalid URL';
                            statusText.classList.remove('animate-pulse');
                        }
                    }
                } else {
                    statusText.textContent = 'Invalid URL';
                    statusText.classList.remove('animate-pulse');
                }
            }
        }
});

    // Update UI based on webview events
    webview.addEventListener('loadstart', () => {
        statusText.textContent = 'Loading...';
        statusText.classList.add('animate-pulse');
    });

    webview.addEventListener('load', () => {
        statusText.textContent = 'Done';
        statusText.classList.remove('animate-pulse');
        urlInput.value = webview.src;
    });
    webview.addEventListener('loaderror', () => {
        statusText.textContent = 'Failed to load';
        statusText.classList.remove('animate-pulse');
        
        // Try falling back to http if https failed
        if (webview.src.startsWith('https://')) {
            const httpUrl = webview.src.replace('https://', 'http://');
            webview.src = httpUrl;
        } else if (!webview.src.includes('google.com/search')) {
            // If still failing, try Google search
            webview.src = `https://www.google.com/search?q=${encodeURIComponent(urlInput.value)}`;
        }
    });
    // Update webview URL when navigation happens inside iframe
    webview.addEventListener('load', () => {
        try {
            urlInput.value = webview.src;
            statusText.textContent = 'Done';
            statusText.classList.remove('animate-pulse');
            
            // Update security indicator
            const securityIndicator = document.querySelector('#page-status i');
            if (webview.src.startsWith('https://')) {
                securityIndicator.classList.remove('text-red-500');
                securityIndicator.classList.add('text-green-500');
                securityIndicator.setAttribute('data-feather', 'shield');
                feather.replace();
            } else {
                securityIndicator.classList.remove('text-green-500');
                securityIndicator.classList.add('text-red-500');
                securityIndicator.setAttribute('data-feather', 'alert-triangle');
                feather.replace();
            }
        } catch (e) {
            // Cross-origin error, can't access iframe src
            console.log('Cross-origin iframe, cannot inspect URL');
        }
    });

    // Zoom functionality
document.addEventListener('keydown', (e) => {
        if (e.ctrlKey) {
            if (e.key === '+' && currentZoom < 200) {
                currentZoom += 10;
                updateZoom();
                e.preventDefault();
            } else if (e.key === '-' && currentZoom > 50) {
                currentZoom -= 10;
                updateZoom();
                e.preventDefault();
            } else if (e.key === '0') {
                currentZoom = 100;
                updateZoom();
                e.preventDefault();
            }
        }
    });

    function updateZoom() {
        webview.style.transform = `scale(${currentZoom / 100})`;
        webview.style.transformOrigin = 'top left';
        zoomText.textContent = `${currentZoom}%`;
    }

    // Dark mode toggle (will be used by navbar component)
    window.toggleDarkMode = function() {
        document.documentElement.classList.toggle('dark');
    };
});