Using WebAssembly in content scripts

175 views
Skip to first unread message

Colorgram

unread,
Aug 26, 2023, 2:57:37 AM8/26/23
to Chromium Extensions
Hey there,

I'm using WebAssembly.instantiateStreaming() in a content script and it's failing on websites with restrictive CSP policies (eg. Github). It gives me the following error:

4585e33dbf98cf20fc625f2a7ccbe60a.png

I've added this section to my MV3 without success:

content_security_policy: {
  extension_pages: "script-src 'self' 'wasm-unsafe-eval'; object-src 'none';",
}


I also tried to use chrome.declarativeNetRequest.updateDynamicRules to relax CSP policies for the main frame, without success either.

I don't know if it's relevant, but I'm injecting the content script using await chrome.scripting.executeScript and I then dynamically import the actual script using a dynamic import (to benefit from ES modules):

// main.ts
;(async () => {
  await import(/* @vite-ignore */ './content')
})()

// content.ts
import init from 'my-wasm-module'

await init()

I'm not exactly sure how to fix this issue. Do you know what I'm missing? Thanks!

wOxxOm

unread,
Aug 26, 2023, 7:32:06 AM8/26/23
to Chromium Extensions, Colorgram
Add an iframe to the web page and point its src to an html page in your extension exposed via web_accessible_resources, then use WASM inside.

Colorgram

unread,
Aug 26, 2023, 11:01:27 AM8/26/23
to Chromium Extensions, wOxxOm, Colorgram
Thanks! It did resolve the initial issue, creating another one.

Because I'm moving code that needs to access the main frame's DOM, I now have a cross origin issue when trying to access window.parent.document.

I could use message passing to the parent script (the one injected in the main frame) but this would introduce a good share of complexity.

Do you know of a way to access the parent frame's document?

Thanks!

wOxxOm

unread,
Aug 26, 2023, 11:10:58 AM8/26/23
to Chromium Extensions, Colorgram, wOxxOm
There's no way because the documents are cross-origin, so you'll need to use messaging.

Colorgram

unread,
Aug 26, 2023, 3:05:37 PM8/26/23
to Chromium Extensions, wOxxOm, Colorgram
Ok I see... Thanks for the help!
Reply all
Reply to author
Forward
0 new messages