Skip to content

Commit

Permalink
updates from feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkettner committed Nov 6, 2024
1 parent 33fb83a commit d7f8fe8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions functional-samples/libraries-xhr-in-sw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This sample demonstrates how to use code that relies on XHR within a extension's

The default background environment for extensions is the service worker. As a result, it only has direct access to [Fetch](https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch). If you want to use a library that is built with XHR, this will not work by default. However, you can usually monkeypatch the expected behavior by polyfilling XHR. This sample shows an example of how you can use build tools to automatically inject a polyfill for XHR that covers most common XHR use cases, allowing for seamless integration into your extension.

In this sample, we are using a "library" that exports a function called [`fetchTitle`](./src/lib/fetchTitle.js). For the fiction of this sample, this is a dependency we _must_ use, but we are unable to change ourselves. Unfortunately, it uses XHR. In order to make this work, we [import](./src/index.js#L1) a [shim](./src/lib/xhr-shim.js), and then [set the global `XMLHttpRequest` to it](./src/index.js#L4).

This is all packaged by a build system, in this case [Rollup](https://rollupjs.org/).

## Running this extension

1. Clone this repository
Expand Down
2 changes: 1 addition & 1 deletion functional-samples/libraries-xhr-in-sw/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"background": {
"service_worker": "dist/background.js"
},
"permissions": ["activeTab", "scripting", "sidePanel"],
"permissions": ["activeTab", "sidePanel"],
"host_permissions": ["<all_urls>"],
"side_panel": {
"default_path": "sidepanel/index.html"
Expand Down

0 comments on commit d7f8fe8

Please sign in to comment.