|
| 1 | +/* eslint-disable unused-imports/no-unused-vars */ |
| 2 | +import * as vscode from 'vscode' |
| 3 | + |
| 4 | +export function activateFunnyBrowser(context: vscode.ExtensionContext) { |
| 5 | + const provider = new FunnyBrowserProvider(context) |
| 6 | + |
| 7 | + context.subscriptions.push( |
| 8 | + vscode.window.registerWebviewViewProvider('funnyBrowserWebview', provider) |
| 9 | + ) |
| 10 | +} |
| 11 | + |
| 12 | +class FunnyBrowserProvider implements vscode.WebviewViewProvider { |
| 13 | + constructor(private readonly context: vscode.ExtensionContext) {} |
| 14 | + |
| 15 | + resolveWebviewView( |
| 16 | + webviewView: vscode.WebviewView, |
| 17 | + context: vscode.WebviewViewResolveContext, |
| 18 | + token: vscode.CancellationToken |
| 19 | + ): void | Thenable<void> { |
| 20 | + webviewView.webview.options = { |
| 21 | + enableScripts: true, |
| 22 | + localResourceRoots: [this.context.extensionUri] |
| 23 | + } |
| 24 | + |
| 25 | + webviewView.webview.html = this.getWebviewContent(webviewView.webview) |
| 26 | + |
| 27 | + webviewView.webview.onDidReceiveMessage( |
| 28 | + message => { |
| 29 | + switch (message.command) { |
| 30 | + case 'log': |
| 31 | + console.log(message.text) |
| 32 | + break |
| 33 | + default: |
| 34 | + break |
| 35 | + } |
| 36 | + }, |
| 37 | + undefined, |
| 38 | + this.context.subscriptions |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + private getWebviewContent(webview: vscode.Webview): string { |
| 43 | + return ` |
| 44 | + <!DOCTYPE html> |
| 45 | + <html lang="en"> |
| 46 | + <head> |
| 47 | + <meta charset="UTF-8"> |
| 48 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 49 | + <title>Browser</title> |
| 50 | + <style> |
| 51 | + body, html { |
| 52 | + margin: 0; |
| 53 | + padding: 0; |
| 54 | + height: 100vh; |
| 55 | + display: flex; |
| 56 | + flex-direction: column; |
| 57 | + background-color: var(--vscode-editor-background); |
| 58 | + color: var(--vscode-editor-foreground); |
| 59 | + font-size: 12px; |
| 60 | + } |
| 61 | + #controls { |
| 62 | + display: flex; |
| 63 | + padding: 5px; |
| 64 | + background-color: var(--vscode-editor-background); |
| 65 | + } |
| 66 | + #urlBar { |
| 67 | + flex-grow: 1; |
| 68 | + margin: 0 5px; |
| 69 | + padding: 2px; |
| 70 | + background-color: var(--vscode-input-background); |
| 71 | + color: var(--vscode-input-foreground); |
| 72 | + border: 1px solid var(--vscode-input-border); |
| 73 | + } |
| 74 | + button { |
| 75 | + padding: 2px 5px; |
| 76 | + background-color: var(--vscode-button-background); |
| 77 | + color: var(--vscode-button-foreground); |
| 78 | + border: none; |
| 79 | + cursor: pointer; |
| 80 | + } |
| 81 | + button:hover { |
| 82 | + background-color: var(--vscode-button-hoverBackground); |
| 83 | + } |
| 84 | + iframe { |
| 85 | + flex-grow: 1; |
| 86 | + border: none; |
| 87 | + } |
| 88 | + </style> |
| 89 | + </head> |
| 90 | + <body> |
| 91 | + <div id="controls"> |
| 92 | + <button id="backBtn">←</button> |
| 93 | + <button id="forwardBtn">→</button> |
| 94 | + <button id="refreshBtn">↻</button> |
| 95 | + <input type="text" id="urlBar" value="https://www.bilibili.com"> |
| 96 | + <button id="goBtn">Go</button> |
| 97 | + </div> |
| 98 | + <iframe |
| 99 | + id="content" |
| 100 | + src="https://www.bilibili.com" |
| 101 | + frameborder="0" |
| 102 | + allowfullscreen |
| 103 | + allow="accelerometer; autoplay; camera; clipboard-write; encrypted-media; fullscreen; geolocation; gyroscope; microphone; midi; payment; picture-in-picture; usb; vr; xr-spatial-tracking" |
| 104 | + sandbox="allow-downloads allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-presentation allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation allow-top-navigation-by-user-activation" |
| 105 | + ></iframe> |
| 106 | + <script> |
| 107 | + const vscode = acquireVsCodeApi(); |
| 108 | + const urlBar = document.getElementById('urlBar'); |
| 109 | + const content = document.getElementById('content'); |
| 110 | + const backBtn = document.getElementById('backBtn'); |
| 111 | + const forwardBtn = document.getElementById('forwardBtn'); |
| 112 | + const refreshBtn = document.getElementById('refreshBtn'); |
| 113 | + const goBtn = document.getElementById('goBtn'); |
| 114 | +
|
| 115 | + function navigate() { |
| 116 | + let url = urlBar.value; |
| 117 | + if (!url.startsWith('http://') && !url.startsWith('https://')) { |
| 118 | + url = 'https://' + url; |
| 119 | + } |
| 120 | + content.src = url; |
| 121 | + vscode.postMessage({ command: 'log', text: 'Navigating to: ' + url }); |
| 122 | + } |
| 123 | +
|
| 124 | + goBtn.addEventListener('click', navigate); |
| 125 | + urlBar.addEventListener('keypress', (e) => { |
| 126 | + if (e.key === 'Enter') { |
| 127 | + navigate(); |
| 128 | + } |
| 129 | + }); |
| 130 | +
|
| 131 | + backBtn.addEventListener('click', () => { |
| 132 | + content.contentWindow.history.back(); |
| 133 | + vscode.postMessage({ command: 'log', text: 'Going back' }); |
| 134 | + }); |
| 135 | +
|
| 136 | + forwardBtn.addEventListener('click', () => { |
| 137 | + content.contentWindow.history.forward(); |
| 138 | + vscode.postMessage({ command: 'log', text: 'Going forward' }); |
| 139 | + }); |
| 140 | +
|
| 141 | + refreshBtn.addEventListener('click', () => { |
| 142 | + content.contentWindow.location.reload(); |
| 143 | + vscode.postMessage({ command: 'log', text: 'Refreshing page' }); |
| 144 | + }); |
| 145 | +
|
| 146 | + content.addEventListener('load', () => { |
| 147 | + try { |
| 148 | + urlBar.value = content.contentWindow.location.href; |
| 149 | + vscode.postMessage({ command: 'log', text: 'Loaded: ' + content.contentWindow.location.href }); |
| 150 | + } catch (e) { |
| 151 | + vscode.postMessage({ |
| 152 | + command: 'log', |
| 153 | + text: 'Unable to access page URL due to security restrictions.' |
| 154 | + }); |
| 155 | + } |
| 156 | + }); |
| 157 | + </script> |
| 158 | + </body> |
| 159 | + </html> |
| 160 | + ` |
| 161 | + } |
| 162 | +} |
0 commit comments