1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- let templateFn = (url) => {
- return `
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Webview Preview</title>
- <style>
- body {
- margin: 0;
- padding: 0;
- font-family: Arial, sans-serif;
- display: flex;
- flex-direction: column;
- height: 100vh;
- align-items: center;
- justify-content: flex-start;
- padding-top: 10px;
- }
- #command-line {
- padding: 10px;
- font-size: 16px;
- width: 80%;
- max-width: 600px;
- border: 1px solid #ccc;
- border-radius: 4px;
- margin-bottom: 10px;
- }
- #refresh-button {
- padding: 10px 20px;
- font-size: 16px;
- background-color: #4CAF50;
- color: white;
- border: none;
- border-radius: 4px;
- cursor: pointer;
- margin-bottom: 10px;
- }
- #refresh-button:hover {
- background-color: #45a049;
- }
- iframe {
- flex-grow: 1;
- width: 100%;
- height: 100%;
- border: none;
- }
- </style>
- </head>
- <body>
- <input id="command-line" type="text" value="${url}" placeholder="Enter URL here" />
- <button id="refresh-button">Refresh</button>
- <iframe id="iframe" src="${url}" sandbox="allow-scripts allow-same-origin"></iframe>
- <script>
- const vscode = acquireVsCodeApi();
- const commandLine = document.getElementById('command-line');
- const refreshButton = document.getElementById('refresh-button');
- const iframe = document.getElementById('iframe');
- function refreshIframe() {
- const newUrl = commandLine.value.trim();
- if (newUrl) {
- iframe.src = newUrl; // Update iframe directly
- vscode.postMessage({ command: 'refreshUrl', url: newUrl }); // Send update message to VSCode extension
- }
- }
- // Refresh on button click
- refreshButton.addEventListener('click', refreshIframe);
- // Refresh on pressing Enter
- commandLine.addEventListener('keypress', (event) => {
- if (event.key === 'Enter') {
- refreshIframe();
- }
- });
- // Update input field when the iframe's URL changes (only works for same-origin pages)
- iframe.addEventListener('load', () => {
- try {
- commandLine.value = iframe.contentWindow.location.href;
- } catch (error) {
- console.warn('Cannot access iframe URL due to cross-origin restrictions.');
- }
- });
- </script>
- </body>
- </html>
- `
- };
- module.exports = templateFn;
|