How to make work RequireJS in manifest v3?

232 views
Skip to first unread message

Mikhail Glukhov

unread,
Nov 29, 2022, 6:46:55 AM11/29/22
to Chromium Extensions

We are currently trying to update our manifest v2 Chrome Extension to manifest v3.

The extension is using RequireJS in content (!) scripts, and we are currently overloading RequireJS load function to load scripts using eval, as shown here:

require.load = function (context, moduleName, url) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.onreadystatechange = function (e) {
    if (xhr.readyState === 4 && xhr.status === 200) {
      eval(xhr.responseText);
      context.completeLoad(moduleName)
    }
  };
  xhr.send(null);
};

This approach has been demonstrated in several sources, for instance:

https://coderwall.com/p/j9bvug/chrome-extension-requirejs-content-scripts

This approach has worked fine in manifest v2 for years in our extension.

However, in manifest v3 using of eval is forbidden.

We tried different techniques, but did not manage to find an elegant solution to this problem. 

Basically the question is: how one can use RequireJS in content (!) scripts in Chrome Extension with manifest v3.

What is important that we need a way to load not external code, but scripts that are part of the extension, and we need to load them as content (not background) scripts, and do it dynamically using RequiesJS.

I think that supporting RequireJS in manifest v3 Chrome Extensions is a must, since this library is so widely used.

Any ideas would be very much appreciated.

Regards,
Mikhail

wOxxOm

unread,
Nov 29, 2022, 9:12:56 AM11/29/22
to Chromium Extensions, mikhail...@gmail.com
Use dynamic import() instead of xhr+eval, here's a generic example which you can incorporate into your override:

Mikhail Glukhov

unread,
Nov 29, 2022, 12:27:23 PM11/29/22
to Chromium Extensions, wOxxOm, Mikhail Glukhov
Thank you very much, this approach seems to be working fine!
Reply all
Reply to author
Forward
0 new messages