Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MV3 Support #32

Closed
SidneyNemzer opened this issue May 6, 2024 · 0 comments · Fixed by #33
Closed

MV3 Support #32

SidneyNemzer opened this issue May 6, 2024 · 0 comments · Fixed by #33

Comments

@SidneyNemzer
Copy link
Owner

SidneyNemzer commented May 6, 2024

Background: Under MV3, the extension background page is no longer persistent; it terminates after a period of inactivity and restarts for the next event.

Problem 1: webext-redux doesn't support mv3

Fixed: tshaddix/webext-redux#297 (published this PR as webext-redux@3.0.0-mv3.0, will publish as latest soon)

The port object used by the store will become disconnected if the service worker sleeps, preventing the store from receiving updates.

  • Solution 1.1: use chrome.runtime.sendMessage instead of ports
  • Solution 1.2: pass custom port object that handles disconnects
    • webext-redux takes a port name string, not a port object, so this requires changes to webext-redux. Long-term maybe not a good idea since using native ports would be a common footgun for webext-redux users.

Problem 2: state is lost when service worker restarts

Fixed: Solution 2.1 is working well (chrome.storage.session).

  • Solution 2.1: persist state
    • chrome.storage.session and chrome.storage.local
      • 10 MB limit (each)
      • Maybe only persist files until they're saved?
      • Might require truncation logic to drop files to make state fit?
  • Solution 2.2: keep-alive
    • While a panel is open, ping the service worker using chrome.runtime.sendMessage() to keep it alive. It would be nice to allow the service worker to sleep but this is a quick fix, especially considering webext-redux will need changes to support the service worker sleeping.
    • After testing, this may not work. if panel pings service worker, the interval can be throttled by Chrome when it's in the background. the worker will miss pings and go to sleep. an interval in the service worker is more reliable, but it won't fire while the computer is asleep, still causing dropped pings and the service worker to sleep. tested with 25s and 5s interval
  • Solution 2.3: don't sync panels anymore
    • they can all have their own state, fetching, saving (continue using chrome sync storage for settings)
  • Solution 2.4: sync panels using chrome.storage.session
    • Replaces webext-redux
    • 10 MB limit
    • Use service worker to save data

Problem 3: Content Security Policy blocks linter worker

Fixed: disabled ace option loadWorkerFromBlob

Refused to load the script 'chrome-extension://afioclbfmnhmggdeelmdhdhcfchbjhbf/worker-javascript-eslint.js' because it violates the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval' 'inline-speculation-rules' http://localhost:* http://127.0.0.1:*". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'chrome-extension://afioclbfmnhmggdeelmdhdhcfchbjhbf/worker-javascript-eslint.js' failed to load.
at blob:chrome-extension://afioclbfmnhmggdeelmdhdhcfchbjhbf/00335355-995f-4e8a-8bb7-2f0cf4c3790a:1:1

Other notes

  • Tried testing MV2 event page behavior but it's not sleeping for some reason. Could investigate this more but not sure it really matters.

  • Idea: Create a port wrapper that reconnects when the background wakes or the service worker restarts

  • Idea: Create a timer lib that setups up chrome alarms for timers beyond 30s

  • When device sleeps, related events (disconnect ports, offline event) fires when the computer wakes, right before the online event.

    Events
    Time Offset Duration Event
    9:20:48 AM 0 s 17 s Port connected
    9:21:06 AM 18 s 10502 s Set pinging to true
    12:16:08 PM 10502 s 0 s Connection changed to offline
    12:16:08 PM 0 s 0 s Port disconnected
    12:16:08 PM 0 s 0 s Port connected
    12:16:11 PM 3 s 41 s Connection changed to online
@SidneyNemzer SidneyNemzer changed the title Service worker MV3 Support May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant