How to import Jquery in Background service worker - Manifest V3 - Chrome Extension

Viewed 6237
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  }

I need to use Jquery in background.js. I cannot add jquery.min.js in the manifest because with the new upgrade v3 I cannot declare more than one JS in service_worker. How can I solve?

background.js

$.ajax({
   type: "POST",
   url: "https://wwww",
   data: {text: "xxx"}
});
2 Answers

jQuery is DOM-based so its $.ajax or $.put or $.get won't run in the service worker as there's no DOM/document/window, consequently no XMLHttpRequest, which is used by these methods. The other DOM-based features of jQuery are also unusable in a service worker.

The solution is to use fetch().
There are also fetch-based libraries if you need more bells and whistles like onprogress.

You can still use a JQuery-noDOM to use ajax etc without retyping your code using fetch.

Related