Open drriguz opened 10 months ago
I think I figured out a workaround:
first, just use the image like any other normal react project, put the image in webview-ui/src/user.png
:
App.css
.user-avatar {
background-image: url(./user.png);
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
After that, the image should be able to display when run npm run start
directly inside the webview-ui project. However, it won't work in extension.
Then, override the css in extension html,
const userImage = getUri(webview, this.context.extensionUri, [
"webview-ui",
"build",
"assets",
"user.png",
]);
<style nonce="${nonce}">
.user-avatar {
background-image: url("${userImage}");
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
</style>
This would make sure that the image could be correctly displayed in extension.
Accroding to GPT, another way it to get the image uri via message. However, this is unnecessary complex, and need to maintain a state to store the uri.
Hi, I'm using toolkit with react+vite. I want to show an image in the webview, like this:
However, this does not work, and shows error message:
There's also a similar issue in stackoverflow.
I've tried to add csp rules, and also not work:
I find a blog says that it's needed to serve the image by a virtual server, and it seems that the css is actually loaded by a virtual https url:
The uri could be build in extension using
getUri
:However, this is not possible in webview-ui.
Can you suggest us how to use custom images (especially in react framework)?