0

I develop a Chrome extension where I capture some data from the user journey on a website. The data is cached in a local variable of the content script added to the webpage. The background script (service worker) queries data from the content script when the user clicks a button in the extension popup.

But the website is not fully a single-page application (SPA) and therefore whenever there is a page navigation, the cached data is lost and I cannot build the whole journey.

Question: Is there a way, I can get the data cached on the page before navigation and send it back to the navigated page?

Few considerations:

  1. The size of data is huge and might exceed the storage limits of browser local storage or session storage, i.e. 5MB.

  2. I persist the data in content script instead of background script for efficiency as the flux rate of data is high and there can be multiple such tabs open at a time.

Vaibhav Nigam
  • 1,334
  • 12
  • 21
  • You'll have to save the data in chrome.storage or in the service worker, anything else like onbeforeunload + sendMessage won't be reliable, AFAIK. – wOxxOm Jul 05 '23 at 19:55
  • @wOxxOm I tried onbeforeunload + chrome.storage.local.set but even that is not reliable. Saving to chrome.storage on every data event seems too expensive. – Vaibhav Nigam Jul 19 '23 at 20:29
  • Try the other thing I suggested, which is to save the data in the service worker. See also [Persistent Service Worker in Chrome Extension](https://stackoverflow.com/a/66618269) – wOxxOm Jul 20 '23 at 04:41

0 Answers0