Eval in background script/ service worker

527 views
Skip to first unread message

Dhruv TechApps

unread,
Mar 30, 2023, 5:39:41 AM3/30/23
to Chromium Extensions
Hi ,

Is it possible to run eval function either through offscreen / sandbox in MV3 version from background script/ service worker.

I successfully implemented offscreen but unable to run EVAL code in it.

manifest.json 
https://github.com/Dhruv-Techapps/acf-extension/blob/sandbox/src/manifest.json#L46-L48

offscreen
https://github.com/Dhruv-Techapps/acf-extension/blob/sandbox/src/background/off_screen.js

communicating with background and eval
https://github.com/Dhruv-Techapps/acf-extension/blob/sandbox/src/off_screen/off_screen.js

Please do share any example also with which we can run eval function within service worker in MV3

#MV3, #serviceworker #offscreen #sandbox #eval

wOxxOm

unread,
Mar 30, 2023, 6:23:06 AM3/30/23
to Chromium Extensions, Dhruv TechApps
I don't have an example, so here's the gist of it:
1. create an iframe inside the offscreen document that points to sandbox.html file in your extension
2. add this file to sandbox in manifest.json, https://developer.chrome.com/docs/extensions/mv3/manifest/sandbox/
3. use eval inside the iframe's scripts
4. communicate via parent.postMessage inside the iframe and frameElem.contentWindow.postMessage inside the offscreen document

Jackie Han

unread,
Mar 30, 2023, 9:35:34 AM3/30/23
to wOxxOm, Chromium Extensions, Dhruv TechApps
Communication flow: Service Worker ⬌ Offscreen Page ⬌ Sandbox Iframe in Offscreen Page
It's really cumbersome, but that's the only way to do it for now.

In my test, you can use a sandbox page to create an offscreen page directly:
    Service Worker ⤄ Sandboxed Offscreen Page
But there is no way to communicate with the sandbox page.


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/8be0a05d-4cb2-45ca-b31d-47bd137fc31cn%40chromium.org.

Nathan Mo

unread,
Feb 28, 2025, 5:38:12 AM2/28/25
to Chromium Extensions, Jackie Han, Chromium Extensions, Dhruv TechApps, wOxxOm
Do we have any progress on this?

I believe this is a very legit use case, to be able to use wasm-eval in background script, since the changes been made to csp i have not found a way to achieve this.


Here are some proposal:
1.  chrome.offscreen.createDocument('some_sandbox_html') => Promise<MessageChannel|ContentWindowThatICanPostMsg>,  the current implementation is Promise<void> which is not very useful.
2. chrome.sandbox.run(xxx) => one-time result
3. chrome.sandbox.start(xxx) => long-running sandbox with a channel to communicate

Oliver Dunk

unread,
Feb 28, 2025, 5:50:06 AM2/28/25
to Nathan Mo, Chromium Extensions, Jackie Han, Dhruv TechApps, wOxxOm
Hi Nathan,

You should actually be able to use WASM in a background service worker directly.


Let me know how you get on!
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


Reply all
Reply to author
Forward
0 new messages