__browserTemplate.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. function getWebviewContent(url) {
  2. return `<!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Web Preview</title>
  8. <style>
  9. body { margin: 0; padding: 0; font-family: Arial, sans-serif; }
  10. .browser-bar {
  11. display: flex;
  12. align-items: center;
  13. background: #f1f1f1;
  14. padding: 8px;
  15. border-bottom: 1px solid #ccc;
  16. }
  17. .btn {
  18. background: #ddd;
  19. border: none;
  20. padding: 6px 12px;
  21. margin-right: 6px;
  22. cursor: pointer;
  23. font-size: 14px;
  24. border-radius: 4px;
  25. }
  26. .btn:hover {
  27. background: #bbb;
  28. }
  29. .url-input {
  30. flex-grow: 1;
  31. padding: 6px;
  32. font-size: 14px;
  33. border: 1px solid #ccc;
  34. border-radius: 4px;
  35. outline: none;
  36. }
  37. iframe {
  38. width: 100vw;
  39. height: calc(100vh - 40px);
  40. border: none;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div class="browser-bar">
  46. <button class="btn" id="backBtn">←</button>
  47. <button class="btn" id="forwardBtn">→</button>
  48. <input type="text" class="url-input" id="urlInput" value="${url}">
  49. <button class="btn" id="refreshBtn">⟳</button>
  50. <button class="btn" id="inspectBtn">🔍 Inspect</button>
  51. </div>
  52. <iframe id="webFrame" src="${url}" sandbox="allow-scripts allow-same-origin"></iframe>
  53. <script>
  54. const vscode = acquireVsCodeApi();
  55. const backBtn = document.getElementById('backBtn');
  56. const forwardBtn = document.getElementById('forwardBtn');
  57. const refreshBtn = document.getElementById('refreshBtn');
  58. const inspectBtn = document.getElementById('inspectBtn');
  59. const urlInput = document.getElementById('urlInput');
  60. const iframe = document.getElementById('webFrame');
  61. let historyStack = [];
  62. let currentIndex = -1;
  63. function sendUrlChangeEvent(url) {
  64. vscode.postMessage({ command: 'urlChanged', url });
  65. }
  66. function navigateTo(url, addToHistory = true) {
  67. iframe.src = url;
  68. urlInput.value = url;
  69. sendUrlChangeEvent(url);
  70. if (addToHistory) {
  71. historyStack = historyStack.slice(0, currentIndex + 1);
  72. historyStack.push(url);
  73. currentIndex++;
  74. }
  75. updateNavButtons();
  76. }
  77. function updateNavButtons() {
  78. backBtn.disabled = currentIndex <= 0;
  79. forwardBtn.disabled = currentIndex >= historyStack.length - 1;
  80. }
  81. backBtn.addEventListener('click', () => {
  82. if (currentIndex > 0) {
  83. currentIndex--;
  84. navigateTo(historyStack[currentIndex], false);
  85. }
  86. });
  87. forwardBtn.addEventListener('click', () => {
  88. if (currentIndex < historyStack.length - 1) {
  89. currentIndex++;
  90. navigateTo(historyStack[currentIndex], false);
  91. }
  92. });
  93. refreshBtn.addEventListener('click', () => {
  94. navigateTo(urlInput.value);
  95. });
  96. urlInput.addEventListener('keypress', (event) => {
  97. if (event.key === 'Enter') {
  98. navigateTo(urlInput.value);
  99. }
  100. });
  101. // Open the iframe in a new window for inspection
  102. inspectBtn.addEventListener('click', () => {
  103. vscode.postMessage({ command: 'inspectUrl', url: iframe.src });
  104. });
  105. // Initialize history with the first URL
  106. navigateTo("${url}");
  107. </script>
  108. </body>
  109. </html>`;
  110. }
  111. module.exports = getWebviewContent;