using WebAssembly in chrome extension

I have a chrome extension that includes a complicated function comp_func(data) which takes a lot of CPU by performing many bitwise operations. Because of that, I’m trying to use WebAssembly.

I’ve tried to follow several tutorials, for example this one and this one.

The first link says:

fetch('simple.wasm').then(response =>
  response.arrayBuffer()
).then(bytes =>
  WebAssembly.instantiate(bytes, importObject)
).then(results => {
  results.instance.exports.exported_func();
});

but I get an error:

Uncaught (in promise) TypeError: WebAssembly Instantiation: Import #0 module=”env” error: module is not an object or function

I’ve tried a lot to use this approach, but it didn’t work. I can’t understand how to use WebAssembly that is loaded from the .wasm file.

So I’ve tried an easier approach:
The second link says to put that line in the html file:


and then just use the function exported:

var result = _roll_dice();

BUT, I’m in extension so I only have a background.html file.
So I’m looking for a way to access the Module which was loaded in the background file.
And things get complicated, because the function comp_func(data) is called from a Worker.

This is what I’ve tried so far:

If I call chrome.extension.getBackgroundPage() I can access the Module
but I can’t send it to the Worker:

Failed to execute ‘postMessage’ on ‘Worker’: # could not be cloned.

And if I try to stringify it first:

Uncaught TypeError: Converting circular structure to JSON

(I tried to un-circular it, didn’t work…)

And I can’t call chrome.extension.getBackgroundPage() from the Worker because I can’t access chrome API from there.

So my questions are:

  1. Did someone tired to load .wasm file in chrome extension and it worked?
    The second approach (loading the js file) is sound simpler but if you have a working example for this approach it will be great.

or 2. how to access the Module that has been loaded in background.html (from the second example)?

or 3. how to pass the functions that I needed from the js file to the Worker (via postMessage)?

To summarize, did someone tried to use WebAssembly in chrome extension and survived to tell?

Using WebAssembly inside chrome sandbox

I have a chrome extension that includes a complicated function comp_func(data) which takes a lot of CPU by performing many bitwise operations. Because of that, I’m trying to use WebAssembly.
After many tries I came to a conclusion that due to permissions issues I need to use this function inside a sandbox.

Right now I’m trying to follow this WebAssembly tutorial and this Sandbox example as an “hello world” example for my problem.

The WebAssembly files includes: index.js and index.wasm,
The sandbox files is: sandbox.html,
page.js is the code of the extension that need to perform comp_func(data) and is using postMessage as a way to communicate to sandbox.html.
background.html is the background file of the extension.

This is what I have so far:

manifest.json

   "sandbox": {
     "pages": ["sandbox.html"]
   },

background.js



sandbox.html


      
        
        
      
    

So, right now the extension is calling sandbox.html who in turn loading index.js, but then I get the error:

Failed to load
chrome-extension://index.wasm: No
‘Access-Control-Allow-Origin’ header is present on the requested
resource. Origin ‘null’ is therefore not allowed access. If an opaque
response serves your needs, set the request’s mode to ‘no-cors’ to
fetch the resource with CORS disabled.

From what I understand, index.js is calling index.wasm and because I’m inside a sandbox there is a problem with CORS permissions.
Is someone know what is the solution in this case?

How to get content of javascript files as variable

I have to develop a chrome extension for my thesis which does following analysis:

This extension should analyse the javascript files (.js) and webssembly files (.wasm) of visited webpages. For example i have to check if a javascript file contains a special string X. In somepages included files are on visited domain in somepages on another domain. Via security reasons maybe it would be diffucult to get content of these files. Is it possible to get content of these files as a variable?

Wasm module compile error in chrome extension

in my extension I want to use my own webassembly module.

After loading my module (to background.html, or popup.html), I catch the compile error:

CompileError: WebAssembly.compile(): Wasm code generation disallowed by embedder.

Are wasm modules not supports in chrome-extensions?