-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sample: Using WASM in Manifest V3 #775
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
I think the I think it's entirely appropriate to commit the compiled Wasm; IMO the main intent of the sample isn't to show how to create Wasm, but rather to show how one can use Wasm they already have in an extension. Guidance on authoring Wasm modules should probably be limited to some notes and links in a |
I tried to write a simple example (#841) of using wasm in manifest v3, but instead of using |
I am currently attempting to create the extension, but I am encountering some issues. whenever I'm trying to load the extension I'm getting this error " File Error:Invalid value for 'content_security_policy'.` Here is my manifest file {
"manifest_version": 3,
"name": "Hello World Extension",
"version": "1.0.0",
"background": {
"service_worker": "sw.js"
},
"content_security_policy": "worker-src 'self'; script-src 'self' 'unsafe-eval'",
"permissions": [],
"host_permissions": [],
"action": {}
} |
@octopols in manifest v3, |
Here's instructions: https://developer.chrome.com/docs/extensions/migrating/improve-security/#update-csp |
@patrickkettner @jpmedley thanks a lot I was able to fi the security thing but I still get many errors, I'm still getting the hang of Rust and I think there's something seriously wrong with my extension. I'll check out some documentation to see if I can figure out how to fix it and give it another shot. |
@octopols feel free to post in progress stuff! |
I'll make a pull request or a separate repository for my progress. Based on my understanding of the problem statement, I need to create an extension that uses manifest version 3 and prints a message in the console. My objective is to make the extension print a message in the console when clicked. To achieve this, I began by creating a base Rust project. Then, I used the Rust repository, wasm-bindgen, which has an example of printing in the console using Rust and WebAssembly (wasm). However, I am currently struggling to link the files generated by running |
here is my current progress https://github.com/octopols/rust_wasl_extension |
great job so far! what is the issue you are hitting? I see a handful (forgive me if anything is obivous)
e.g. "background": {
"service_worker": "background.js",
"type": "module"
},
import wasm_bindgen from './pkg/index_bg.js'; note that it also should be a relative import as I changed above
import * as wasm from "./pkg/index_bg.js";
pub fn bare_bones() {
log("Hello from Rust!");
log_u32(42);
log_many("Logging", "many values!");
} From there, running the |
Thanks a lot for testing and suggesting fixes, I'll try to implement them after college today. I have one question though, what's the need/use to run |
great question!
in turn,
|
I have a |
rather than use wasm-pack directly, use the tooling already in the project
- `npm run build`.
…On Wed, Mar 29, 2023 at 4:14 PM hirnaymay ***@***.***> wrote:
great job so far!
what is the issue you are hitting? I see a handful (forgive me if anything
is obivous)
1. running npm run build && npm run serve produces a working wasm page
for me, so the build itself is good.
2. The background script in your manifets has a dynamic import, which
isn't allowed without also setting "type": "module" in your manifest
e.g.
"background": {
"service_worker": "background.js",
"type": "module"
},
1. the reference file (i.e. console_log_bg.wasm.js) doesn't exist.
changing that to something like
import wasm_bindgen from './pkg/index_bg.js';
note that it also should be a relative import as I changed above
1. your rust output does not export a default. You would either need
to declare one or just import everything
import * as wasm from "./pkg/index_bg.js";
1. I *believe* (I know very little about wasm rust) you need to
declare some of your functions as public within your lib.rs
pub fn bare_bones() {
log("Hello from Rust!");
log_u32(42);
log_many("Logging", "many values!");}
From there, running the npm run build, I am able to load the unpacked
extension. Hope that helps a bit!
I have a console_log_bg.wasm file generated when I run wasm-pack build
--target web there is no index_bg.js in the pkg folder? I don't know if
I'm doing something really dumb here
—
Reply to this email directly, view it on GitHub
<#775 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADRUBXIAVZWNFCUWOBVW2TW6SJYVANCNFSM6AAAAAAR6LGL2M>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
patrick
|
I'll give it try, thanks for the reply |
THIS WORKS! but I'm just loading the webpage rn, how do I make it work as an extension. I'm thinking of making the extension clickable, so whenever I click on the extension it logs "hello world!" to the console. |
congratulations!!! You should be able to load the unpacked version of the extension in chrome by going to chrome://extensions > Load unpacked (if you don't see Load unpacked, make sure the Developer mode" toggle is enabled in the chrome://extension page). You would want to load the If you want to be able to have a clickable action, I would suggest checking out browserAction. |
I've started working on the extension part (updated my repo). I looked around various online videos talking about wasm and realised that the wasm files only run on a live server. Extensions are locally loaded (as far as I know) so getting the wasm file to console log while being used in an extension is not working for me. :/ |
The code you already had running is a wasm file, running in the browser. You are already pretty much there. Did you load the unpacked extension? |
I loaded the dist folder, created a manifest file. In the popup I added a button that loads the |
no worries, lets step back and reevaluate. We know WASM runs in the browser. It has been the case for a very long time. I am not entirely sure what you mean by "a live server", but in essence WebAssembly is a compilation target for other programing languages. That means we write the code in something other than javascript - in this case, rust, which is not able to run directly in the browser. We then use tooling to convert that code into something that can run directly in the browser - in this case, WASM code. If that is all true, we want to write code in rust that is compiled into something that logs a string in our console, in an extension. You have already create something that is written in rust, and that logs code to the console. We just need to get that into an extension. Lets go over the architecture in your repo to see how to make that happen. You have your rust code, that should be the core functionality of your extension. It needs to be compiled into wasm code. The compilation is done via wasm-pack, which your repo has implemented using webpack. Webpack knows what to compile based on the entry point. Your entry point is pointing to index.js. That file is just importing the Putting that all aside, lets look at your extension. The entry of the extension is the manifest file. I think part of the problem you are having is that you have multiple manifest files. Your original one, and the one you just added. I believe this happened because you manually added a file to the dist folder - which is a mistake. The dist folder should just be the output of your build process. You can enforce this by setting the Your original manifest just loads a single javascript file - import * as wasm from "./pkg/index_bg.js";
chrome.browserAction.onClicked.addListener(async (tab) => {
await wasm_bindgen.default();
}); It was importing the wasm code being generated, and then calling it on You said you wanted to have an extension that worked based on the extension's popup. Looking at your manifest you have a If you check out your repo, the <!DOCTYPE html>
<html>
<head>
<title>My Chrome Extension</title>
<script src="popup.js"></script>
<meta charset='UTF-8' />
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Using WebAssembly with 🦀Rust in chrome extension using Manifest V3.</h1>
<button id="myButton">Click me to log to console!</button>
</div>
</body>
</html> but if you check the same file after running the build step (i.e. <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Webpack App</title>
<meta name="viewport" content="width=device-width, initial-scale=1"><script defer src="index.js"></script></head>
<body>
</body>
</html> It is way different, because you are using HtmlWebpackPlugin, which is overwriting your html code with the html webpack is generating to load your compiled code. This is a reason why setting the That being said, since you made an html page already, you don't need Looking at your index.html page, you are loading in popup.js. Looking at that file (which also should not be in Let me know if anything is unclear! |
Omg thank you so much!!! I will go through this and try to make this work. I really appreciate you for giving your time 💖. |
awesome! please feel free to reach out if you hit any problems :D |
If you check out your repo, the [index.html]
but if you check the same file after running the build step (i.e. npm run build), it will look like this
|
If you check out your repo, the [index.html]
but if you check the same file after running the build step (i.e. npm run build), it will look like this
|
I'd like to try working on a PR to demonstrate using WASM in MV3. Before doing so, I wanted to propose it here. I've tried my best to follow the format which @dotproto used in other issues (e.g #766).
Overview
A sample showing how to load WASM in Manifest V3. This requires the new
wasm-unsafe-eval
CSP directive (see https://developer.chrome.com/docs/extensions/mv3/mv3-migration/#content-security-policy).Proposed Implementation
I think a nice demo would be a simple extension which prints "Hello World!" from Rust.
wasm-unsafe-eval
importScripts
to include the generated JSAdditional Thoughts
Deciding where to put this is a little hard. Looking at existing discussions (here), I suspect
cookbook
may be the best place as this feels beyond an API demo but is not a fully functioning extension. We may want to commit the compiled WASM to avoid the need for a build step, or alternatively that could be documented in the README.I also considered demonstrating how to access something like
self.clients
since this is not covered well in general WASM tutorials, which seem to focus heavily on webpages. However, in the interests of keeping this demo simple I suspect that may be something to revisit in the future.The text was updated successfully, but these errors were encountered: