1

I am trying to inject a module structured like "injection.js" below into a browser tab with a chrome extension structured like "extension.js". I am using manifest v3.

//injection.js
export function f(){...}
export function g(){...}
export default function main(){
  f()
  g()
}
//extension.js
import main from "./injection.js";

...
chrome.scripting.executeScript({target: {tabId: tabs[0].id}, function:main });//tabs[0].id is defined elsewhere
...

However, I receive an error, because f and g are undefined, since it literally seems to only be injecting main. If I try to inject the entire file injection.js, I receive an error for using the keyword export since I can't specify I am injecting a module.

I am aware that I could wrap all 3 functions in injection.js with a wrapper function to resolve this problem. However, this makes running tests on functions f and g difficult, since I have no way of isolating them for unit tests.

Is there a way to inject modules into tabs on chrome extensions?

(This is a simplified version of my actual code. For example, my actual code has the return values of f and g interacting with each other. But for the purpose of highlighting the problem, I included only necessary info)

Jen H
  • 147
  • 8
  • The injected code runs in a different page in a different OS process so you can't do what you want. – wOxxOm Apr 29 '21 at 05:25
  • What is the best practice then for injecting the functions then, while still keeping them testable? – Jen H Apr 29 '21 at 05:27
  • 1
    I guess you're using webpack or another bundler so you can add an entry that produces a separate file for injection.js which you will specify in `files` parameter instead of `function`. – wOxxOm Apr 29 '21 at 05:37
  • See also https://stackoverflow.com/a/53033388/9080566 – Jan Joneš Jul 04 '22 at 12:25

0 Answers0