-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvb.js
More file actions
79 lines (68 loc) · 3.19 KB
/
vb.js
File metadata and controls
79 lines (68 loc) · 3.19 KB
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
// vb.js
document.addEventListener('DOMContentLoaded', function() {
const openVBButton = document.getElementById('openVB');
const vbOverlay = document.getElementById('vbOverlay');
const vbIframe = document.getElementById('vbIframe');
const vbReloadButton = document.getElementById('vbReload');
const vbCloseButton = document.getElementById('vbClose');
const codeMirror = document.querySelector('.CodeMirror').CodeMirror;
const vbSearchForm = document.getElementById('vbSearchForm');
const vbSearchInput = document.getElementById('vbSearchInput');
const vbBackButton = document.getElementById('vbBack'); // New
const vbForwardButton = document.getElementById('vbForward'); // New
//const vbSearchEngine = document.getElementById('vbSearchEngine'); // No longer needed
openVBButton.addEventListener('click', function() {
const htmlContent = codeMirror.getValue();
const dataURI = 'data:text/html;charset=utf-8,' + encodeURIComponent(htmlContent);
vbIframe.src = dataURI;
vbOverlay.style.display = 'flex';
});
vbReloadButton.addEventListener('click', function() {
// Correct way to reload the iframe content. IMPORTANT: Use try/catch
try {
vbIframe.contentWindow.location.reload();
} catch (error) {
console.error("Error reloading iframe:", error);
// Optionally, display an error message to the user.
alert("Iframe reload failed. The content might not be from a trusted source.");
}
});
vbSearchForm.addEventListener('submit', function(event) {
event.preventDefault();
const searchTerm = vbSearchInput.value;
//const searchEngine = vbSearchEngine.value; // No longer needed
// Always use Bing
const searchURL = 'https://www.bing.com/search?q=' + encodeURIComponent(searchTerm);
vbIframe.src = searchURL;
});
// Back button functionality
vbBackButton.addEventListener('click', function() {
try {
if (vbIframe.contentWindow.history.length > 1) {
vbIframe.contentWindow.history.back();
} else {
// Already at the beginning of history, so close the viewer
vbOverlay.style.display = 'none';
vbIframe.src = "about:blank";
}
} catch (error) {
console.error("Error going back:", error);
// Close the viewer in case of an error (e.g., cross-origin issue)
vbOverlay.style.display = 'none';
vbIframe.src = "about:blank";
}
});
vbCloseButton.addEventListener('click', function() {
vbOverlay.style.display = 'none';
vbIframe.src = "about:blank";
});
// Forward button functionality
vbForwardButton.addEventListener('click', function() {
try {
vbIframe.contentWindow.history.forward();
} catch (error) {
console.error("Error going forward:", error);
alert("Cannot go forward. Possibly at the end of history, or content from an untrusted source.");
}
});
});