capsiums / capsium-webextension

Chrome extension to load Capsium packages
0 stars 1 forks source link

Make the Capsium package webextension work #2

Closed vikasg603 closed 3 months ago

vikasg603 commented 3 months ago

Metanorma PR checklist

This PR introduces the solution that resolves the issue here: https://github.com/metanorma/capsium-webextension/issues/1

Documentation on how this works

Restrictions with Chrome extension

  1. Chrome doesn't allow us to host multiple files as a static server.
  2. We can't change the response body of HTTP requests.
  3. Regarding static files like JS, We definitely can't run JS to make changes in the file.

How this extension solves the problem

  1. Chrome doesn't allow us to modify the response body for any HTTP request, but it does allow us to redirect it to somewhere else, and here is a key, We can also redirect it to the Data URI https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs
  2. When we upload the zip, the popup.js sends this to the background.js, Here previously there was a bug we were trying to send an array buffer, but the problem is, for runtime communication, we can use only JSON, and that's why I converted file into base64.
  3. On background.js, we read the zip using the same jszip library and then store the contents in Chrome storage.
  4. The point to consider here is, that we are also creating a unique ID for the cap file, called as caped
  5. We are storing every content in the zip with the id "capId-FilePath"
  6. While storing the HTML, we are making sure to replace each relative path with absolute path with our custom domain
  7. We are also making sure to clear the storage after every 30 minutes.
  8. But we have done with the storage, now what next?
  9. For every capId, we are creating a custom domain URL like {capId}.cap
  10. We are handling this {capId}.cap domain using declarativeNetRequest and then redirecting the it to the content data uri.
ronaldtse commented 3 months ago

@vikasg603 I am able to open the bare_package-0.1.0.cap file, but the mn-samples-iso-0.1.0.cap file failed to open with this message:

Failed to execute 'btoa' on 'WorkerGlobalScope': The string to be encoded contains characters outside of the Latin1 range.
Screenshot 2024-06-07 at 11 21 34 AM