0

I had multiple background scripts in chrome extension manifest version 2. Now I need to migrate to manifest version 3 and therefore need to make necessary changes.

According to documentation, I have kept only one file in service_worker as shown below:

"background": {
    "service_worker": "background.js"
},

and in the background.js I wrote the following javascript statement:

try {
importScripts('js/google-analytics.js', 'js/google-analytics-bundle.js', 'js/eventSender.js', 'js/api-gsc.js', 'js/api-sm.js', 'js/lib/jquery.js');
} catch (e) {
console.error(e);
}

The code in google-analytics.js has the following code:

window.onload = startApp;

Also, inside background.js I am using localStorage.

When I try to load the extension, it gives me the following errors:

background.js:16 ReferenceError: window is not defined
at google-analytics.js:23
at background.js:14

Error in event handler: ReferenceError: localStorage is not defined
at chrome-extension://<ExtensionId>/background.js:99:28

TypeError: Cannot read property 'createElement' of undefined
at ja (jquery.js:2)
at jquery.js:2
at jquery.js:2
at jquery.js:2
at jquery.js:2
at background.js:14

Error handling response: ReferenceError: XMLHttpRequest is not defined
at getUserStatus (chrome-extension://<extensionId>/js/api-gsc.js:9:19)
at chrome-extension://<ExtensionId>/background.js:112:17

and many other errors also.

It seems like it is not able to import the javaScript files properly.

It might be alot to ask in a single question. But, can someone please help me where I am putting wrong code and if someone please help me with the solution.

Thanks in advance.

4

1 回答 1

0

尝试使用fetch()替换XMLHttpRequest

我认为您可以在此页面中找到答案Cross-origin XMLHttpRequest

于 2021-12-31T08:02:48.720 回答