Arbitrary code execution in background script

Hello.

I’d like to execute code from content scripts in background script context. Is it ok to use something like https://www.npmjs.com/package/comlink for this or it is not allowed?

Thanks.

Have you tried it yet?
The available API in the content scripts is quite limited, usually nothing fancy works, and this looks quite fancy :slight_smile:.

Also, it’s forbidden to execute remote code - that is executing a code that’s not part of the addon:

Practically, regarding content scripts, if you want to execute some job in the background script, you use something like this:

const dataToSend = {foo: 'bar'};
const result = await browser.runtime.sendMessage({type: 'content_script_job', data: dataToSend});
console.log('background computed result:', result);

So sending a data to the background script, which then processes them and sends result - by returning a Promise in the https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage handler.

Thanks for your reply!

So, as long as all code is included within extension, it is allowed?

Regarding your example, I already have it done like this, but I thought it would be more convenient for development to do it with something like the library mentioned.