Skip to content

Commit 3528077

Browse files
committed
feat: add webview
1 parent ab48b74 commit 3528077

File tree

6 files changed

+185
-8
lines changed

6 files changed

+185
-8
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
.idea
44
*.log
55
*.tgz
6-
*.vsix
76
coverage
87
dist
98
lib-cov

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"request": "launch",
1212
"runtimeExecutable": "${execPath}",
1313
"args": [
14-
"--extensionDevelopmentPath=${workspaceFolder}"
15-
// "--disable-extensions"
14+
"--extensionDevelopmentPath=${workspaceFolder}",
15+
"--disable-extensions"
1616
],
1717
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
1818
"preLaunchTask": "npm: watch",

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@
3333
"onStartupFinished"
3434
],
3535
"contributes": {
36+
"viewsContainers": {
37+
"panel": [
38+
{
39+
"id": "funnyBrowser",
40+
"title": "Funny Browser",
41+
"icon": "res/icon.png"
42+
}
43+
]
44+
},
45+
"views": {
46+
"funnyBrowser": [
47+
{
48+
"type": "webview",
49+
"id": "funnyBrowserWebview",
50+
"name": "Funny Browser"
51+
}
52+
]
53+
},
3654
"configuration": {
3755
"title": "%config.title%",
3856
"properties": {

src/explorerWebview.ts

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
}

src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode'
22

3-
import { activateFightAnimation } from './fightAnimation'
3+
import { activateFunnyBrowser } from './explorerWebview'
44
import { initializeLocalization } from './i18n'
55

66
export const activate = async (context: vscode.ExtensionContext) => {
@@ -9,10 +9,8 @@ export const activate = async (context: vscode.ExtensionContext) => {
99

1010
await initializeLocalization()
1111

12-
// Add this line to activate the fight animation feature
13-
activateFightAnimation(context)
14-
15-
// await renderWebview(context)
12+
// Activate the Funny Browser WebView
13+
activateFunnyBrowser(context)
1614
} catch (err) {
1715
console.warn('Failed to activate extension', err)
1816
}

vscode-funny.vsix

6.78 MB
Binary file not shown.

0 commit comments

Comments
 (0)