Manifest v3 and jsencrypt

134 views
Skip to first unread message

DBA-Dream Believer

unread,
Jan 19, 2022, 6:00:42 PM1/19/22
to Chromium Extensions
I have a manifest v2 chrome extension that uses jsencrypt.js to encrypt the body of an api post. We're migrating to manifest v3 and I can't seem to get jsencrypt to work in the service worker.

I'm using it in the context of an event listener that receives a message from the content script:

importScripts("jsencrypt.min.js");
let RSAEncrypt = new JSEncrypt();

The extension never gets past the first line, throwing the following error:

Error handling response: Error: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'chrome-extension://(ExtensionID)/scripts/jsencrypt.min.js' failed to load.
 
Someone advised that jsencrypt uses 'window' but I'm not sure what they mean.


hrg...@gmail.com

unread,
Jan 19, 2022, 6:21:31 PM1/19/22
to Chromium Extensions, DBA-Dream Believer
It means it uses the window global object: https://developer.mozilla.org/en-US/docs/Web/API/Window

This variable doesn't exist in a service worker, so any code that depends on it will fail.

DBA-Dream Believer

unread,
Jan 24, 2022, 1:55:23 PM1/24/22
to Chromium Extensions, hrg...@gmail.com, DBA-Dream Believer
Any suggestions for a workaround? Here's more context on the block of code I'm using it in:

chrome.storage.sync.get(['AccessKey'],function(result){
            console.log('AccessKey is ',result.AccessKey); //Debug code
            importScripts("/scripts/jsencrypt.min.js");
            let RSAEncrypt = new JSEncrypt();
            RSAEncrypt.setPublicKey(result.AccessKey);
            var encryptedmsg = RSAEncrypt.encrypt(message.content + "##" + daysBack);
            const request = new Request(api_url, {
                method: 'POST',
                headers: {
                'Content-Type': 'application/json'
                },
                body: "\"" + encryptedmsg + "\""
            });
            console.log('This url is ',api_url); //Debug code
            fetch(request)
            .then(response => response.json())
            .then(responseData => {
                console.log('APITotal Person hits: ',responseData); //Debug code
                var person = responseData;
            })
 });    


The encryption process is part of an API request (fetch) that I can't control so I'll need the encryption to be there. 

hrg...@gmail.com

unread,
Jan 24, 2022, 3:13:28 PM1/24/22
to Chromium Extensions, DBA-Dream Believer, hrg...@gmail.com
I took a quick look at the JSEncrypt library, and it seems to be written in the form of a module.
AFAIK, the  importScripts function cannot import modules, it can only import regular javascript files.

In your manifest, you can declare your service worker script as type:"module", this will allow you to import other modules.

Cuyler Stuwe

unread,
Jan 24, 2022, 3:18:01 PM1/24/22
to hrg...@gmail.com, Chromium Extensions, DBA-Dream Believer
My suggestion would just be "use a different library" (or don't use a library at all).

If the issue is just module importing (as HRG alluded to), then this might be something you could resolve w/ a bundler like Webpack/Parcel.

--
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/79a41620-115f-47ea-9d44-f118cb89732en%40chromium.org.

Cuyler Stuwe

unread,
Jan 24, 2022, 3:19:00 PM1/24/22
to hrg...@gmail.com, Chromium Extensions, DBA-Dream Believer
And if the reason you're trying to force a square peg into a round hole is that you're copy/pasting from a StackOverflow answer with no idea of alternative solutions... Stop doing that.

wOxxOm

unread,
Jan 25, 2022, 11:09:38 AM1/25/22
to Chromium Extensions, salem...@gmail.com, Chromium Extensions, DBA-Dream Believer, hrg...@gmail.com
DBA-Dream Believer, try adding `self.window = self;` before importing the library. The library uses a standard module definition check so since you're not using a module system (webpack, AMD, etc.) it tries to set a global on `window`. If this is the only place where `window` is used then you're fine, otherwise find a different library or try polyfilling whatever feature the library wants.

DBA-Dream Believer

unread,
Jan 26, 2022, 12:55:28 PM1/26/22
to Chromium Extensions, wOxxOm, salem...@gmail.com, Chromium Extensions, DBA-Dream Believer, hrg...@gmail.com
Hi wOxxOm,
Thank you for your reply. If I'm understanding what you're advising, would I be updating the listener in background.js, like so:

chrome.storage.sync.get(['AccessKey'],function(result){
            console.log('AccessKey is ',result.AccessKey); //Debug code
            self.window = self; importScripts("/scripts/jsencrypt.min.js");
            let RSAEncrypt = new JSEncrypt();
            RSAEncrypt.setPublicKey(result.AccessKey);
            var encryptedmsg = RSAEncrypt.encrypt(message.content + "##" + daysBack);
            const request = new Request(api_url, {
                method: 'POST',
                headers: {
                'Content-Type': 'application/json'
                },
                body: "\"" + encryptedmsg + "\""
            });
            console.log('This url is ',api_url); //Debug code
            fetch(request)
            .then(response => response.json())
            .then(responseData => {
                console.log('APITotal Person hits: ',responseData); //Debug code
                var person = responseData;
            })
 });    

With this in place I'm getting the following error:
Error handling response: Error: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'chrome-extension://<chrome ID>/scripts/jsencrypt.min.js' failed to load.

wOxxOm

unread,
Jan 26, 2022, 1:00:39 PM1/26/22
to Chromium Extensions, DBA-Dream Believer, wOxxOm, salem...@gmail.com, Chromium Extensions, hrg...@gmail.com
Yes. If you still want to use this library specifically, you'll have to debug it. Copy its code and paste into your script after the self.window line, then you will see a more sensible error message.
Reply all
Reply to author
Forward
0 new messages