DEV Community

Discussion on: August 13th, 2020: What did you learn this week?

Collapse
 
nickytonline profile image
Nick Taylor • Edited

Thinking emojis floating around

Interesting. I haven't done any WASM aside from a simple Rust tutorial and haven't touched Blazor. Maybe it's a limitation of WASM that it's not supposed to run in an extension. Having said that I found this post from 2 years ago on SO with an answer that explains how to make it work. I do not know if this is still the case and also if what they do is considered a good practice.

I've been fiddling with WebAssembly recently, and found a way to make it work. Here are the script files:

main.js

chrome.browserAction.onClicked.addListener(function(tab) {
 chrome.tabs.executeScript(null, {file: "content_script.js"});
});

content_script.js

  var importObject = { imports: { imported_func: arg => console.log(arg) } };
  url = 'data:application/wasm;base64,' + "AGFzbQEAAAABCAJgAX8AYAAAAhkBB2ltcG9ydHMNaW1wb3J0ZWRfZnVuYwAAAwIBAQcRAQ1leHBvcnRlZF9mdW5jAAEKCAEGAEEqEAAL";
  WebAssembly.instantiateStreaming(fetch(url), importObject)
  .then(obj => obj.instance.exports.exported_func());

The…